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

Open
opened 2026-11-26 21:20:06 +01:00 by 4ld0 · 0 comments
Owner

📋 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)

  • 0205a16 run_semantic_tests: the files of a run compile the translator once (SEMANTIC-RUNNER-SHARED-BYTECODE)
  • 11df870 run_semantic_tests: say where CPython's f-string tokenizer copy ends (SEMANTIC-RUNNER-SHARED-BYTECODE)
  • 0285365 run_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.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


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/2114 **Author:** [@syfyra](https://github.com/syfyra) **Created:** 11/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `perf/semantic-bytecode-cache` --- ### 📝 Commits (3) - [`0205a16`](https://github.com/ubiquitarianism/leonida/commit/0205a16426753ab0b39806acb67b2f0b85a89a71) run_semantic_tests: the files of a run compile the translator once (SEMANTIC-RUNNER-SHARED-BYTECODE) - [`11df870`](https://github.com/ubiquitarianism/leonida/commit/11df8700e0f5aa1966d319218307f97afebc52f1) run_semantic_tests: say where CPython's f-string tokenizer copy ends (SEMANTIC-RUNNER-SHARED-BYTECODE) - [`0285365`](https://github.com/ubiquitarianism/leonida/commit/0285365f0c87c7f3a4d104cd7c6d1a8f0d8a0c8e) run_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) <details> <summary>View changed files</summary> 📝 `scripts/run_semantic_tests.py` (+86 -4) 📝 `tests/semantic/README.md` (+13 -0) 📝 `tests/test_run_semantic_tests.py` (+108 -0) </details> ### 📄 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.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 - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] 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](https://claude.com/claude-code) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
starred/leonida#2119
No description provided.