Repository navigation
Conversation
…EMANTIC-RUNNER-SHARED-BYTECODE)
Importing tools/rdna2spirv.py from source takes 7.2 s on Python 3.14.7 and
7.5 s on 3.13.13 (Windows 11); 0.1 s of that is code generation. Since 3.12
CPython's tokenizer copies the rest of a source string at every f-string `{`
(update_fstring_expr in 3.12's Parser/tokenizer.c), and the file has about
4,800 f-strings in 1.2 MB. Run as a script, where the tokenizer reads line
by line, it starts in 0.4 s.
Most files of tests/semantic set sys.dont_write_bytecode before they import
the translator, so a file compiles it itself unless a __pycache__ entry is
already there. Where bytecode may be written, 21 files that do not set the
flag write that entry early in a run. Where it may not
(PYTHONDONTWRITEBYTECODE, -B), every file compiled the translator.
The children of a parallel run now share what they compile through a
temporary folder of the run. A loader ahead of the path finder takes over
source_to_code, which the import system calls only when a module has no
valid bytecode file: it reads the entry another child wrote, or compiles
and writes it. An entry is named after the path and the bytes of its
source, so none is stale; one that cannot be read or written means
compiling, as before. Nothing is written into the tree, and the folder is
removed when the run ends.
Tested (Windows 11, 8 cores, Python 3.14.7, 8 jobs, 0 failures each): with
PYTHONDONTWRITEBYTECODE=1 the 478 files other than test_m*.py (6,245 tests)
took 533 s before and 93 s after, the whole suite (491 files, 6,312 tests)
577 s and 254 s. With bytecode allowed and no __pycache__ the same runs
took 93 s before and 98 s after, and 260 s and 253 s.
tests/test_run_semantic_tests.py (7 new tests) passes on 3.14.7 and 3.13.13.
Python 3.12 and Linux were not run.
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
|
Checked at
A data point on the size of the win: on this Linux machine with 3.14.8, AI-assisted (Claude Code). |
|
Independent Windows / CPython 3.12.10 check for
Synthetic fixtures only. GPUs tested: n/a. AI-assisted (OpenAI Codex). |
…(SEMANTIC-RUNNER-SHARED-BYTECODE) The comment and the README said "since 3.12", as if every later CPython still tokenized a source string in quadratic time. CPython fixed it in 3.13.16, 3.14.8 and 3.15.0 (gh-155525 in their Misc/NEWS.d; `strlen(tok->cur)` is gone from Parser/lexer/lexer.c at those tags and still there at v3.13.15 and v3.14.7). 3.12.16, the newest 3.12, still has it in Parser/tokenizer.c, and CI runs 3.12. Words only: the runner is unchanged. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…SLATOR-PIECEWISE-PARSE) v3.13.16 and v3.13.17 no longer copy the source at an f-string field (Parser/lexer/lexer.c keeps last_expr_start); v3.12.16 still does. needed() is now true on 3.12, 3.13.0 to 3.13.15 and 3.14.0 to 3.14.7. The README paragraph moves below the one about calling the translator, so it no longer touches the lines ubiquitarianism#2114 adds. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
… per run (SEMANTIC-RUNNER-SHARED-BYTECODE) The docstring said "compiled once per run". The jobs of a run start together, so each of them compiles the translator before the first entry is written and every file after those reads it. Counted on `-p 'test_scalar_*.py'` (16 files) with a marker file written beside each compile: 4 compiles with 4 jobs, 8 with 8. Pointed out in review of the pull request. Words only: the runner is unchanged. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
|
Thanks, both. The 3.14.8 number is the CPython version rather than the platform: the quadratic f-string tokenization was fixed in 3.13.16, 3.14.8 and 3.15.0 (gh-155525), so there is nothing left to save there, while 3.12, which CI runs, still has it in 3.12.16 (the 3.12.10 run above shows it). The description and the runner's comment said "since 3.12" without that end; 11df870 and the edited description correct it, and the description now points at both of your runs for the versions I could not run here. On "compiled once per run": right. The jobs of a run start together, so each compiles the translator before the first entry is written. Counted on the same slice ( AI-assisted (Claude Code). |
Claimed in the coordination issue as: SEMANTIC-RUNNER-SHARED-BYTECODE (#1608 (comment))
Depends on: none. It conflicts textually with eight open pull requests on
scripts/run_semantic_tests.pyand its test: #863, #1051, #1406, #1437, #1700, #1838, #2073 and #2201 (git merge-treeagainst each head on 2026-11-26, listed by thescripts/pr_overlap.pyof #2349). With #2073 it is the parameter both add torun_moduleand to itspool.submitcall. Whichever lands second needs a rebase.What and why
Importing
tools/rdna2spirv.pyfrom source takes 7.2 s on Python 3.14.7 and 7.5 s on 3.13.13 (Windows 11); 0.1 s of that is code generation. The rest is the tokenizer: from 3.12 up to 3.13.15 and 3.14.7 CPython copies the rest of a source string at every f-string{(update_fstring_exprin 3.12'sParser/tokenizer.c:strlen(tok->cur),PyMem_Malloc,strncpy), so parsing is quadratic in f-string fields, and the file has about 4,800 f-strings in 1.2 MB. CPython fixed that in 3.13.16, 3.14.8 and 3.15.0 (gh-155525 in theirMisc/NEWS.d; the copy is gone from the source at those tags); 3.12.16, the newest 3.12, still has it, and CI runs 3.12. So the gain is for interpreters without the fix; with it the change makes no measurable difference (see the 3.14.8 numbers under Tested). A plain class with 9,000 f-strings in 15,000 lines takes 4.8 s toast.parse; the same size without f-strings takes 0.09 s. Run as a script, where the tokenizer reads the file line by line,rdna2spirv.py --helpstarts in 0.4 s.Of the 491 files in
tests/semantic, only 21 import the translator with bytecode writing left on. The rest setsys.dont_write_bytecodefirst, themselves or throughsupport.py, or do not import it. So a file compiles the translator itself unless a__pycache__entry already exists:PYTHONDONTWRITEBYTECODE,-B: containers and agent shells often set it), every file compiled the translator.With this change the children of a parallel run share what they compile through a temporary folder of the run. A loader placed just ahead of the path finder takes over
source_to_code, which the import system calls only when a module has no valid bytecode file: it reads the entry another child wrote, or compiles and writes it. The jobs of a run start together, so each compiles the translator before the first entry is written and every file after those reads it: once per job instead of once per file (counted on-p 'test_scalar_*.py', 16 files: 4 compiles with 4 jobs, 8 with 8). An entry is named after the path and the bytes of its source, so none is ever stale (a__pycache__file is trusted on size and modification time alone). One that cannot be read or written means compiling, as before. Nothing is written into the tree, a file's ownsys.dont_write_bytecodestill keeps__pycache__out of it, and the folder is removed when the run ends.-j 1(one process) is unchanged.Tested
Windows 11, 8 cores, Python 3.14.7, 8 jobs, 0 failures in every run:
test_m*.py, 6,245 testsPYTHONDONTWRITEBYTECODE=1__pycache__PYTHONDONTWRITEBYTECODE=1__pycache__With bytecode allowed nothing measurable changes; the run only stops depending on those 21 files. The whole suite is bounded by
tests/semantic/test_mutations.py, which compiles a changed copy of the translator for every mutation and was the only file still running after 220 s (#1516 reworks it; it is not touched here).python -I tests/test_run_semantic_tests.py: 32 tests pass on 3.14.7 and on 3.13.13. The 7 new ones (SharedBytecodeTest) need the change: a second file runs the entry the first one wrote; a source changed to the same length and modification time is compiled again; a damaged entry or a missing folder falls back to compiling; a run shares one folder and removes it; a child is not given the folder of an outer run.python -I tests/test_text_write_line_endings.py,python -I tests/test_ci_registration.py,python -m compileall -qandpython -I scripts/check_no_game_data.pyon the changed files.-p 'test_scalar_*.py' -j 4(16 files, 445 tests) withPYTHONDONTWRITEBYTECODE=1: on 3.12.10 (Windows) it went from about 115 s to about 40 s; on 3.14.8 (Linux), which has the fix, compiling the translator takes 0.44 s and the slice took 15 s before and 16 s after.GPUs tested: n/a
AI-assisted: yes
Checklist
mainand merges cleanly (a conflicting pull request is labeledneeds rebase).docs/design.md, with how it was confirmed. (No translator rule in this change.)🤖 Generated with Claude Code