Skip to content

[mypyc] Add librt.strings.cached_chr and char_at - #22465

Open
Jahongir-Qurbonov wants to merge 3 commits into
python:masterfrom
Jahongir-Qurbonov:mypyc-str-char-cache
Open

Jahongir-Qurbonov wants to merge 3 commits into
python:masterfrom
Jahongir-Qurbonov:mypyc-str-char-cache

Conversation

@Jahongir-Qurbonov

@Jahongir-Qurbonov Jahongir-Qurbonov commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

CPython caches single-character strings only for Latin-1. In compiled code, chr(n), s[i] and for c in s therefore allocate and free a new str for 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.strings instead of changing the built-in operations:

  • cached_chr(c: i64) -> str: like chr(c)
  • char_at(s: str, i: i64) -> str: like s[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.strings module and compiled code reaches it through a new capsule API function, so all compiled modules share one cache (LIBRT_STRINGS_API_VERSION is 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.

Text for c in s s[i] char_at(s, i) chr(ord(s[i])) cached_chr(ord(s[i]))
ASCII 6.2 6.5 6.4 6.3 6.0
Cyrillic 14.0 13.5 5.5 11.4 5.6
Greek 13.4 13.7 5.7 11.6 5.5
Hindi 13.6 13.5 5.6 11.8 5.6
random CJK 13.7 13.1 12.1 11.7 12.4
random emoji 13.5 13.0 12.0 11.8 12.2

Code points outside the cached range aren't affected (the differences are within noise).

Testing

  • New run test testLibrtStringsCachedChars_librt compares both functions with chr and s[i] across Latin-1, the cached range and beyond, including cache hits on a second pass, error cases, calls through Any (the interpreted wrappers), and that c += "x" on a cached string doesn't modify it. It also checks that compiled and interpreted calls return the same cached object.
  • New irbuild tests for both primitives.
  • librt_strings.c compiles with -Werror against free-threaded 3.14 and 3.10 headers.

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 JukkaL left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
@Jahongir-Qurbonov Jahongir-Qurbonov changed the title [mypyc] Cache single-character strings up to U+0FFF [mypyc] Add librt.strings.cached_chr and char_at Nov 16, 2026
@github-actions

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants