GeezerVison is showing github.com — gh-161155: Speed up ASCII lower() and upper() for str, bytes and bytearray by ptweezy · Pull Request #161201 · python/cpython
Conversation
ptweezy
commented
Nov 2, 2026
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
_Py_bytes_lower() and _Py_bytes_upper(), used by ASCII str and by bytes/bytearray lower(), upper() and capitalize(), now convert a machine word at a time; strings shorter than a word keep the existing loop. The word loop is a separate non-inlined function so short strings skip its setup, and its helpers are Py_ALWAYS_INLINE because gcc's PGO build otherwise calls one per word. Under MSVC the word loop counts words, since MSVC only vectorizes loops that count by one.
1.7x to 4.2x faster at 1,024 characters on Linux, Windows and macOS (x86-64 and arm64, release and PGO builds); 0.94x to 1.13x below 8 characters. Tables, including builds without auto-vectorization: https://github.com/ptweezy/cpython-ascii-case-bench/blob/main/RESULTS.md
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
…d bytearray
_Py_bytes_lower() and _Py_bytes_upper() now convert strings of at least
one machine word (8 bytes on 64-bit platforms, 4 on 32-bit) a word at a
time with integer arithmetic. When the length is not a multiple of the
word size, the last word overlaps the one before it. Shorter strings
still take the existing table loop.
str.lower(), str.upper() and str.casefold() use these functions for
ASCII strings, as do lower(), upper() and capitalize() on bytes and
bytearray.
|
All commit authors signed the Contributor License Agreement.

|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
ptweezy
added a commit
to ptweezy/cpython-ascii-case-bench
that referenced
this pull request
Nov 2, 2026
Count words instead of bytes, index them through an array-of-words
type, and compute the two per-call constants before the loop. MSVC
only vectorizes loops whose counter steps by one, and with the
multiplication by "first" inside the loop it vectorizes that with an
AVX-512 instruction and runs the vector loop only on AVX-512 CPUs.
The unsigned word count avoids an idiv in size-optimized (PGO) code,
and converting the overlapping last word first keeps the loop as
short as before for compilers that don't vectorize it.
Counting words makes clang use post-indexed loads and stores on
AArch64, which are slower on Apple cores, so step by bytes as before
everywhere except under MSVC, which only vectorizes a loop that counts
by one. Both loops get the constants from outside the loop (otherwise
MSVC vectorizes the multiplication with an AVX-512 instruction), and
the overlapping last word is converted after the loop again.
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
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.
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.
|