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 — [mypyc] Speed up ord(str[n]) by inlining by JukkaL · Pull Request #20578 · python/mypy

Conversation

@JukkaL

@JukkaL JukkaL commented Jan 13, 2026

Copy link
Copy Markdown
Collaborator

The approach is similar to #20552, which added a fast inlined implementation of bytes get item. However, we do it for ord(str[n]) instead of just str[n], since the latter produces a reference-counted string of length 1, which is often too slow for performance-critical code. Later on the idea is to add a string writer class that supports quickly appending unicode code points represented as integers.

This makes a micro-benchmark that finds the highest unicode code point in a string about 18x faster.

Sorry, something went wrong.

Uh oh!

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

All reactions
JukkaL added 10 commits January 13, 2026 11:25
@JukkaL
WIP faster ord(str[i])
9e75d86
@JukkaL
Add inline specialized ops for ord(str[i])
5e0ff86
@JukkaL
Document
421ef63
@JukkaL
Minor refactoring
d3ef016
@JukkaL
These aren't experimental
058f314
@JukkaL
Fix test
107ada9
@JukkaL
Update doc
8708e5b
@JukkaL
Use more specific return type
e6ad733
@JukkaL
Add run tests
c9a845f
@JukkaL
Only run test on 64-bit platforms
dcd47d4
p-sawicki
p-sawicki approved these changes Jan 13, 2026
View reviewed changes
@JukkaL
JukkaL merged commit 59257a2 into master Jan 13, 2026
@JukkaL
JukkaL deleted the mypyc-ord-str branch January 13, 2026 17:44
michaelm-openai pushed a commit to michaelm-openai/mypy that referenced this pull request Jan 16, 2026
@JukkaL @michaelm-openai
[mypyc] Speed up ord(str[n]) by inlining (python#20578)
846b7f0
The approach is similar to python#20552, which added a fast inlined
implementation of bytes get item. However, we do it for `ord(str[n])`
instead of just `str[n]`, since the latter produces a reference-counted
string of length 1, which is often too slow for performance-critical
code. Later on the idea is to add a string writer class that supports
quickly appending unicode code points represented as integers.

This makes a micro-benchmark that finds the highest unicode code point
in a string about 18x faster.
michaelm-openai pushed a commit to michaelm-openai/mypy that referenced this pull request Jan 16, 2026
@JukkaL @michaelm-openai
[mypyc] Speed up ord(str[n]) by inlining (python#20578)
e4b1b96
The approach is similar to python#20552, which added a fast inlined
implementation of bytes get item. However, we do it for `ord(str[n])`
instead of just `str[n]`, since the latter produces a reference-counted
string of length 1, which is often too slow for performance-critical
code. Later on the idea is to add a string writer class that supports
quickly appending unicode code points represented as integers.

This makes a micro-benchmark that finds the highest unicode code point
in a string about 18x faster.
VaggelisD added a commit to VaggelisD/sqlglot-mypy that referenced this pull request May 11, 2026
@VaggelisD
[mypyc] Add librt.strings.isspace codepoint primitive
f189057
Adds a codepoint-taking `librt.strings.isspace(c: i32) -> bool` that
wraps `Py_UNICODE_ISSPACE`. Combined with the existing `ord(s[i])`
specialization (python#20578), this lets per-character hot loops avoid the
1-character `PyUnicode` materialization that `s[i].isspace()` forces.

Microbenchmark (counting whitespace in a 12 KB SQL fragment, 5000
iterations): mypyc-compiled `s[i].isspace()` takes 0.075 ms; the
codepoint path `c: i32 = i32(ord(s[i])); isspace(c)` takes 0.034 ms,
roughly 2.2x faster. Wins compound for tokenizer-shaped workloads
mixing classification and literal compares.
VaggelisD added a commit to VaggelisD/sqlglot-mypy that referenced this pull request May 11, 2026
@VaggelisD
[mypyc] Add librt.strings.isspace codepoint primitive
b1cd1fd
Adds a codepoint-taking `librt.strings.isspace(c: i32) -> bool` that
wraps `Py_UNICODE_ISSPACE`. Combined with the existing `ord(s[i])`
specialization (python#20578), this lets per-character hot loops avoid the
1-character `PyUnicode` materialization that `s[i].isspace()` forces.

Microbenchmark (counting whitespace in a 12 KB SQL fragment, 5000
iterations): mypyc-compiled `s[i].isspace()` takes 0.075 ms; the
codepoint path `c: i32 = i32(ord(s[i])); isspace(c)` takes 0.034 ms,
roughly 2.2x faster. Wins compound for tokenizer-shaped workloads
mixing classification and literal compares.
VaggelisD added a commit to VaggelisD/sqlglot-mypy that referenced this pull request May 11, 2026
@VaggelisD
[mypyc] Add librt.strings.isspace codepoint primitive
8b0557a
Adds a codepoint-taking `librt.strings.isspace(c: i32) -> bool` that
wraps `Py_UNICODE_ISSPACE`. Combined with the existing `ord(s[i])`
specialization (python#20578), this lets per-character hot loops avoid the
1-character `PyUnicode` materialization that `s[i].isspace()` forces.

Microbenchmark (counting whitespace in a 12 KB SQL fragment, 5000
iterations): mypyc-compiled `s[i].isspace()` takes 0.075 ms; the
codepoint path `c: i32 = i32(ord(s[i])); isspace(c)` takes 0.034 ms,
roughly 2.2x faster. Wins compound for tokenizer-shaped workloads
mixing classification and literal compares.
VaggelisD added a commit to VaggelisD/sqlglot-mypy that referenced this pull request Jun 2, 2026
@VaggelisD
[mypyc] Specialize s[i] == 'x' to a codepoint int compare
9df622f
Recognizes the AST shape `IndexExpr(str) == StrLiteral` (and the symmetric
`StrLiteral == IndexExpr(str)`, plus the `!=` variants) and lowers it to
an int compare of codepoints reusing the existing CPyStr_GetItemUnsafeAsInt
primitive.

Today the pattern lowers to CPyStr_GetItem + CPyStr_EqualLiteral, which
allocates or looks up a 1-character PyUnicode object per iteration and
goes through a generic string-equality call. After specialization it
becomes an inlined PyUnicode_READ plus an int compare -- about 4x faster
on bench_str_compare with a 3-compares-per-iteration workload, and closer
to ~9x with the more typical 1-compare-per-iteration shape.

No annotations required; benefits any code that compares a string index
against a 1-character literal. Multi-character / empty literals fall
through to the generic path (which still correctly returns False).
Bounds checking is preserved -- the helper raises IndexError for
out-of-range indices, same as the unspecialized path.

Stack: builds on the `ord(s[i])` primitive (python#20578) and the librt.strings
codepoint helpers (python#21462, python#21504, python#21509, python#21521, python#21522, python#21553).
VaggelisD added a commit to VaggelisD/sqlglot-mypy that referenced this pull request Jun 2, 2026
@VaggelisD
[mypyc] Specialize s[i] == 'x' to a codepoint int compare
60abe82
Recognizes the AST shape `IndexExpr(str) == StrLiteral` (and the symmetric
`StrLiteral == IndexExpr(str)`, plus the `!=` variants) and lowers it to
an int compare of codepoints reusing the existing CPyStr_GetItemUnsafeAsInt
primitive.

Today the pattern lowers to CPyStr_GetItem + CPyStr_EqualLiteral, which
allocates or looks up a 1-character PyUnicode object per iteration and
goes through a generic string-equality call. After specialization it
becomes an inlined PyUnicode_READ plus an int compare -- about 4x faster
on bench_str_compare with a 3-compares-per-iteration workload, and closer
to ~9x with the more typical 1-compare-per-iteration shape.

No annotations required; benefits any code that compares a string index
against a 1-character literal. Multi-character / empty literals fall
through to the generic path (which still correctly returns False).
Bounds checking is preserved -- the helper raises IndexError for
out-of-range indices, same as the unspecialized path.

Stack: builds on the `ord(s[i])` primitive (python#20578) and the librt.strings
codepoint helpers (python#21462, python#21504, python#21509, python#21521, python#21522, python#21553).
VaggelisD added a commit to VaggelisD/sqlglot-mypy that referenced this pull request Jun 4, 2026
@VaggelisD
[mypyc] Specialize s[i] == 'x' to a codepoint int compare
e116c95
Recognizes the AST shape `IndexExpr(str) == StrLiteral` (and the symmetric
`StrLiteral == IndexExpr(str)`, plus the `!=` variants) and lowers it to
an int compare of codepoints reusing the existing CPyStr_GetItemUnsafeAsInt
primitive.

Today the pattern lowers to CPyStr_GetItem + CPyStr_EqualLiteral, which
allocates or looks up a 1-character PyUnicode object per iteration and
goes through a generic string-equality call. After specialization it
becomes an inlined PyUnicode_READ plus an int compare -- about 4x faster
on bench_str_compare with a 3-compares-per-iteration workload, and closer
to ~9x with the more typical 1-compare-per-iteration shape.

No annotations required; benefits any code that compares a string index
against a 1-character literal. Multi-character / empty literals fall
through to the generic path (which still correctly returns False).
Bounds checking is preserved -- the helper raises IndexError for
out-of-range indices, same as the unspecialized path.

Stack: builds on the `ord(s[i])` primitive (python#20578) and the librt.strings
codepoint helpers (python#21462, python#21504, python#21509, python#21521, python#21522, python#21553).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@p-sawicki p-sawicki p-sawicki approved these changes

Uh oh!

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

Assignees

No one assigned

Labels

None yet

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.

2 participants

@JukkaL @p-sawicki
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 01402775 — 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.