Repository navigation
[mypyc] Add librt.strings.cached_chr and char_at - #22465
Open
Jahongir-Qurbonov wants to merge 3 commits into
Open
Jahongir-Qurbonov wants to merge 3 commits into
Jahongir-Qurbonov wants to merge 3 commits into
Conversation
CPython only caches single-character Latin-1 strings, so iterating over or indexing text in other alphabetic scripts (Greek, Cyrillic, Arabic, Indic scripts, etc.) allocated and freed a new str object for every character. In compiled code the allocation dominated the cost of `for c in s`. Cache single-character strings for U+0100..U+0FFF in a table that is filled on first use and never evicted, and use it for str indexing, iteration and chr(). Larger code points such as CJK aren't cached: a small direct-mapped cache made random CJK text ~35% slower due to evictions. The cache is disabled on free-threaded builds.
JukkaL
reviewed
Nov 16, 2026
JukkaL
left a comment
Collaborator
There was a problem hiding this comment.
The up to 230kB of additional memory use is quite a lot, though I can see how this could benefit some use cases.
Since this makes a different tradeoff than the stdlib chr, this would be a good candidate to be added to librt.strings, with the memory/speed tradeoff documented. This way users can decide to opt into this when chr on Unicode codepoints is a performance bottleneck and they are fine with the extra memory use.
Add librt.strings.cached_chr and char_at, which cache single-character strings for U+0100..U+0FFF, instead of using the cache in the built-in chr(), str indexing and iteration, since the cache uses up to ~230 kB of memory. The cache is shared by all compiled modules via a new capsule API function.
Contributor
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
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.
CPython caches single-character strings only for Latin-1. In compiled code,
chr(n),s[i]andfor c in stherefore allocate and free a newstrfor every character in most other alphabetic scripts, and the allocation is most of the cost of a loop that processes such text one character at a time.As suggested in review, this adds the cache as opt-in functions in
librt.stringsinstead of changing the built-in operations:cached_chr(c: i64) -> str: likechr(c)char_at(s: str, i: i64) -> str: likes[i](negative indices are supported)Both cache the single-character strings for code points in U+0100..U+0FFF (Latin Extended, Greek, Cyrillic, Armenian, Hebrew, Arabic, Indic scripts, Thai, Georgian, ...). Entries are created on first use and never freed. The table is 30 kB of pointers, plus ~60 bytes for each distinct character used (at most ~230 kB). The docs describe the tradeoff. The built-in
chr, indexing and iteration are unchanged.The cache lives in the
librt.stringsmodule and compiled code reaches it through a new capsule API function, so all compiled modules share one cache (LIBRT_STRINGS_API_VERSIONis bumped to 5). Latin-1 code points and the range checks are handled inline in compiled code. The cache isn't used on free-threaded builds, or when the functions are called from interpreted code in a subinterpreter, since cached objects can't be shared between interpreters.Benchmark
1M-character strings, ms, best of 9, CPython 3.13, Linux x86_64,
MYPYC_OPT_LEVEL=3. Each loop compares the character against a non-literal string, so that the comparison isn't specialized.for c in ss[i]char_at(s, i)chr(ord(s[i]))cached_chr(ord(s[i]))Code points outside the cached range aren't affected (the differences are within noise).
Testing
testLibrtStringsCachedChars_librtcompares both functions withchrands[i]across Latin-1, the cached range and beyond, including cache hits on a second pass, error cases, calls throughAny(the interpreted wrappers), and thatc += "x"on a cached string doesn't modify it. It also checks that compiled and interpreted calls return the same cached object.librt_strings.ccompiles with-Werroragainst free-threaded 3.14 and 3.10 headers.