← index #15513PR #3254
Off-topic · high · value 1.191
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

a2b_base64: Ignore invalid chars like CPython

closedby alex-robbinsopened 2017-08-05updated 2017-08-20

Like CPython, MicroPython's b2a_base64 function adds a '\n' to the encoded data. For decoding, however, CPython's a2b_base64 ignores invalid characters in its input, while MicroPython's does not. Besides hurting compatibility with CPython, this means that MicroPython cannot decode its own Base64-encoded data; a2b_base64(b2a_base64(b'foo')) fails.

In this patch I completely rewrote the a2b_base64 function to ignore invalid characters, basing it heavily on CPython's implementation. It is intended to work identically; if anyone finds any discrepancies, that'd be worth mentioning.

This change reduces code size on my 64-bit Unix platform by 160 bytes.

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