Skip to content

run_semantic_tests: the files of a run share the translator's bytecode (SEMANTIC-RUNNER-SHARED-BYTECODE) - #2114

Draft
syfyra wants to merge 3 commits into
ProjectLeonida:mainfrom
syfyra:perf/semantic-bytecode-cache
Draft

syfyra wants to merge 3 commits into
ProjectLeonida:mainfrom
syfyra:perf/semantic-bytecode-cache

Conversation

@syfyra

@syfyra syfyra commented Nov 25, 2026 •

Copy link
Copy Markdown

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.py and its test: #863, #1051, #1406, #1437, #1700, #1838, #2073 and #2201 (git merge-tree against each head on 2026-11-26, listed by the scripts/pr_overlap.py of #2349). With #2073 it is the parameter both add to run_module and to its pool.submit call. Whichever lands second needs a rebase.

What and why

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. 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_expr in 3.12's Parser/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 their Misc/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 to ast.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 --help starts in 0.4 s.

Of the 491 files in tests/semantic, only 21 import the translator with bytecode writing left on. The rest set sys.dont_write_bytecode first, themselves or through support.py, or do not import it. So a file compiles the translator itself unless a __pycache__ entry already exists:

  • where bytecode may be written, those 21 files write the entry early in a run and the rest read it;
  • where it may not (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 own sys.dont_write_bytecode still 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:

run environment before after
478 files other than test_m*.py, 6,245 tests PYTHONDONTWRITEBYTECODE=1 533 s 93 s
the same bytecode allowed, no __pycache__ 93 s 98 s
whole suite, 491 files, 6,312 tests PYTHONDONTWRITEBYTECODE=1 577 s 254 s
the same bytecode allowed, no __pycache__ 260 s 253 s

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 -q and python -I scripts/check_no_game_data.py on the changed files.
  • Not run by me: Python 3.12 (its tokenizer source was read, not timed), a Python with the tokenizer fix (none is installed here), and Linux or macOS. Two comments below report them for -p 'test_scalar_*.py' -j 4 (16 files, 445 tests) with PYTHONDONTWRITEBYTECODE=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

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (No translator rule in this change.)

🤖 Generated with Claude Code

…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>
@qhukz13

qhukz13 commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Checked at 0205a1642 on Linux (Python 3.14.8), which the description lists as not run.

  • tests/test_run_semantic_tests.py: OK here too.
  • The loader looks safe to me: the entry is keyed by path plus source bytes through importlib.util.source_hash (which also folds in the interpreter's magic number, so two Python versions never share an entry) and sys.flags.optimize; the write goes through a per-pid file and os.replace; a busy or damaged entry falls back to compiling (on Windows a replace over a file another child has open raises PermissionError, which the except OSError covers). Only a plain SourceFileLoader is replaced. test_mutations.py runs its mutants as python -m unittest subprocesses without this loader and only compile()s for a syntax check, so mutated copies do not pile up in the folder.
  • No leftover leonida-bytecode-* folder after a run.

A data point on the size of the win: on this Linux machine with 3.14.8, compile() of tools/rdna2spirv.py from source takes 0.44 s (bytes or str), not 7 s. A slice of the suite, -p 'test_scalar_*.py' -j 4 with PYTHONDONTWRITEBYTECODE=1 (16 files, 445 tests), took 15 s with main's runner and 16 s with this one. So the quadratic f-string cost does not show here; it may be specific to the Windows build (allocator) or to 3.14.7. The change is still harmless where it does not help, so no objection, but the description could say the gain is measured on Windows.

AI-assisted (Claude Code).

@promise-ssss

Copy link
Copy Markdown
Contributor

Independent Windows / CPython 3.12.10 check for 0205a1642 applied to main a73a57e13 (Windows 11 build 26300, CPU only).

  • python -I -B tests/test_run_semantic_tests.py — main: 26 run. Main + PR: 32 run. One skip in each, no failures.
  • PYTHONDONTWRITEBYTECODE=1 and python -I -B scripts/run_semantic_tests.py tests/semantic -p 'test_scalar_*.py' -j 4 --timeout 90 — 16 files and 445 tests with no failed files in all four runs. Wall time in run order: main 115.71 s, main + PR 39.37 s, main + PR 42.53 s, main 113.56 s. That is about 64% less elapsed time on this slice. Both temporary checkouts had zero normal __pycache__ directories.
  • In a two-worker synthetic cold-cache probe, both workers reached compilation for the same helper before either could publish the cache entry. It recorded two compilations and both tests passed. The "compiled once per run" wording in scripts/run_semantic_tests.py could say that workers reuse bytecode after the first cache entry is published.

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>
syfyra added a commit to syfyra/leonida that referenced this pull request Nov 26, 2026
…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>
@syfyra syfyra changed the title run_semantic_tests: the files of a run compile the translator once (SEMANTIC-RUNNER-SHARED-BYTECODE) run_semantic_tests: the files of a run share the translator's bytecode (SEMANTIC-RUNNER-SHARED-BYTECODE) Nov 26, 2026
@syfyra

syfyra commented Nov 26, 2026

Copy link
Copy Markdown
Author

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 (-p 'test_scalar_*.py', 16 files) with a marker file written beside each compile: 4 compiles with 4 jobs, 8 with 8. 0285365 says "once per job" in the docstring, the comment and the README, and the title no longer says "once".

AI-assisted (Claude 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.

3 participants