tools: one cached work_root() per shader instead of nine realpath() calls (-14% fixed cost, byte-identical) #55

Open
github-bot wants to merge 3 commits from github-bot/gh-55 into main AGit
Owner

@⁠Andretuta on GitHub · 2026-11-24 02:08 UTC · original
ubiquitarianism:old-pr/9777 → main · +65 −10 · 4 files · 3 commits · draft

Carried over from old PR 9777 by pes7 in the original Leonida repository, which went private on 23 Nov 2026. The code is the branch old-pr/9777, exactly that PR's last commit. All carried-over PRs are listed in #17.
Opened as a draft, and the CI runs the opening started were cancelled so 250 PRs don't fill the queue. Re-run CI here when you review it.
pes7: this is your work. Feel free to take it over, by pushing to the branch or opening your own PR and closing this one.


Original description (mentions and #N references are disabled; the numbers refer to the old repository):

Every shader of the build pays 0.8 ms of path resolution that nothing needs. A build translates 26,503 shaders; this takes ~14% off the fixed cost of each one, with byte-identical output.

Nine places derive a shader's work folder with the same expression — five in rdna2spirv.py, four in shader_meta.py:

os.path.dirname(os.path.dirname(os.path.realpath(path)))

They run for index.json (which gives the stage), the .dxbc input signature and the fssm header, so the same directory is resolved up to sixteen times per shader, and os.path.realpath is a _getfinalpathname call on Windows (~40 µs measured).

What changes

report_paths.work_root() — that module already owns the path rules — resolves the folder once per path and keeps it, with a capped table (1<<14) and a clear at the cap, so a process that walks a whole corpus cannot grow it. A relative path is resolved every time and never cached: the same spelling names a different folder after a chdir (found in review by @senyurah, fixed, with a regression test).

Measured

scripts/bench_translate.py --compare (#7056), which alternates the two tools/ trees in the same round, drops a warm-up round and keeps the best of 3 per side; 5 rounds, Windows 11, Python 3.12.4:

measurement before after ratio
corpus: 52 semantic fixtures 63.2 µs/instr 54.6 µs/instr 0.86x
synthetic ALU, 2,004 instructions 42.4 42.0 0.99x
synthetic s_nop, 2,004 17.7 17.5 0.99x

The same-tree control of that harness reads 1.01x / 1.00x / 0.99x, so 0.86x is outside its noise.

Correct because

  • All 52 pixel and vertex fixtures translate to the same SPIR-V assembly text: SHA-256 over name + output is 157189dcc0158f00… before and after.
  • scripts/run_semantic_tests.py tests/semantic: 5,821 tests, the same single pre-existing red file as main (test_float_nan_fract).
  • Independent runs by reviewers on Steam Deck (RADV), RX 6700 XT (RDNA2), RX 5700 XT (RDNA1) and RTX 5070 (Blackwell): pass, results identical to main.
  • tests/test_report_paths.py: 24 tests, five new ones for this (including the chdir case).

cc @ItsNotRexed @KoertLichtendonk

Checklist

  • 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. (Not applicable: no rule changes; the emitted SPIR-V is byte-identical.)
**@⁠Andretuta** on GitHub · 2026-11-24 02:08 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/55) `ubiquitarianism:old-pr/9777` → `main` · +65 −10 · 4 files · 3 commits · draft > Carried over from **old PR 9777** by `pes7` in the original Leonida repository, which went private on 23 Nov 2026. The code is the branch `old-pr/9777`, exactly that PR's last commit. All carried-over PRs are listed in #17. > Opened as a draft, and the CI runs the opening started were cancelled so 250 PRs don't fill the queue. Re-run CI here when you review it. > `pes7`: this is your work. Feel free to take it over, by pushing to the branch or opening your own PR and closing this one. --- Original description (mentions and `#N` references are disabled; the numbers refer to the old repository): **Every shader of the build pays 0.8 ms of path resolution that nothing needs.** A build translates 26,503 shaders; this takes ~14% off the fixed cost of each one, with byte-identical output. Nine places derive a shader's work folder with the same expression — five in `rdna2spirv.py`, four in `shader_meta.py`: ```python os.path.dirname(os.path.dirname(os.path.realpath(path))) ``` They run for `index.json` (which gives the stage), the `.dxbc` input signature and the `fssm` header, so the same directory is resolved **up to sixteen times per shader**, and `os.path.realpath` is a `_getfinalpathname` call on Windows (~40 µs measured). ### What changes `report_paths.work_root()` — that module already owns the path rules — resolves the folder once per path and keeps it, with a capped table (1<<14) and a clear at the cap, so a process that walks a whole corpus cannot grow it. A **relative** path is resolved every time and never cached: the same spelling names a different folder after a `chdir` (found in review by `@senyurah`, fixed, with a regression test). ### Measured `scripts/bench_translate.py --compare` (`#7056`), which alternates the two `tools/` trees in the same round, drops a warm-up round and keeps the best of 3 per side; 5 rounds, Windows 11, Python 3.12.4: | measurement | before | after | ratio | |---|---|---|---| | corpus: 52 semantic fixtures | 63.2 µs/instr | **54.6 µs/instr** | **0.86x** | | synthetic ALU, 2,004 instructions | 42.4 | 42.0 | 0.99x | | synthetic `s_nop`, 2,004 | 17.7 | 17.5 | 0.99x | The same-tree control of that harness reads 1.01x / 1.00x / 0.99x, so 0.86x is outside its noise. ### Correct because - All 52 pixel and vertex fixtures translate to **the same SPIR-V assembly text**: SHA-256 over name + output is `157189dcc0158f00…` before and after. - `scripts/run_semantic_tests.py tests/semantic`: 5,821 tests, the same single pre-existing red file as `main` (`test_float_nan_fract`). - Independent runs by reviewers on **Steam Deck (RADV), RX 6700 XT (RDNA2), RX 5700 XT (RDNA1) and RTX 5070 (Blackwell)**: pass, results identical to `main`. - `tests/test_report_paths.py`: 24 tests, five new ones for this (including the `chdir` case). cc `@ItsNotRexed` `@KoertLichtendonk` ### Checklist - [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`. (Not applicable: no rule changes; the emitted SPIR-V is byte-identical.)
Everything that reads index.json, the .dxbc input signatures or the fssm headers derives the shader's work folder
with os.path.dirname(os.path.dirname(os.path.realpath(path))) - nine copies of that expression across rdna2spirv.py
and shader_meta.py, asked up to sixteen times per shader, and os.path.realpath is a _getfinalpathname call on Windows
(~40 us). It is the same string every time, so report_paths.work_root() resolves it once per path and keeps it, with a
1<<14 cap for a process that walks a corpus.

Measured with scripts/bench_translate.py --compare (#7056), 5 rounds, best of 3, same machine: the 52-fixture corpus
goes 63.2 -> 54.6 us/instruction (0.86x), ALU and s_nop unchanged (0.99x). The 52 pixel and vertex fixtures still
translate to byte-identical SPIR-V assembly (sha256 157189dc...).
A relative path names a different folder after a chdir, so a cache keyed by the spelling alone answered the second
call with the first working directory's root (reported by @senyurah on #7335, with a two-work-folder repro through
shader_meta.fspx_code_sha256_file). Absolute paths, which is what the translator passes, are still resolved once;
relative ones are now resolved on every call, exactly as before the cache.

tests/test_report_paths.py: two new tests, one for the chdir sequence, one that an absolute path is resolved once.
tempfile hands back the 8.3 spelling on this machine (NAZARP~1) while realpath, which work_root uses, gives the long
one; the assertion now resolves both sides the same way.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 08:42:31 +00:00
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/55/head:github-bot/gh-55'
git switch 'github-bot/gh-55'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-55'
git switch 'github-bot/gh-55'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-55'
git switch 'github-bot/gh-55'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-55'
git switch 'main'
git merge --squash 'github-bot/gh-55'
git switch 'main'
git merge --ff-only 'github-bot/gh-55'
git switch 'main'
git merge 'github-bot/gh-55'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.