Skip to content

gh-161155: Speed up ASCII lower() and upper() for str, bytes and bytearray - #161201

Open
ptweezy wants to merge 6 commits into
python:mainfrom
ptweezy:ascii-lower-upper-word
Open

ptweezy wants to merge 6 commits into
python:mainfrom
ptweezy:ascii-lower-upper-word

Conversation

@ptweezy

@ptweezy ptweezy commented Nov 2, 2026 •

Copy link
Copy Markdown

_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

…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.
@python-cla-bot

python-cla-bot Bot commented Nov 2, 2026 •

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

@ptweezy
ptweezy force-pushed the ascii-lower-upper-word branch from 0cda1cc to b4d59af Compare November 2, 2026 17:58
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review type-feature A feature request or enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant