A program pick reads and decodes every stage of a new program under the shader lock, even when the same bytes were decoded before #1609

Open
opened 2026-11-18 15:43:49 +00:00 by mooki · 1 comment
Member

Type: performance · Area: renderer, shaders, program pick · Part of: #1493

TL;DR

When the game makes a program object, the first draw that picks it reads each stage's control section and code from guest memory again, keys it and decodes the instructions, with the shaders' lock held. The decoded stages are kept by program serial, so the same bytes under a new serial are decoded again. The program's stage keys were already computed once at creation, without the decode, and the pick does not use them. We do not know what this costs on main, so we propose a counter first.

Breakdown

  • Who it affects: any player, at the moments many programs are made at once (an area load, a save load). It would show as a hitch on the replay thread, not as a lower steady rate.
  • What is wrong, by reading: codes_of reads, keys and decodes every stage of a new serial in full while shader_mu is held. The result is kept by serial, not by content. The creation-time read of the same stages is a second, separate pass.
  • What we propose: a counter of picks that read and decode stages (count, milliseconds, and how many serials repeat bytes seen before). If the milliseconds matter, a bounded cache of decoded stages by their bytes, with an exact byte compare before an entry is trusted, and a pick that takes the keys computed at creation and decodes only when the program must be translated.
  • What does not change: the translation, the program keys, the cache files.
  • What we do not claim: any cost. The only figure about it that we have seen is an outside audit's, from a fork.
  • How we would prove it: the counter in a game run first. If it is small the issue closes. If not, a fixture for the cache, then paired timings of the picks after a null pair.

Evidence

Code on forge/main at 430eef4f2:

  • gpu/vk/vk_shader.cpp:1044-1052: codes_of fills a serial's vector with read_code for each of its stages when the vector is empty.
  • :165-176: read_code reads the control section and the code area from guest memory, then calls parse_code, which takes the code key over both and decodes the instructions.
  • :1012: the store is a map codes "by program serial".
  • :2072-2073: program_pick takes shader_mu and calls codes_of while holding it. A comment at :2631 says shader_mu is held through whole translations.
  • :2635-2660 and :2680-2700: at creation, read_code_head reads the same control section and code area and computes the key and the flags the pick needs (scene_fit, header_only), but does not decode the instructions. program_created_read keeps the result by serial (created_codes) for the load-at-create path. The pick does not read from there.
  • Merged #1564 found 45.9 ms of "shader program handling" in a 74.9 ms renderer frame in one Market playtest. Its cause was speculative pipeline probes, not this decode. It shows the bucket can be large; it does not measure this work.

The fork's claim, not ours: an outside audit of a fork says program picks read and decode stage code for about 45 ms in a save's load-in frame (37 to 67 ms across sessions), that decoding a stage took about 48 microseconds and a cached hit about 1.4, and that the fork fixed it with a byte-keyed cache. These are figures from a comment in the fork's source on its own lab PC. We have not measured them on Longshot.

Proposed change

  1. Counters, in the program pick: picks that read stages, milliseconds in read_code and parse_code, bytes read, and the count of serials whose stage bytes equal an earlier serial's. Print once per 600 presents with the other renderer lines. This changes no behaviour.
  2. If the milliseconds are material: a bounded cache of decoded stages found by a quick hash of the bytes and accepted only after an exact byte compare, so two stages that share a quick hash cost a decode, never one another's code. An entry depends on the bytes alone.
  3. A pick whose program is already in the program cache takes the keys and flags from the creation-time read and decodes the instructions only when the program is translated.
  4. Only if the lock itself is the cost: do the guest read before taking shader_mu.

Step 3 changes what a pick must be able to tell without decoding. parse_code and read_code_head hold two copies of the same logic for scene_fit and header_only; they have to agree for the step to be safe, and that needs a careful look before anyone writes it.

How we would test it

  • Game-free: a fixture for the cache, written first, so it cannot pass on main. The same bytes under two serials decode once; different bytes with the same quick hash are kept apart; a bounded cache drops its oldest entry; a stage with a header only is still a header only. vk_shader.cpp cannot be linked in a test today, so there is no game-free test of today's codes_of to make red.
  • Game-free, for step 3: a fixture that feeds the same stage bytes through the creation-time read and the pick's read and compares the key and both flags. If they differ on any case, that is a bug found before the change.
  • In the game: the counter on a save load and on an area change, any GPU. Report the milliseconds per load and the share of serials that repeat bytes. A paired timing of the picks follows only if the first number is above a frame's noise, after a main-against-main pair.
  • No speed claim is made before that pair.
  • #1493 slice d (the pipeline and translation side) and slice e (the draw path).
  • #795 (open, work in progress): a draw keeps its program across texture registrations and uniform updates that do not touch it. Same path, a different cost.
  • #1564 and #1553 (merged): other Market costs in the same area.
  • #1509: what the shader stock holds after the start. A cache by bytes adds to that stock and has to be bounded.
  • The issue about the shader cache stamp (to be linked), on the same files.

New on this forge; not opened on GitHub.

**Type:** performance · **Area:** renderer, shaders, program pick · **Part of:** #1493 ## TL;DR When the game makes a program object, the first draw that picks it reads each stage's control section and code from guest memory again, keys it and decodes the instructions, with the shaders' lock held. The decoded stages are kept by program serial, so the same bytes under a new serial are decoded again. The program's stage keys were already computed once at creation, without the decode, and the pick does not use them. We do not know what this costs on main, so we propose a counter first. ## Breakdown - **Who it affects:** any player, at the moments many programs are made at once (an area load, a save load). It would show as a hitch on the replay thread, not as a lower steady rate. - **What is wrong, by reading:** `codes_of` reads, keys and decodes every stage of a new serial in full while `shader_mu` is held. The result is kept by serial, not by content. The creation-time read of the same stages is a second, separate pass. - **What we propose:** a counter of picks that read and decode stages (count, milliseconds, and how many serials repeat bytes seen before). If the milliseconds matter, a bounded cache of decoded stages by their bytes, with an exact byte compare before an entry is trusted, and a pick that takes the keys computed at creation and decodes only when the program must be translated. - **What does not change:** the translation, the program keys, the cache files. - **What we do not claim:** any cost. The only figure about it that we have seen is an outside audit's, from a fork. - **How we would prove it:** the counter in a game run first. If it is small the issue closes. If not, a fixture for the cache, then paired timings of the picks after a null pair. ## Evidence Code on `forge/main` at `430eef4f2`: - `gpu/vk/vk_shader.cpp:1044-1052`: `codes_of` fills a serial's vector with `read_code` for each of its stages when the vector is empty. - `:165-176`: `read_code` reads the control section and the code area from guest memory, then calls `parse_code`, which takes the code key over both and decodes the instructions. - `:1012`: the store is a map `codes` "by program serial". - `:2072-2073`: `program_pick` takes `shader_mu` and calls `codes_of` while holding it. A comment at `:2631` says `shader_mu` is held through whole translations. - `:2635-2660` and `:2680-2700`: at creation, `read_code_head` reads the same control section and code area and computes the key and the flags the pick needs (`scene_fit`, `header_only`), but does not decode the instructions. `program_created_read` keeps the result by serial (`created_codes`) for the load-at-create path. The pick does not read from there. - Merged #1564 found 45.9 ms of "shader program handling" in a 74.9 ms renderer frame in one Market playtest. Its cause was speculative pipeline probes, not this decode. It shows the bucket can be large; it does not measure this work. The fork's claim, not ours: an outside audit of a fork says program picks read and decode stage code for about 45 ms in a save's load-in frame (37 to 67 ms across sessions), that decoding a stage took about 48 microseconds and a cached hit about 1.4, and that the fork fixed it with a byte-keyed cache. These are figures from a comment in the fork's source on its own lab PC. We have not measured them on Longshot. ## Proposed change 1. Counters, in the program pick: picks that read stages, milliseconds in `read_code` and `parse_code`, bytes read, and the count of serials whose stage bytes equal an earlier serial's. Print once per 600 presents with the other renderer lines. This changes no behaviour. 2. If the milliseconds are material: a bounded cache of decoded stages found by a quick hash of the bytes and accepted only after an exact byte compare, so two stages that share a quick hash cost a decode, never one another's code. An entry depends on the bytes alone. 3. A pick whose program is already in the program cache takes the keys and flags from the creation-time read and decodes the instructions only when the program is translated. 4. Only if the lock itself is the cost: do the guest read before taking `shader_mu`. Step 3 changes what a pick must be able to tell without decoding. `parse_code` and `read_code_head` hold two copies of the same logic for `scene_fit` and `header_only`; they have to agree for the step to be safe, and that needs a careful look before anyone writes it. ## How we would test it - Game-free: a fixture for the cache, written first, so it cannot pass on main. The same bytes under two serials decode once; different bytes with the same quick hash are kept apart; a bounded cache drops its oldest entry; a stage with a header only is still a header only. `vk_shader.cpp` cannot be linked in a test today, so there is no game-free test of today's `codes_of` to make red. - Game-free, for step 3: a fixture that feeds the same stage bytes through the creation-time read and the pick's read and compares the key and both flags. If they differ on any case, that is a bug found before the change. - In the game: the counter on a save load and on an area change, any GPU. Report the milliseconds per load and the share of serials that repeat bytes. A paired timing of the picks follows only if the first number is above a frame's noise, after a main-against-main pair. - No speed claim is made before that pair. ## Related - #1493 slice d (the pipeline and translation side) and slice e (the draw path). - #795 (open, work in progress): a draw keeps its program across texture registrations and uniform updates that do not touch it. Same path, a different cost. - #1564 and #1553 (merged): other Market costs in the same area. - #1509: what the shader stock holds after the start. A cache by bytes adds to that stock and has to be bounded. - The issue about the shader cache stamp (to be linked), on the same files. New on this forge; not opened on GitHub.
Collaborator

Possibly related, for context:

  • issue #1493 Renderer rewrite — Renderer rewrite initiative; this is a focused renderer performance measurement/change proposal.
  • issue #1509 Shader programs, modules and pipelines are never released while the game runs, and the rule that would release a program has no caller — Related shader-program lifetime and resident-stock concern; proposed decoded-stage cache would need bounded memory.
  • issue #1611 The shader cache stamp hashes the translator's source byte for byte, so a comment edit makes every player prepare all shaders again — Touches same shader source file and explicitly calls out this program-pick issue as related.
Possibly related, for context: - issue #1493 Renderer rewrite — Renderer rewrite initiative; this is a focused renderer performance measurement/change proposal. - issue #1509 Shader programs, modules and pipelines are never released while the game runs, and the rule that would release a program has no caller — Related shader-program lifetime and resident-stock concern; proposed decoded-stage cache would need bounded memory. - issue #1611 The shader cache stamp hashes the translator's source byte for byte, so a comment edit makes every player prepare all shaders again — Touches same shader source file and explicitly calls out this program-pick issue as related.
Sign in to join this conversation.
No milestone
No project
No assignees
2 participants
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
MagicBeanDev/Longshot#1609
No description provided.