GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-124951: Optimize base64 encode & decode for an easy 2-3x speedup [no SIMD] by gpshead · Pull Request #143262 · python/cpython

Conversation

@gpshead

@gpshead gpshead commented Dec 29, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

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 groups

The 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):

             64 bytes    64K        1M
  Zen2:      1.2x/1.8x   1.7x/2.8x  1.5x/2.8x
  Zen4:      1.2x/1.7x   1.6x/3.0x  1.5x/3.0x  [old data, likely faster]
  M4:        1.3x/1.9x   2.3x/2.8x  2.4x/2.9x  [old data, likely faster]
  RPi5-32:   1.2x/1.2x   2.4x/2.4x  2.0x/2.1x
  RPi4-64:   1.3x/2.0x   2.4x/5.0x  1.8x/5.0x

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 as simdutf (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 in main...gpshead:cpython:claude/vectorize-base64-c-S7Hku

  • Issue: base64 module: Link against SIMD library for 10x performance. #124951

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

stonebig, reneleonhardt, madrury, and agriyakhetarpal reacted with heart emoji johnslavik, Fidget-Spinner, dolfinus, emmatyping, and agriyakhetarpal reacted with rocket emoji
All reactions
  • ❤️ 4 reactions
  • 🚀 5 reactions
@gpshead @claude
Add base64 benchmark tool and optimize encoding/decoding
2d2be30
Add Tools/binasciibench/binasciibench.py benchmark for measuring base64
encoding/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 groups

Performance 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.9x

Additional 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 <[email protected]>
@gpshead
gpshead force-pushed the opt-base64-cpu branch from f15ad3e to 2d2be30 Compare December 29, 2025 00:14
@gpshead gpshead changed the title Optimize base64 encode and decode for an easy 2-3x performance win gh-124951: Optimize base64 encode and decode for an easy 2-3x performance win [no SIMD required] Dec 29, 2025
@bedevere-app bedevere-app Bot mentioned this pull request Dec 29, 2025
base64 module: Link against SIMD library for 10x performance. #124951
Open
@gpshead gpshead added the performance Performance or resource usage label Dec 29, 2025
@gpshead gpshead changed the title gh-124951: Optimize base64 encode and decode for an easy 2-3x performance win [no SIMD required] gh-124951: Optimize base64 encode and decode for an easy 2-3x speedup [no SIMD required] Dec 29, 2025
@gpshead gpshead changed the title gh-124951: Optimize base64 encode and decode for an easy 2-3x speedup [no SIMD required] gh-124951: Optimize base64 encode & decode for an easy 2-3x speedup [no SIMD] Dec 29, 2025
gpshead and others added 3 commits December 29, 2025 00:30
@gpshead @claude
Fix MSVC build: move table_b2a_base64 before inline functions
573eaf3
MSVC doesn't support forward declarations of arrays without explicit
size. Move the table definition before the inline functions that use
it, eliminating the need for a forward declaration.

Co-authored-by: Claude Opus 4.5 <[email protected]>
@gpshead
NEWS entry
eaac671
@gpshead
Add a whatsnew entry
060dbf5
picnixz
picnixz reviewed Dec 29, 2025
View reviewed changes
Comment thread Tools/binasciibench/binasciibench.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@gpshead @picnixz
expose the benchmark defaults in the help text.
1e12273
Co-authored-by: Bénédikt Tran <[email protected]>
@gpshead
gpshead marked this pull request as ready for review December 29, 2025 01:08
@gpshead
gpshead requested a review from AA-Turner as a code owner December 29, 2025 01:08
@bedevere-app bedevere-app Bot added the awaiting core review label Dec 29, 2025
@gpshead
gpshead requested a review from serhiy-storchaka December 29, 2025 01:08
@gpshead gpshead self-assigned this Dec 29, 2025
gpshead and others added 3 commits December 29, 2025 01:40
@gpshead @claude
Align base64 tables to 64-byte cache line boundaries
ef38895
Add Py_ALIGNED(64) to both lookup tables to ensure each fits
within a single L1 cache line, reducing potential cache misses
during encoding/decoding loops.

Co-authored-by: Claude Opus 4.5 <[email protected]>
@gpshead @claude
Use BASE64_PAD macro instead of literal '=' in fast path
7458c99
Replace hardcoded '=' characters with the BASE64_PAD macro
for consistency with the rest of the codebase.

Co-authored-by: Claude Opus 4.5 <[email protected]>
@gpshead @claude
Simplify block comment for base64 helpers
1f8ff74
Co-authored-by: Claude Opus 4.5 <[email protected]>
@gpshead
gpshead force-pushed the opt-base64-cpu branch from aad7a15 to 1f8ff74 Compare December 29, 2025 02:23
gpshead
gpshead commented Dec 29, 2025
View reviewed changes
Comment thread Tools/binasciibench/binasciibench.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@gpshead gpshead mentioned this pull request Dec 29, 2025
Add bm_base64 covering common base64 module APIs python/pyperformance#447
Merged
@gpshead
Remove binasciibench in favor of pyperformance bm_base64 addition PR.
4b1245b
serhiy-storchaka
serhiy-storchaka reviewed Dec 29, 2025
View reviewed changes

@serhiy-storchaka serhiy-storchaka left a comment

Copy link
Copy Markdown
Member

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 for ignorechars 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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

gpshead reacted with thumbs up emoji gpshead reacted with heart emoji
All reactions
  • 👍 1 reaction
  • ❤️ 1 reaction
Comment thread Doc/whatsnew/3.15.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Modules/binascii.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Modules/binascii.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Modules/binascii.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Modules/binascii.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Modules/binascii.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

@serhiy-storchaka serhiy-storchaka mentioned this pull request Dec 29, 2025
gh-101178: Add Ascii85, Base85, and Z85 support to binascii #102753
Merged
gpshead and others added 4 commits January 2, 2026 04:28
@gpshead
reword L1 cache line comment
4b50c73
@gpshead
doc Contributed by
f7b27ee
@gpshead @claude
Remove redundant PAD check in base64 decode fast path
2b13b81
Address review feedback from serhiy-storchaka: the fast path was doing
two checks per group - an explicit PAD comparison and the invalid char
check in base64_decode_quad().

Change PAD's table entry from 0 to 64 so the existing (v0|v1|v2|v3)&0xc0
check catches it, eliminating 4 comparisons per group.

The slow path is unaffected since it checks for PAD character before
the table lookup.

Decode is ~16% faster at 64K (1.62 GB/s → 1.88 GB/s).

Co-authored-by: Claude Opus 4.5 <[email protected]>
@gpshead @claude
Use pointer increment in base64_encode_fast
30976a9
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 <[email protected]>
@gpshead
gpshead merged commit 61fc72a into python:main Jan 2, 2026
46 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the awaiting core review label Jan 2, 2026
serhiy-storchaka
serhiy-storchaka reviewed Jan 2, 2026
View reviewed changes
Comment thread Modules/binascii.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

thunder-coding pushed a commit to thunder-coding/cpython that referenced this pull request Feb 15, 2026
@gpshead @thunder-coding
pythongh-124951: Optimize base64 encode & decode for an easy 2-3x spe…
56d4916
…edup [no SIMD] (pythonGH-143262)

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 groups

Performance gains (encode/decode speedup vs main, PGO builds):
```
             64 bytes    64K        1M
  Zen2:      1.2x/1.8x   1.7x/2.8x  1.5x/2.8x
  Zen4:      1.2x/1.7x   1.6x/3.0x  1.5x/3.0x  [old data, likely faster]
  M4:        1.3x/1.9x   2.3x/2.8x  2.4x/2.9x  [old data, likely faster]
  RPi5-32:   1.2x/1.2x   2.4x/2.4x  2.0x/2.1x
```

Based on my exploratory work done in python/cpython@main...gpshead:cpython:claude/vectorize-base64-c-S7Hku

See PR and issue for further thoughts on sometimes MUCH faster SIMD vectorized versions of this.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@serhiy-storchaka serhiy-storchaka serhiy-storchaka left review comments
@picnixz picnixz picnixz left review comments
@AA-Turner AA-Turner Awaiting requested review from AA-Turner AA-Turner is a code owner

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@gpshead gpshead

Labels

performance Performance or resource usage

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@gpshead @serhiy-storchaka @picnixz
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.