mirror of
https://github.com/ubiquitarianism/leonida.git
synced 2026-11-27 00:30:52 +01:00
[PR #2114] run_semantic_tests: the files of a run share the translator's bytecode (SEMANTIC-RUNNER-SHARED-BYTECODE) #2119
Labels
No labels
bug
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
starred/leonida#2119
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/ubiquitarianism/leonida/pull/2114
Author: @syfyra
Created: 11/25/2026
Status: 🔄 Open
Base:
main← Head:perf/semantic-bytecode-cache📝 Commits (3)
0205a16run_semantic_tests: the files of a run compile the translator once (SEMANTIC-RUNNER-SHARED-BYTECODE)11df870run_semantic_tests: say where CPython's f-string tokenizer copy ends (SEMANTIC-RUNNER-SHARED-BYTECODE)0285365run_semantic_tests: the translator is compiled once per job, not once per run (SEMANTIC-RUNNER-SHARED-BYTECODE)📊 Changes
3 files changed (+207 additions, -4 deletions)
View changed files
📝
scripts/run_semantic_tests.py(+86 -4)📝
tests/semantic/README.md(+13 -0)📝
tests/test_run_semantic_tests.py(+108 -0)📄 Description
Claimed in the coordination issue as: SEMANTIC-RUNNER-SHARED-BYTECODE (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7174783375)
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
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.