Commit 6bcd02e
authored
[mypyc] Use cached ASCII characters in
For characters < 256, use `PyUnicode_FromOrdinal()` which returns
CPython's cached single-char Latin-1 string objects instead of
allocating a new PyUnicode object on every `str[i]` access. This avoids
allocation+deallocation overhead in character-scanning hot loops.
Characters >= 256 (BMP, supplementary) keep the original `PyUnicode_New`
allocation path unchanged.
I ran the following micro-benchmark: Scan a 50k-character string with
`s[i]` in a loop (repeated the benchmark 5000 times):
| String type | Before (ms/iter) | After (ms/iter) | Speedup |
|--------------------------|-------------------|-----------------|-----------------|
| ASCII (0–127) | 0.651 | 0.166 | **3.9x (-75%)** |
| Latin-1 (128–255) | 0.752 | 0.162 | **4.6x (-78%)** |
| BMP (256–65535) | 0.901 | 0.809 | no change |
| Supplementary (>65535) | 0.842 | 0.743 | no change |
| Mixed (25% each) | 0.817 | 0.542 | **1.5x (-34%)** |
<br />
This was coauthored with @tobymaoCPyStr_GetItem (#21035)1 parent 0183a21 commit 6bcd02e
2 files changed
+41
-4
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
109 | 109 | | |
110 | 110 | | |
111 | 111 | | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
112 | 118 | | |
113 | 119 | | |
114 | 120 | | |
115 | | - | |
116 | | - | |
117 | | - | |
118 | | - | |
| 121 | + | |
119 | 122 | | |
120 | 123 | | |
121 | 124 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
264 | 264 | | |
265 | 265 | | |
266 | 266 | | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
267 | 301 | | |
268 | 302 | | |
269 | 303 | | |
| |||
0 commit comments