Skip to content

binascii: Reduce module RAM overhead and compiled footprint. - #1157

Open
agatti wants to merge 1 commit into
micropython:masterfrom
agatti:binascii
Open

binascii: Reduce module RAM overhead and compiled footprint.#1157
agatti wants to merge 1 commit into
micropython:masterfrom
agatti:binascii

Conversation

@agatti

@agatti agatti commented Sep 6, 2026

Copy link
Copy Markdown
Contributor

Summary

This PR makes some changes to the binascii module to reduce its footpint once compiled, and marginally improve the time it takes to import the module itself.

The decoding table is now precomputed as a 256 entries long bytes object rather than doing the table building at import time. The table was built as a list of integers, so there's some considerable overhead compared to a single bytes object.

Decoding data would also do some unnecessary roundtrips between integers and characters for the table lookup to figure out the 6-bits sequence to use for a given character. The original code, despite having a function called "a2b_base64" does not work with strings, so the roundtrips were not needed to begin with. CPython's equivalent works with strings too, but the changes proposed here do not change the function's behaviour.

There was some dead code in the encoder function, used to calculate the final size of the output buffer. The code uses a list anyway, so this is not needed.

The tables are now built using const(), and made private (hence the minor version number being bumped up). This saves even more space when compiled.

Finally, the base64 part of the test suite was updated a bit to cover more padding cases.

These changes reduce the compiled module size by 204 bytes.

Testing

The existing CI tests suite still passes, and the test file also runs as expected with CPython once moved out of its original directory (so the MicroPython binascii module isn't used instead).

Trade-offs and Alternatives

binascii.a2b_base64 could be modified to also work with strings, following CPython, at the expense of a slightly reduced savings figure.

Generative AI

I did not use generative AI tools when creating this PR.

This commit makes some changes to the `binascii` module to reduce its
footpint once compiled, and marginally improve the time it takes to
import the module itself.

The decoding table is now precomputed as a 256 entries long `bytes`
object rather than doing the table building at import time.  The table
was built as a list of integers, so there's some considerable overhead
compared to a single `bytes` object.

Decoding data would also do some unnecessary roundtrips between
integers and characters for the table lookup to figure out the 6-bits
sequence to use for a given character.  The original code, despite
having a function called "a2b_base64" does not work with strings, so
the roundtrips were not needed to begin with.  CPython's equivalent
works with strings too, but the changes proposed here do not change
the function's behaviour.

There was some dead code in the encoder function, used to calculate the
final size of the output buffer.  The code uses a list anyway, so this
is not needed.

The tables are now built using `const()`, and made private (hence the
minor version number being bumped up).  This saves even more space when
compiled.

Finally, the base64 part of the test suite was updated a bit to cover
more padding cases.

These changes reduce the compiled module size by 204 bytes.

Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant