[PR #2237] tests: parse the translator in pieces: 7 s -> 1 s per uncached import, 60 s -> 17 s in five standalone tests (TEST-TRANSLATOR-PIECEWISE-PARSE) #2241

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

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/2237
Author: @syfyra
Created: 11/26/2026
Status: 🔄 Open

Base: main ← Head: perf/translator-piecewise-parse


📝 Commits (4)

  • 21d1f0c tests: support.py compiles the translator from pieces, 7 s -> 1 s per uncached import (TEST-TRANSLATOR-PIECEWISE-PARSE)
  • e654d10 piecewise_parse: CPython 3.13.16 has the tokenizer fix too (TEST-TRANSLATOR-PIECEWISE-PARSE)
  • 53604ab tests: the five tests that parse the translator source do it in pieces, 60 s -> 17 s (TEST-TRANSLATOR-PIECEWISE-PARSE)
  • 1f92ae6 piecewise_parse: the barry test holds on CPython 3.12.1; parse() takes pieces only where needed (TEST-TRANSLATOR-PIECEWISE-PARSE)

📊 Changes

9 files changed (+1244 additions, -9 deletions)

View changed files

📝 tests/semantic/README.md (+13 -0)
➕ tests/semantic/piecewise_parse.py (+201 -0)
📝 tests/semantic/support.py (+43 -0)
📝 tests/test_cmake_translator_deps.py (+3 -1)
📝 tests/test_isa_coverage.py (+15 -4)
➕ tests/test_piecewise_parse.py (+958 -0)
📝 tests/test_resource_hygiene.py (+3 -1)
📝 tests/test_runtime_paths.py (+4 -1)
📝 tests/test_shader_stage_resume.py (+4 -2)

📄 Description

Claimed in the coordination issue as: TEST-TRANSLATOR-PIECEWISE-PARSE (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7188514735)

What and why

A test process that finds no valid .pyc of tools/rdna2spirv.py compiles the 1.2 MB file from source. On CPython 3.12, 3.13.0 to 3.13.15 and 3.14.0 to 3.14.7 that takes 7 s, 0.1 s of it code generation: the tokenizer copies the rest of its input at every f-string field (update_fstring_expr: strlen(tok->cur), PyMem_Malloc, strncpy), and import hands the file over as one string, so the cost grows with the square of the file (4,759 f-strings, about 12,700 fields). CPython fixed it in 3.13.16, 3.14.8 and 3.15.0 (gh-155525 in CPython; read from the release tags). 3.12, which every CI job pins, is security-only and keeps it: v3.12.16 still copies.

That compile happens after every edit of the translator (most test files set sys.dont_write_bytecode, so the stale .pyc is never replaced: 7.2 s on every run of one test file until something else rewrites it), for each of the 14 translator copies test_mutations.py runs, and always where PYTHONDONTWRITEBYTECODE is set.

tests/semantic/piecewise_parse.py parses a source in pieces of 300 lines and joins them into the tree a whole-file parse gives: tokenize (line by line, linear) finds where statements start, top-level statements are parsed in runs, a longer top-level class is cut between the statements of its body, and each piece is parsed with one newline in front for every line before it, so positions are the file's. Anything that does not fit (a piece that does not parse, barry_as_FLUFL, a NUL, a statement that does not start where tokenize said) returns None and the source is compiled the usual way. tests/semantic/support.py hands the modules of TOOLS_DIR to a SourceFileLoader subclass that compiles that tree, through a sys.meta_path finder placed just before PathFinder, and only on the interpreters above. A valid .pyc is still used first.

A .pyc does not help a test that reads the translator's syntax tree. tests/test_runtime_paths.py, tests/test_isa_coverage.py, tests/test_shader_stage_resume.py, tests/test_cmake_translator_deps.py and tests/test_resource_hygiene.py made seven whole-file ast.parse calls on it between them, in every job that runs the standalone tests (check, stdlib-tests-linux, macos-python). They now call piecewise_parse.parse, which takes pieces on the interpreters above and the whole-file parse elsewhere. tools/isa_coverage.py parses inside scan_translator and accepted_names; its test patches ast.parse around those calls on the real translator, and the tool is not edited (#1799 changes those lines).

The helper is in tests/semantic/ because it is test infrastructure that support.py imports as a sibling, like spirv_mini_interp.py; tools/ is what make_game.py runs and what test_mutations.py copies and sabotages, and the loader for TOOLS_DIR should not come out of TOOLS_DIR.

Not changed: files that import rdna2spirv before support or without it (247 of the 491 at 9cd0c3f42) still pay the 7 s; test_mutations.py keeps 13 in-process compile() calls on the whole translator text (its CRLF test), which is what is left of its time. scripts/run_semantic_tests.py, test_mutations.py and everything under tools/ are not edited. git merge-tree and git merge-file report no conflict with #2114, #1516, #2201, #1799, #565, #1361, #1372 or #1529.

One visible difference: when a module of tools/ has a syntax error, the SyntaxError (file, line, caret, message) is the same, but the traceback above it shows five importlib frames and support.py's source_to_code, which a plain import hides.

Tested

Windows 11, 8 logical CPUs, no GPU involved. The loader, measured at 9cd0c3f42 (before = that commit, after = the first commit of this branch on it):

before after
python -m unittest discover -s tests/semantic -p test_ps_semantics.py, Python 3.14.7 7.3 s 1.3 s
the same, Python 3.13.13 7.6 s 1.6 s
the same after the translator's mtime changed, bytecode writing allowed 7.2 s every run 1.2 s every run
tests/semantic/test_mutations.py alone 196.9 s 113.3 s
python -I scripts/run_semantic_tests.py tests/semantic (491 files, 6,312 tests, 0 failed, 8 jobs), bytecode writing allowed 246.4 s 161.3 s
the same with PYTHONDONTWRITEBYTECODE=1 597.6 s 396.7 s
-p "test_[bpv]*.py" on Python 3.13.13 (119 files, 1,892 tests, 0 failed) 171.6 s 88.5 s

The long ones are single runs on a machine other work shares. The branch has since been rebased onto a73a57e13: there one file takes 1.3 s again (1.3, 1.3, 1.4) and a plain import of the translator 7.4 s; the long runs were not repeated.

The five standalone tests, Python 3.14.7 at a73a57e13, with a valid .pyc of the translator as after CI's compileall step, two runs each (they agree within 0.3 s):

before after
python -I tests/test_runtime_paths.py (2 parses) 16.0 s 3.7 s
python -I tests/test_isa_coverage.py (2) 15.0 s 2.5 s
python -I tests/test_shader_stage_resume.py (1) 14.2 s 8.2 s
python -I tests/test_cmake_translator_deps.py (1) 7.6 s 1.4 s
python -I tests/test_resource_hygiene.py (1) 7.6 s 1.3 s
sum 60.4 s 17.1 s

They were found with a wrapper around compile that records every source over 500 kB, run over the 78 tests/test_*.py that mention the translator: nine such parses, these seven and two in tests/test_piecewise_parse.py itself, which needs the whole-file parse as its reference.

python -I tests/test_piecewise_parse.py: 26 tests, 46 s on 3.14.7 and 49 s on 3.13.13. It compares the trees for the 1,359 Python files under tools/, scripts/, tests/ and make_game.py in pieces of one statement (and the long files of tools/ in pieces of 300 lines; the translator in a test of its own), ast.dump(..., include_attributes=True) for 15 hand-written sources in pieces of 1, 2, 3, 5 and 8 lines with LF, CRLF and CR line ends, the exception for 28 sources with a syntax error and 9 the compiler rejects against compile's, the warnings, the code objects of 7 files and of the translator against compile(bytes), and the finder and loader on throwaway folders. It fails on main (the module does not exist).

Also run: python -m compileall -q on the changed files, python -I tests/test_text_write_line_endings.py, python -I tests/test_ci_registration.py, python -I tests/test_semantic_translator_use.py, python -I tests/test_run_semantic_tests.py (3.13.13, at 9cd0c3f42), python -I scripts/check_no_game_data.py on the changed files, and the five tests above on 3.13.13.

From the review (1f92ae6a5):

  • lixonik ran e654d10fd on CPython 3.12.1: 24 of the 25 tests passed and test_barry_as_flufl_is_parsed_whole errored. That parser does not read the barry_as_FLUFL import from the source (_PyPegen_checked_future_import is missing in v3.12.0, v3.12.1 and v3.13.0 and present in v3.12.16, v3.13.13 and v3.14.7), so the stock compile the test took as its reference rejects <>. The test now takes the running interpreter's compile outcome, tree or error, for both spellings. I have no 3.12.1: the new test passes and the old one fails with that parser's behaviour simulated.
  • GrahamboJangles measured the translator on a fixed interpreter (3.14.9: 0.21 s whole, 0.54 s in pieces), so parse() now takes pieces only where needed() says so, as support.py already did for the loader.

Not tested: Python 3.12 here (not installed; the 3.12.1 run above is the reviewer's, and CI's own run on 3.12 is the check), Linux and macOS, and an interpreter with the fix (3.13.16+, 3.14.8+) here.

GPUs tested: n/a

AI-assisted: yes (Claude Code)

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. (n/a: no translator rule)

🤖 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/2237 **Author:** [@syfyra](https://github.com/syfyra) **Created:** 11/26/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `perf/translator-piecewise-parse` --- ### 📝 Commits (4) - [`21d1f0c`](https://github.com/ubiquitarianism/leonida/commit/21d1f0ceb890f118e6a20614c0304283ab859a0a) tests: support.py compiles the translator from pieces, 7 s -> 1 s per uncached import (TEST-TRANSLATOR-PIECEWISE-PARSE) - [`e654d10`](https://github.com/ubiquitarianism/leonida/commit/e654d10fdf77072a388017b1872a8c517104b608) piecewise_parse: CPython 3.13.16 has the tokenizer fix too (TEST-TRANSLATOR-PIECEWISE-PARSE) - [`53604ab`](https://github.com/ubiquitarianism/leonida/commit/53604abbfcdab3a9203ddce7135ef88475788173) tests: the five tests that parse the translator source do it in pieces, 60 s -> 17 s (TEST-TRANSLATOR-PIECEWISE-PARSE) - [`1f92ae6`](https://github.com/ubiquitarianism/leonida/commit/1f92ae6a5d515db5b0a7b0641e3470833785d0ba) piecewise_parse: the barry test holds on CPython 3.12.1; parse() takes pieces only where needed (TEST-TRANSLATOR-PIECEWISE-PARSE) ### 📊 Changes **9 files changed** (+1244 additions, -9 deletions) <details> <summary>View changed files</summary> 📝 `tests/semantic/README.md` (+13 -0) ➕ `tests/semantic/piecewise_parse.py` (+201 -0) 📝 `tests/semantic/support.py` (+43 -0) 📝 `tests/test_cmake_translator_deps.py` (+3 -1) 📝 `tests/test_isa_coverage.py` (+15 -4) ➕ `tests/test_piecewise_parse.py` (+958 -0) 📝 `tests/test_resource_hygiene.py` (+3 -1) 📝 `tests/test_runtime_paths.py` (+4 -1) 📝 `tests/test_shader_stage_resume.py` (+4 -2) </details> ### 📄 Description Claimed in the coordination issue as: TEST-TRANSLATOR-PIECEWISE-PARSE (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7188514735) ### What and why A test process that finds no valid `.pyc` of `tools/rdna2spirv.py` compiles the 1.2 MB file from source. On CPython 3.12, 3.13.0 to 3.13.15 and 3.14.0 to 3.14.7 that takes 7 s, 0.1 s of it code generation: the tokenizer copies the rest of its input at every f-string field (`update_fstring_expr`: `strlen(tok->cur)`, `PyMem_Malloc`, `strncpy`), and `import` hands the file over as one string, so the cost grows with the square of the file (4,759 f-strings, about 12,700 fields). CPython fixed it in 3.13.16, 3.14.8 and 3.15.0 (gh-155525 in CPython; read from the release tags). 3.12, which every CI job pins, is security-only and keeps it: v3.12.16 still copies. That compile happens after every edit of the translator (most test files set `sys.dont_write_bytecode`, so the stale `.pyc` is never replaced: 7.2 s on every run of one test file until something else rewrites it), for each of the 14 translator copies `test_mutations.py` runs, and always where `PYTHONDONTWRITEBYTECODE` is set. `tests/semantic/piecewise_parse.py` parses a source in pieces of 300 lines and joins them into the tree a whole-file parse gives: `tokenize` (line by line, linear) finds where statements start, top-level statements are parsed in runs, a longer top-level class is cut between the statements of its body, and each piece is parsed with one newline in front for every line before it, so positions are the file's. Anything that does not fit (a piece that does not parse, `barry_as_FLUFL`, a NUL, a statement that does not start where `tokenize` said) returns None and the source is compiled the usual way. `tests/semantic/support.py` hands the modules of `TOOLS_DIR` to a `SourceFileLoader` subclass that compiles that tree, through a `sys.meta_path` finder placed just before `PathFinder`, and only on the interpreters above. A valid `.pyc` is still used first. A `.pyc` does not help a test that reads the translator's syntax tree. `tests/test_runtime_paths.py`, `tests/test_isa_coverage.py`, `tests/test_shader_stage_resume.py`, `tests/test_cmake_translator_deps.py` and `tests/test_resource_hygiene.py` made seven whole-file `ast.parse` calls on it between them, in every job that runs the standalone tests (`check`, `stdlib-tests-linux`, `macos-python`). They now call `piecewise_parse.parse`, which takes pieces on the interpreters above and the whole-file parse elsewhere. `tools/isa_coverage.py` parses inside `scan_translator` and `accepted_names`; its test patches `ast.parse` around those calls on the real translator, and the tool is not edited (#1799 changes those lines). The helper is in `tests/semantic/` because it is test infrastructure that `support.py` imports as a sibling, like `spirv_mini_interp.py`; `tools/` is what `make_game.py` runs and what `test_mutations.py` copies and sabotages, and the loader for `TOOLS_DIR` should not come out of `TOOLS_DIR`. Not changed: files that import `rdna2spirv` before `support` or without it (247 of the 491 at 9cd0c3f42) still pay the 7 s; `test_mutations.py` keeps 13 in-process `compile()` calls on the whole translator text (its CRLF test), which is what is left of its time. `scripts/run_semantic_tests.py`, `test_mutations.py` and everything under `tools/` are not edited. `git merge-tree` and `git merge-file` report no conflict with #2114, #1516, #2201, #1799, #565, #1361, #1372 or #1529. One visible difference: when a module of `tools/` has a syntax error, the SyntaxError (file, line, caret, message) is the same, but the traceback above it shows five importlib frames and `support.py`'s `source_to_code`, which a plain import hides. ### Tested Windows 11, 8 logical CPUs, no GPU involved. The loader, measured at 9cd0c3f42 (before = that commit, after = the first commit of this branch on it): | | before | after | | --- | --- | --- | | `python -m unittest discover -s tests/semantic -p test_ps_semantics.py`, Python 3.14.7 | 7.3 s | 1.3 s | | the same, Python 3.13.13 | 7.6 s | 1.6 s | | the same after the translator's mtime changed, bytecode writing allowed | 7.2 s every run | 1.2 s every run | | `tests/semantic/test_mutations.py` alone | 196.9 s | 113.3 s | | `python -I scripts/run_semantic_tests.py tests/semantic` (491 files, 6,312 tests, 0 failed, 8 jobs), bytecode writing allowed | 246.4 s | 161.3 s | | the same with `PYTHONDONTWRITEBYTECODE=1` | 597.6 s | 396.7 s | | `-p "test_[bpv]*.py"` on Python 3.13.13 (119 files, 1,892 tests, 0 failed) | 171.6 s | 88.5 s | The long ones are single runs on a machine other work shares. The branch has since been rebased onto a73a57e13: there one file takes 1.3 s again (1.3, 1.3, 1.4) and a plain import of the translator 7.4 s; the long runs were not repeated. The five standalone tests, Python 3.14.7 at a73a57e13, with a valid `.pyc` of the translator as after CI's `compileall` step, two runs each (they agree within 0.3 s): | | before | after | | --- | --- | --- | | `python -I tests/test_runtime_paths.py` (2 parses) | 16.0 s | 3.7 s | | `python -I tests/test_isa_coverage.py` (2) | 15.0 s | 2.5 s | | `python -I tests/test_shader_stage_resume.py` (1) | 14.2 s | 8.2 s | | `python -I tests/test_cmake_translator_deps.py` (1) | 7.6 s | 1.4 s | | `python -I tests/test_resource_hygiene.py` (1) | 7.6 s | 1.3 s | | sum | 60.4 s | 17.1 s | They were found with a wrapper around `compile` that records every source over 500 kB, run over the 78 `tests/test_*.py` that mention the translator: nine such parses, these seven and two in `tests/test_piecewise_parse.py` itself, which needs the whole-file parse as its reference. `python -I tests/test_piecewise_parse.py`: 26 tests, 46 s on 3.14.7 and 49 s on 3.13.13. It compares the trees for the 1,359 Python files under tools/, scripts/, tests/ and make_game.py in pieces of one statement (and the long files of tools/ in pieces of 300 lines; the translator in a test of its own), `ast.dump(..., include_attributes=True)` for 15 hand-written sources in pieces of 1, 2, 3, 5 and 8 lines with LF, CRLF and CR line ends, the exception for 28 sources with a syntax error and 9 the compiler rejects against `compile`'s, the warnings, the code objects of 7 files and of the translator against `compile(bytes)`, and the finder and loader on throwaway folders. It fails on main (the module does not exist). Also run: `python -m compileall -q` on the changed files, `python -I tests/test_text_write_line_endings.py`, `python -I tests/test_ci_registration.py`, `python -I tests/test_semantic_translator_use.py`, `python -I tests/test_run_semantic_tests.py` (3.13.13, at 9cd0c3f42), `python -I scripts/check_no_game_data.py` on the changed files, and the five tests above on 3.13.13. From the review (1f92ae6a5): - lixonik ran e654d10fd on CPython 3.12.1: 24 of the 25 tests passed and `test_barry_as_flufl_is_parsed_whole` errored. That parser does not read the `barry_as_FLUFL` import from the source (`_PyPegen_checked_future_import` is missing in v3.12.0, v3.12.1 and v3.13.0 and present in v3.12.16, v3.13.13 and v3.14.7), so the stock compile the test took as its reference rejects `<>`. The test now takes the running interpreter's compile outcome, tree or error, for both spellings. I have no 3.12.1: the new test passes and the old one fails with that parser's behaviour simulated. - GrahamboJangles measured the translator on a fixed interpreter (3.14.9: 0.21 s whole, 0.54 s in pieces), so `parse()` now takes pieces only where `needed()` says so, as `support.py` already did for the loader. Not tested: Python 3.12 here (not installed; the 3.12.1 run above is the reviewer's, and CI's own run on 3.12 is the check), Linux and macOS, and an interpreter with the fix (3.13.16+, 3.14.8+) here. GPUs tested: n/a AI-assisted: yes (Claude Code) ### 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. (n/a: no translator rule) 🤖 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#2241
No description provided.