Title: Proposal: Native `char` type for fast char ops in mypyc · Issue #21418 · python/mypy · GitHub
Open Graph Title: Proposal: Native `char` type for fast char ops in mypyc · Issue #21418 · python/mypy
X Title: Proposal: Native `char` type for fast char ops in mypyc · Issue #21418 · python/mypy
Description: Feature Add mypy_extensions.char as a mypyc native type for one Unicode codepoint, stored unboxed as int32. Models the existing i64 / i32 / i16 / u8 pattern, applied to str. Pitch CPython already has codepoint-classification macros (Py_U...
Open Graph Description: Feature Add mypy_extensions.char as a mypyc native type for one Unicode codepoint, stored unboxed as int32. Models the existing i64 / i32 / i16 / u8 pattern, applied to str. Pitch CPython already h...
X Description: Feature Add mypy_extensions.char as a mypyc native type for one Unicode codepoint, stored unboxed as int32. Models the existing i64 / i32 / i16 / u8 pattern, applied to str. Pitch CPython already h...
Opengraph URL: https://github.com/python/mypy/issues/21418
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Proposal: Native `char` type for fast char ops in mypyc","articleBody":"### **Feature**\n\nAdd `mypy_extensions.char` as a mypyc native type for one Unicode codepoint, stored unboxed as `int32`. Models the existing `i64` / `i32` / `i16` / `u8` pattern, applied to `str`.\n\n\n### **Pitch**\n\nCPython already has codepoint-classification macros (`Py_UNICODE_ISALNUM`, `Py_UNICODE_ISSPACE`, `Py_UNICODE_TOUPPER`, etc.) that take an integer codepoint. The current str-method path goes through a 1-character `PyUnicode` only to dispatch into those same macros; `char` skips the round trip. The result is that `PyObject` operations, refcount pairs etc collapse to inlined integer ops. \n\n\nConsider this function which scans forward while the char is part of a word (could be part of a lexer):\n\n```python3\ndef find_word_end(s: str, start: int) -> int:\n i = start\n while i \u003c len(s):\n c = s[i]\n if not (c.isalnum() or c == '_'):\n return i\n i += 1\n return i\n```\n\n\u003cbr />\n\n**Today**, with `c: str` inferred, the loop body lowers to roughly:\n\n```c\nPyObject *t = CPyStr_GetItem(s, i); // cache lookup, INCREF\nint alnum = CPyStr_IsAlnum(t); // method dispatch on the 1-char str\nint eq = CPyStr_Equal(t, underscore); // string compare\nPy_DECREF(t); // refcount op\n```\n\n\u003cbr />\n\n\n**With `c: char = s[i]`** (the proposed type), the same body lowers to:\n\n```c\nint32_t c = CPyStr_GetCharAt(s, i); // inlined PyUnicode_READ\nint alnum = Py_UNICODE_ISALNUM(c); // inline macro on int32\nint eq = (c == '_'); // int compare\n```\n\n\u003cbr />\n\nSpeedups from a [working prototype](https://github.com/VaggelisD/sqlglot-mypy/commit/b3fbc77600e53c51dfcd3a9cb5addacd835fc72b), both columns mypyc-compiled:\n\n| Workload | Speedup |\n|---|---:|\n| `c.isspace()` per char | 3.27x |\n| `c.isdigit()` per char | 1.63x |\n| ASCII `c.upper()` | 10.92x |\n| Identifier validator (tokenizer-shaped) | 1.57x |\n| Token classifier (lexer inner loop) | 2.12x |\n\n\u003cbr />\n\nNote: On Latin-1 text (most source-code-like input), `s[i]` does not actually allocate, since CPython caches single-character strings under `U+0100`. The savings are refcount traffic, function-call overheads, and the codepoint-to-`PyObject`-and-back round trip that `s[i].method()` forces. For non-Latin-1 text the picture changes and `char` would pull further ahead.\n\nImplementation would follow the existing `i64` template: new rprimitive, the same type-promotion plumbing that makes `i64` and `int` interoperate, codepoint-taking method ops for the common `str` methods, and one IR pass to fold `s[i] -> char` into a direct int32 read. There are a couple of design choices to settle along the way (promotion direction, runtime class shape), but the question I'm asking here is whether the team is interested in the direction at all.\n","author":{"url":"https://github.com/VaggelisD","@type":"Person","name":"VaggelisD"},"datePublished":"2026-05-06T10:59:42Z","url":"https://github.com/python/mypy/issues/21418"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:d0d65333-b4c9-0289-0710-c45593783a49 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 88D4:30263F:243827B:3153B29:6B0033F6 |
| html-safe-nonce | 4ab8f8ae60415b83bb44d252143ee3b599e4ec93d3754d10216f6fc60f012251 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4OEQ0OjMwMjYzRjoyNDM4MjdCOjMxNTNCMjk6NkIwMDMzRjYiLCJ2aXNpdG9yX2lkIjoiNzc5Mzg0NDI2NzgzNDY4NDQwNiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | c43e38649a2ac4eafeef4246bd40ac9c5585961427b968a7733b9606e6801808 |
| hovercard-subject-tag | issue:4390792454 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/mypy/21418/issue_layout |
| twitter:image | https://opengraph.githubassets.com/fff51c0361147fa542812824bb7f049b9ebc9e8efddcaf246d9fc85fb2f6cdce/python/mypy/issues/21418 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/fff51c0361147fa542812824bb7f049b9ebc9e8efddcaf246d9fc85fb2f6cdce/python/mypy/issues/21418 |
| og:image:alt | Feature Add mypy_extensions.char as a mypyc native type for one Unicode codepoint, stored unboxed as int32. Models the existing i64 / i32 / i16 / u8 pattern, applied to str. Pitch CPython already h... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | VaggelisD |
| hostname | github.com |
| expected-hostname | github.com |
| None | 7d0829bbde9e48b2486cb5dd6d147896eb01d32134627091f818958a935ef3f0 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/mypy git https://github.com/python/mypy.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 7053637 |
| octolytics-dimension-repository_nwo | python/mypy |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 7053637 |
| octolytics-dimension-repository_network_root_nwo | python/mypy |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | f3f3a43ca095cdea3abbb394112e971f9fdb94fa |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width