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-161155: Speed up ASCII lower() and upper() for str, bytes and bytearray by ptweezy · Pull Request #161201 · python/cpython

Conversation

@ptweezy

@ptweezy ptweezy commented Nov 2, 2026 •
edited
Loading

Uh oh!

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

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

  • Issue: Speed up ASCII str.lower() and str.upper() (2 to 4x at 1 KB) #161155

Sorry, something went wrong.

Uh oh!

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

All reactions
@ptweezy
pythongh-161155: Speed up ASCII lower() and upper() for str, bytes an…
b4d59af
…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.
@bedevere-app bedevere-app Bot added awaiting review type-feature A feature request or enhancement labels Nov 2, 2026
@bedevere-app bedevere-app Bot mentioned this pull request Nov 2, 2026
Speed up ASCII str.lower() and str.upper() (2 to 4x at 1 KB) #161155
Open
@python-cla-bot

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

Uh oh!

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

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

All reactions

Sorry, something went wrong.

Uh oh!

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

@ptweezy
ptweezy force-pushed the ascii-lower-upper-word branch from 0cda1cc to b4d59af Compare November 2, 2026 17:58
@ptweezy
Merge branch 'main' into ascii-lower-upper-word
4adeee1
ptweezy added a commit to ptweezy/cpython-ascii-case-bench that referenced this pull request Nov 2, 2026
@ptweezy
Add RESULTS.md with the tables for python/cpython#161201
ea5bff8
ptweezy added 8 commits November 2, 2026 14:26
@ptweezy
Shorten comments
a907379
@ptweezy
pythongh-161155: Let MSVC vectorize the word loop
91c788f
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.
@ptweezy
Merge branch 'main' into ascii-lower-upper-word
fcbd4c1
@ptweezy
pythongh-161155: Use the count-by-one loop only under MSVC
9433a66
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.
@ptweezy
Merge branch 'main' into ascii-lower-upper-word
9d2c049
@ptweezy
Merge branch 'main' into ascii-lower-upper-word
feb5e53
@ptweezy
Merge branch 'main' into ascii-lower-upper-word
492ef59
@ptweezy
Merge branch 'main' into ascii-lower-upper-word
1ad78fd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Uh oh!

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

Assignees

No one assigned

Labels

awaiting review type-feature A feature request or enhancement

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.

1 participant

@ptweezy
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.