← index #15513PR #5583
Related · high · value 2.885
QUERY · ISSUE

Base64 encoding into buffer, to reduce allocations

openby jonnoropened 2024-07-20updated 2024-07-27
enhancement

Description

base64 encoding is supported by binasci.b2a_base64. This implements the standard API in CPython. It always allocates a new buffer, and returns it. The allocations could be reduced/eliminated if a b2a_base64_into type API would (also) be available. This function would take an additional parameter: the buffer (bytearray) that is to be filled with encoded data. The caller must make sure it is sufficiently large (4/3 the size of the input + up to 4 bytes of padding).
This is similar to how there is struct.unpack_into, et.c.

Code Size

Existing code could be moved into the b2a_base64_into implementation. And then have b2a_base64 be a compatibility wrapper, which creates the needed buffer internally. This should make the code increase minimal.

Implementation

I hope the MicroPython maintainers or community will implement this feature

Code of Conduct

Yes, I agree

CANDIDATE · PULL REQUEST

Base64Url encoding

closedby odewdneyopened 2020-01-26updated 2022-06-24
extmod

The default base64 implementation adds a linefeed to the end of the string. To perform a base64url encoding we need to remove this, the padding and translate the two replaced characters. This is fine on a desktop, but on a resource constrained device this involves multiple copies of the string. This PR adds an optional parameter to the b2a_base64 and a2b_base64 that allow the caller to control the encoding with an const/enum defined bit flags. Also the a2b function allocates the correct output buffer size.

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied