perf(shader): borrow unpatched native code during IR construction #1240

Manually merged
github merged 1 commit from github-bot/gh-1240 into main 2026-11-29 08:21:47 +00:00 AGit
Owner

@⁠Damokles2 on GitHub · 2026-11-28 16:14 UTC · original
Damokles2:perf-shader-borrow-unpatched-code → main · +53 −9 · 2 files · 1 commits · merged 2026-11-29 08:16 UTC

Summary

NativeProgram::patched_code() eagerly clones the entire native shader byte buffer, even when there are no export words to replace. Its caller, spill::build(), passes the bytes directly to ir::build(), which accepts a borrowed &[u8].

The internal code_for_ir() helper returns a Cow<'_, [u8]>. It borrows the original code when there are no export words to replace and clones once when patching is required. The public patched_code() -> Vec<u8> API still returns owned bytes. The helper preserves export patch order and bounds checks, including the existing offset arithmetic for empty patches, and leaves the input program unchanged.

Validation

  • 1,014 differential cases passed (14 deterministic and 1,000 seeded), comparing exact code bytes, complete IR, repeated calls, input immutability, and existing error and panic behavior.
  • Native tests ran and passed, 12 on the baseline and 13 on the candidate, with zero failures. All 3 focused spill tests and all 87 asset-independent CPU tests passed on both revisions.
  • Locked workspace compilation passed on both revisions.
  • All 21 unpatched workloads eliminated one allocation and one full payload copy. All 14 patched workloads retained identical allocation and copy counts.

Representative unpatched compute code-preparation medians from the synthetic benchmark:

Code size Baseline Candidate
64 B 21.25 ns 6.00 ns
4 KiB 40.57 ns 6.15 ns
256 KiB 6.66 µs 8.33 ns

Some small patched workloads showed roughly 3–11 ns of helper-only overhead. No repeatable full-IR slowdown was observed. The timings above cover code preparation only.

Hosted Windows checks

  • Baseline source 9ce3cf9e340adb1cc8de3a681e0f1b8016d46b3e. Baseline run
  • Candidate source 0f5793399dfbe5ff87d1afce7351451c36881f19. Candidate run

Both jobs finished with identical pre-existing failures in strict default and all-features Clippy, the same 3,793 formatting diff hunks, and asset-policy rejection of the upstream bindings.ini file type.

Closes #1235.

AI-assisted with OpenAI Codex.

**@⁠Damokles2** on GitHub · 2026-11-28 16:14 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/1240) `Damokles2:perf-shader-borrow-unpatched-code` → `main` · +53 −9 · 2 files · 1 commits · merged 2026-11-29 08:16 UTC ## Summary `NativeProgram::patched_code()` eagerly clones the entire native shader byte buffer, even when there are no export words to replace. Its caller, `spill::build()`, passes the bytes directly to `ir::build()`, which accepts a borrowed `&[u8]`. The internal `code_for_ir()` helper returns a `Cow<'_, [u8]>`. It borrows the original code when there are no export words to replace and clones once when patching is required. The public `patched_code() -> Vec<u8>` API still returns owned bytes. The helper preserves export patch order and bounds checks, including the existing offset arithmetic for empty patches, and leaves the input program unchanged. ## Validation - 1,014 differential cases passed (14 deterministic and 1,000 seeded), comparing exact code bytes, complete IR, repeated calls, input immutability, and existing error and panic behavior. - Native tests ran and passed, 12 on the baseline and 13 on the candidate, with zero failures. All 3 focused spill tests and all 87 asset-independent CPU tests passed on both revisions. - Locked workspace compilation passed on both revisions. - All 21 unpatched workloads eliminated one allocation and one full payload copy. All 14 patched workloads retained identical allocation and copy counts. Representative unpatched compute code-preparation medians from the synthetic benchmark: | Code size | Baseline | Candidate | | --- | ---: | ---: | | 64 B | 21.25 ns | 6.00 ns | | 4 KiB | 40.57 ns | 6.15 ns | | 256 KiB | 6.66 µs | 8.33 ns | Some small patched workloads showed roughly 3–11 ns of helper-only overhead. No repeatable full-IR slowdown was observed. The timings above cover code preparation only. ## Hosted Windows checks - Baseline source `9ce3cf9e340adb1cc8de3a681e0f1b8016d46b3e`. [Baseline run](https://github.com/Damokles2/VIctory-NVIDIA/actions/runs/46162937313) - Candidate source `0f5793399dfbe5ff87d1afce7351451c36881f19`. [Candidate run](https://github.com/Damokles2/VIctory-NVIDIA/actions/runs/46162937381) Both jobs finished with identical pre-existing failures in strict default and all-features Clippy, the same 3,793 formatting diff hunks, and asset-policy rejection of the upstream `bindings.ini` file type. Closes #1235. AI-assisted with OpenAI Codex.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-28 16:15:31 +00:00
Author
Owner

@⁠Damokles2 on GitHub · 2026-11-28 16:23 UTC · original

The official CI failure matches the pre-existing Clippy diagnostics identified during baseline validation:

  • clippy::let_and_return in victory-shader/src/emit/mod.rs:2768:9
  • clippy::manual_is_multiple_of in victory-gpu/src/shadow.rs:1708:32

Neither file is modified by this PR.

Both hosted validation runs reproduced these exact diagnostics:

Both hosted runs passed locked workspace compilation and all applicable CPU, native shader, and spill tests. The candidate additionally passed all 1,014 differential cases and all 35 allocation-comparison workloads.

No candidate-specific failures were observed.

The production patch remains unchanged to keep the contribution focused.

**@⁠Damokles2** on GitHub · 2026-11-28 16:23 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/1240#issuecomment-7242204282) The official CI failure matches the pre-existing Clippy diagnostics identified during baseline validation: - `clippy::let_and_return` in `victory-shader/src/emit/mod.rs:2768:9` - `clippy::manual_is_multiple_of` in `victory-gpu/src/shadow.rs:1708:32` Neither file is modified by this PR. Both hosted validation runs reproduced these exact diagnostics: - [[Official CI failure](https://github.com/trustno1-coder/VIctory-NVIDIA/actions/runs/46164110978/job/141037724786)](https://github.com/trustno1-coder/VIctory-NVIDIA/actions/runs/46164110978/job/141037724786) - [[Unmodified upstream baseline](https://github.com/Damokles2/VIctory-NVIDIA/actions/runs/46162937313)](https://github.com/Damokles2/VIctory-NVIDIA/actions/runs/46162937313) - [[Patched candidate](https://github.com/Damokles2/VIctory-NVIDIA/actions/runs/46162937381)](https://github.com/Damokles2/VIctory-NVIDIA/actions/runs/46162937381) Both hosted runs passed locked workspace compilation and all applicable CPU, native shader, and spill tests. The candidate additionally passed all 1,014 differential cases and all 35 allocation-comparison workloads. No candidate-specific failures were observed. The production patch remains unchanged to keep the contribution focused.
github manually merged commit da36ecbe3e into main 2026-11-29 08:21:47 +00:00
This discussion has been locked. Commenting is limited to contributors.
No reviewers
No milestone
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
github/trustno1-coder--VIctory-NVIDIA!1240
No description provided.