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

Closed
leonida/mandy
Andretuta wants to merge 0 commits from old-pr/9777 into main

Pull request closed

This pull request cannot be reopened because the branch was deleted.

Andretuta commented 2026-11-24 02:08:49 +00:00

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.)
> 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.)
aslan2142 reviewed 2026-12-01 11:15:20 +00:00
aslan2142 left a comment
No description provided.
Confirmed `report_paths.work_root()` has not landed on `main` yet, so this carried-over draft is not obsolete. However, it is still a draft, currently conflicts with `main`, and its CI runs were cancelled on carryover. To move forward, someone should claim it, rebase onto `main`, re-run CI (including `tests/test_report_paths.py`), and mark it ready for review. *(AI-assisted review.)*
aslan2142 commented 2026-12-01 12:05:23 +00:00

Marking Ready for Review since no WIP/blocker reason for draft status is specified on this PR, so it can enter normal CI and review.

(AI-assisted triage.)

Marking Ready for Review since no WIP/blocker reason for draft status is specified on this PR, so it can enter normal CI and review. *(AI-assisted triage.)*
aslan2142 reviewed 2026-12-01 12:56:51 +00:00
aslan2142 left a comment
No description provided.
Now ready for review, requesting changes before merge: - **Merge conflicts (`CONFLICTING`/`DIRTY`)**: conflicts with `main` across `tools/report_paths.py`, `tools/rdna2spirv.py`, and `tests/test_report_paths.py`. - **CI**: all checks from 2026-11-24 were cancelled. Please rebase onto current `main` and run fresh green CI. *(AI-assisted review.)*
aslan2142 commented 2026-12-01 17:19:47 +00:00

Closing this PR because the legacy C++ Vulkan runtime (and legacy Python shader compiler) is now considered deprecated in favor of the new Rust Vulkan runtime (in progress) and the Rust DX12 runtime (d3d12/) available now. Thank you for the contribution! If you believe this PR was closed by mistake (for example, if it still applies to active tools, d3d12/, or general docs), please feel free to reopen it.

Closing this PR because the legacy C++ Vulkan runtime (and legacy Python shader compiler) is now considered deprecated in favor of the new Rust Vulkan runtime (in progress) and the Rust DX12 runtime (`d3d12/`) available now. Thank you for the contribution! If you believe this PR was closed by mistake (for example, if it still applies to active tools, `d3d12/`, or general docs), please feel free to reopen it.
Sign in to join this conversation.
No description provided.