GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — [mypyc] Speed up ord(str[n]) by inlining by JukkaL · Pull Request #20578 · python/mypy Conversation
Collaborator
The approach is similar to #20552, which added a fast inlined implementation of bytes get item. However, we do it for 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
p-sawicki
approved these changes
michaelm-openai
pushed a commit
to michaelm-openai/mypy
that referenced
this pull request
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
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
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
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
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
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
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
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
ReviewersUh oh!
There was an error while loading. Please reload this page. AssigneesNo one assignedLabels
None yet
ProjectsMilestoneNo milestoneDevelopmentSuccessfully 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. |
| 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. |