Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork34k
gh-124951: Optimize base64 encode & decode for an easy 2-3x speedup [no SIMD]#143262
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
Add Tools/binasciibench/binasciibench.py benchmark for measuring base64encoding/decoding throughput.Optimize base64 encoding/decoding by eliminating loop-carried dependencies.Key changes:- Add base64_encode_trio() and base64_decode_quad() helper functions that process complete groups independently- Add base64_encode_fast() and base64_decode_fast() wrappers- Update b2a_base64 and a2b_base64 to use fast path for complete groupsPerformance gains (encode/decode speedup vs main, PGO builds): 64 bytes 64K 1M Zen2: 1.1x/1.6x 1.6x/2.4x 1.4x/2.4x Zen4: 1.2x/1.7x 1.6x/3.0x 1.5x/3.0x M4: 1.3x/1.9x 2.3x/2.8x 2.4x/2.9x RPi5-32: 1.4x/1.4x 2.4x/2.0x 2.0x/1.9xAdditional SIMD implementations (NEON, AVX-512 VBMI) can achieve+50% to +1500% further gains and are planned for follow-on work.Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
MSVC doesn't support forward declarations of arrays without explicitsize. Move the table definition before the inline functions that useit, eliminating the need for a forward declaration.Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
Uh oh!
There was an error while loading.Please reload this page.
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
Add Py_ALIGNED(64) to both lookup tables to ensure each fitswithin a single L1 cache line, reducing potential cache missesduring encoding/decoding loops.Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
Replace hardcoded '=' characters with the BASE64_PAD macrofor consistency with the rest of the codebase.Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
Uh oh!
There was an error while loading.Please reload this page.
serhiy-storchaka left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Looks pretty simple with large benefit.
BTW, I'm going to add support forignorechars in the decoder, so it could support a multiline input without ignoring all other errors. The decoder will return on the fast path for each line.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Address review feedback from serhiy-storchaka: the fast path was doingtwo checks per group - an explicit PAD comparison and the invalid charcheck in base64_decode_quad().Change PAD's table entry from 0 to 64 so the existing (v0|v1|v2|v3)&0xc0check catches it, eliminating 4 comparisons per group.The slow path is unaffected since it checks for PAD character beforethe table lookup.Decode is ~16% faster at 64K (1.62 GB/s → 1.88 GB/s).Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
Suggested by serhiy-storchaka: replace index math (in + i*3, out + i*4)with pointer increments. Encode is ~7% faster at 64K (2.11 → 2.25 GB/s).🤖 Generated with [Claude Code](https://claude.com/claude-code)Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
61fc72a intopython:mainUh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Optimize base64 encoding/decoding by eliminating loop-carried dependencies. Key changes:
base64_encode_trio()andbase64_decode_quad()helper functions that process complete groups independentlybase64_encode_fast()andbase64_decode_fast()wrappersb2a_base64anda2b_base64to use fast path for complete groupsThe binasciibench I used measuring base64 encoding/decoding throughput is included in commit history, but i pulled it out of the PR in favor of adding to pyperformance.
Performance gains (encode/decode speedup vs main, PGO builds):
Additional SIMD implementations (NEON, AVX-512 VBMI) can achieve +50% (M4) to +1500% (!! Zen4) further gains and are planned for follow-on work if deemed simple to maintain.
Widely used third party libraries contain industry canonical SIMD accelerated variants such assimdutf (C++ based unfortunately) so the decision of how to link and use those and when is best kept separate.
This PR's simple pure better use of modern CPU functional unit pipelining wins make sense regardless.
Based on my exploratory work done inmain...gpshead:cpython:claude/vectorize-base64-c-S7Hku
base64module: Link against SIMD library for 10x performance. #124951