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

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

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/55
Author: @Andretuta
Created: 11/24/2026
Status: 🔄 Open

Base: main ← Head: old-pr/9777


📝 Commits (3)

  • e606b54 tools: one cached work_root() instead of nine repeated realpath() calls per shader
  • e535c92 report_paths: resolve a relative work_root every time, never from the cache
  • ec340d9 tests: compare work_root with the resolved temporary folder, as the function returns it

📊 Changes

4 files changed (+65 additions, -10 deletions)

View changed files

📝 tests/test_report_paths.py (+28 -0)
📝 tools/rdna2spirv.py (+6 -6)
📝 tools/report_paths.py (+25 -0)
📝 tools/shader_meta.py (+6 -4)

📄 Description

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

🔄 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/55 **Author:** [@Andretuta](https://github.com/Andretuta) **Created:** 11/24/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `old-pr/9777` --- ### 📝 Commits (3) - [`e606b54`](https://github.com/ubiquitarianism/leonida/commit/e606b548cffecbf6a709132d4cc32ac5d4776f54) tools: one cached work_root() instead of nine repeated realpath() calls per shader - [`e535c92`](https://github.com/ubiquitarianism/leonida/commit/e535c92d2d020844571cd95a459ca7de2c56d497) report_paths: resolve a relative work_root every time, never from the cache - [`ec340d9`](https://github.com/ubiquitarianism/leonida/commit/ec340d9c1e1308700dd6b2a754279e2fac4df71f) tests: compare work_root with the resolved temporary folder, as the function returns it ### 📊 Changes **4 files changed** (+65 additions, -10 deletions) <details> <summary>View changed files</summary> 📝 `tests/test_report_paths.py` (+28 -0) 📝 `tools/rdna2spirv.py` (+6 -6) 📝 `tools/report_paths.py` (+25 -0) 📝 `tools/shader_meta.py` (+6 -4) </details> ### 📄 Description > 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.) --- <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#106
No description provided.