Commit 21f3f3f
committed
Optimize CPython PEG parser: 29% speedup on stdlib parsing
Stack of optimizations to the PEG parser that together yield a ~29%
throughput improvement (4135ms -> 2924ms parsing 1867 stdlib files):
1. Reduce stack overflow check frequency: only call
_Py_ReachedRecursionLimitWithMargin every 100 recursion levels
instead of every single rule entry (+16%)
2. Lazy token bytes: defer PyBytes_FromStringAndSize allocation until
token bytes are actually accessed, avoiding allocation for tokens
that are only type-checked (+2% incremental)
3. Inline _PyPegen_expect_token: move the hottest function (called
for every token match) from pegen.c to a static inline in
pegen.h, eliminating function call overhead (+3% incremental)
4. Inline _PyPegen_is_memoized: move memo lookup (called for every
memoized rule) to a static inline in pegen.h for non-debug
builds (+3% incremental)
5. Direct identifier creation: create PyUnicode identifiers directly
from raw token pointers (start, len) instead of going through
PyBytes intermediary (+5% incremental)
https://claude.ai/code/session_0116H8dSsjY7pmMiZs5WWjF31 parent f615556 commit 21f3f3f
File tree
6 files changed
+629
-506
lines changed- Parser
- Tools/peg_generator/pegen
6 files changed
+629
-506
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
902 | 902 | | |
903 | 903 | | |
904 | 904 | | |
905 | | - | |
| 905 | + | |
906 | 906 | | |
907 | 907 | | |
908 | 908 | | |
| |||
919 | 919 | | |
920 | 920 | | |
921 | 921 | | |
922 | | - | |
923 | 922 | | |
924 | 923 | | |
925 | | - | |
| 924 | + | |
926 | 925 | | |
927 | 926 | | |
928 | 927 | | |
| |||
1306 | 1305 | | |
1307 | 1306 | | |
1308 | 1307 | | |
1309 | | - | |
| 1308 | + | |
1310 | 1309 | | |
1311 | 1310 | | |
1312 | 1311 | | |
| |||
1401 | 1400 | | |
1402 | 1401 | | |
1403 | 1402 | | |
1404 | | - | |
| 1403 | + | |
1405 | 1404 | | |
1406 | 1405 | | |
1407 | 1406 | | |
| |||
1426 | 1425 | | |
1427 | 1426 | | |
1428 | 1427 | | |
1429 | | - | |
| 1428 | + | |
1430 | 1429 | | |
1431 | 1430 | | |
1432 | 1431 | | |
| |||
1444 | 1443 | | |
1445 | 1444 | | |
1446 | 1445 | | |
1447 | | - | |
| 1446 | + | |
1448 | 1447 | | |
1449 | 1448 | | |
1450 | 1449 | | |
| |||
0 commit comments