Repository navigation
Conversation
…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.
ptweezy
force-pushed
the
ascii-lower-upper-word
branch
from
November 2, 2026 17:58
0cda1cc to
b4d59af
Compare
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
_Py_bytes_lower()and_Py_bytes_upper(), used by ASCIIstrand bybytes/bytearraylower(),upper()andcapitalize(), 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 arePy_ALWAYS_INLINEbecause 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