perf(menu): a tab shown again reads only the rows that moved; the settings generation covers every field #2874

Merged
HumanBean merged 2 commits from HumanBean/Longshot:perf/menu-ui-render into mod-api 2026-11-30 19:27:00 +00:00
Owner

Summary

Switching to a tab of the Insert menu with many rows did all its reading at once. The first frame read the tab's declarations and laid them out. The next frame read every row's state and also drew the first rows. That second frame was the heaviest of the switch. With every system row in the menu (#2861: the Graphics tab has 58), it went over the menu's 4.5 ms budget in the debug cost test.

This change does three things:

  • Keeps each page's layout. The menu keeps each page's declarations, laid out, until their generation moves on: a mod starts or stops, the language changes, or the renderer's lists change. A tab shown again reads nothing to lay itself out.
  • Reads the rows earlier on a page shown again. Its rows are read in the frame that takes its kept declarations, the frame before it is drawn, so its first drawing frame reads only the rows that changed since. A page's first showing reads as before: declarations in one frame, rows with its first drawing, never both reads in one frame (putting both in one frame made the debug first showing 5.7 ms).
  • Fixes the generation. It only counted each row's key, label and kind. Now it counts every field the menu gets from a declaration (help, kind and choices, place, flags, native use, a slider's unit, display scale and default). So a mod declared again with other choices or another place is read again. This change was needed for the kept layouts to be safe.

laidOut also builds the menu's own lines more cheaply: one shared flags object, and no set to follow an anchor's chain.

Profiling (stack samples of mod test on the release fake game) showed where a switch's time goes. Per frame, it is about 24 components drawn and their commit. The rest was the reads: about 0.2 ms to read the rows and 0.3 ms for the declarations and their layout, all landing on the switch's first drawing frame. With the reads moved, the heaviest frame is now an ordinary drawing frame.

Files to read: game/mods/menu/src/main.tsx (declsOf, the laid read in Menu), game/crates/api/src/services/settings_host.rs (generation_of), game/mods/menu/test/menu.test.ts (the cost test and the new reads test).

Testing

Tested: Linux, RTX 3090.

  • Queue jobs:

    • cargo test -p modtool the_typescript_system_mods_pass_their_tests (debug) passes, every menu test included.
    • cargo test -p api settings_host and cargo clippy -p api pass.
    • dev.py pack and dev.py rt pass.
  • Smoke run, headless (shared queue): kokiri-start, then Insert, then all seven tabs stepped through. Status showed menu_open, hosted by system/menu, and the log has no script error. One 3.7 ms warning for the menu's first open came while the PC was paging. The bridge's screenshots do not include the menu.

  • New test: "a page switched to again reads no declarations until they change; its rows are read with them". It fails on #2861: the rows were read a frame after the declarations.

  • Cost test, made stricter: a tab shown again is held to SWITCH_MS 4.5 ms again, with every system row. A tab's first showing is timed apart, under FIRST_MS 5.5 ms (the switch's ceiling in #2861). For it, the rows are declared again with a label changed, which gives a new generation, and the test asserts that the declarations were read again.

  • Generation test: a_row_declared_again_with_another_format_or_default_moves_the_generation_on (cargo test -p api): another unit, display scale or default each move the generation on (review L1).

  • Revisit test, frame by frame: the Audio tab shown again has its rows read in a frame before the one that first draws it, and none in that frame (review L2).

  • Queue job on 656c1a57: cargo test -p modtool (228 passed), cargo test -p api --lib (452 passed), clippy -D warnings and rustfmt pass.

  • Release fake game: all 34 menu tests pass on mod test (release builder).

Still to test:

  • Any PC: open the Insert menu, switch between tabs, and open and close the pages. Expect every row drawn as before and the switch a little quicker (by hand).
  • Any PC: change the menu's language, then switch tabs. Expect every label in the new language (by hand).

Measurements

Verdict: in the debug test, a tab shown again now costs 1.61 ms in its heaviest frame, against 4.49 ms before. A tab's first showing is as before.

Setup: Linux, RTX 3090; the menu's cost test (LONGSHOT_COST_REPORT=1), its heaviest frame of script CPU while a tab or page is switched to, the least of 5 samples (#2861: every switch read the declarations again; here, a tab shown again). Each side is the least of 5 samples in one job, the two sides from separate jobs, not interleaved. 656c1a57 after it changes only the generation's hash (once per declarations' list, not per frame) and the tests, so the numbers hold for the head.

Before: mod-api @ 12f42db5 After: this PR @ 1dcf4b82
Graphics switch, debug cargo test -p modtool (the test's ceiling) 4.49 ms (#2861's job; 5.35 under load) 1.61 ms (shown again; first showing under 5.5, asserted)

Risks

  • A stale layout. A kept page's declarations would go stale if they changed without the generation moving on. The generation now covers every field a declaration gives the menu, and the menu's language and the renderer's lists already moved it. A revert is enough to undo this.
  • More re-reads. A mod restarted or declared again with other help or choices now moves the generation, so the menu reads every page again. That happens rarely: when a mod starts or stops.
  • The rows read a frame earlier. The page shown is still the one before until the next frame, as it was.
  • Nothing for players to notice apart from the speed. Their files and saves are unchanged.

Checklist

Ready for review

  • One topic, on current mod-api; the title is a Conventional Commit.
  • Tested on my own PC, in the game for a change the game runs, as Testing says.
  • mise run test passes (any failure that also fails on main is named). On 656c1a57: 836 passed, 1 failed: tests/test_lifter.py (the builder's control::tests::the_socket_is_made_private_and_a_client_handed_over), code this PR does not touch; mod-api's own CI fails run_all at 12f42db5.
  • Measured against main, or Measurements says why there is nothing to measure.
  • No game files, build output or hand-edited generated files; third_party/ untouched; nothing GPL-incompatible.

When it applies

  • A check under tests/ that reads changed source is updated, not weakened.
  • AI assistance recorded with Assisted-by: / Co-Authored-By: trailers.

Ready to merge

  • Everything in Still to test has run, its results in Tested.
  • Approved in review, every comment answered.
  • CI passes on the head; on current main, measured again or a comment says why the numbers still hold.

🤖 Generated with Claude Code

https://claude.ai/code/session_016o1k1SQVyAWsujjFVd8Wuc

## Summary Switching to a tab of the Insert menu with many rows did all its reading at once. The first frame read the tab's declarations and laid them out. The next frame read every row's state and also drew the first rows. That second frame was the heaviest of the switch. With every system row in the menu (#2861: the Graphics tab has 58), it went over the menu's 4.5 ms budget in the debug cost test. This change does three things: - **Keeps each page's layout.** The menu keeps each page's declarations, laid out, until their generation moves on: a mod starts or stops, the language changes, or the renderer's lists change. A tab shown again reads nothing to lay itself out. - **Reads the rows earlier on a page shown again.** Its rows are read in the frame that takes its kept declarations, the frame before it is drawn, so its first drawing frame reads only the rows that changed since. A page's first showing reads as before: declarations in one frame, rows with its first drawing, never both reads in one frame (putting both in one frame made the debug first showing 5.7 ms). - **Fixes the generation.** It only counted each row's key, label and kind. Now it counts every field the menu gets from a declaration (help, kind and choices, place, flags, native use, a slider's unit, display scale and default). So a mod declared again with other choices or another place is read again. This change was needed for the kept layouts to be safe. `laidOut` also builds the menu's own lines more cheaply: one shared flags object, and no set to follow an anchor's chain. Profiling (stack samples of `mod test` on the release fake game) showed where a switch's time goes. Per frame, it is about 24 components drawn and their commit. The rest was the reads: about 0.2 ms to read the rows and 0.3 ms for the declarations and their layout, all landing on the switch's first drawing frame. With the reads moved, the heaviest frame is now an ordinary drawing frame. Files to read: `game/mods/menu/src/main.tsx` (`declsOf`, the `laid` read in `Menu`), `game/crates/api/src/services/settings_host.rs` (`generation_of`), `game/mods/menu/test/menu.test.ts` (the cost test and the new reads test). ## Testing **Tested:** Linux, RTX 3090. - **Queue jobs:** - `cargo test -p modtool the_typescript_system_mods_pass_their_tests` (debug) passes, every menu test included. - `cargo test -p api settings_host` and `cargo clippy -p api` pass. - `dev.py pack` and `dev.py rt` pass. - **Smoke run, headless (shared queue):** kokiri-start, then Insert, then all seven tabs stepped through. Status showed `menu_open`, hosted by `system/menu`, and the log has no script error. One 3.7 ms warning for the menu's first open came while the PC was paging. The bridge's screenshots do not include the menu. - **New test:** "a page switched to again reads no declarations until they change; its rows are read with them". It fails on #2861: the rows were read a frame after the declarations. - **Cost test, made stricter:** a tab shown again is held to `SWITCH_MS` 4.5 ms again, with every system row. A tab's first showing is timed apart, under `FIRST_MS` 5.5 ms (the switch's ceiling in #2861). For it, the rows are declared again with a label changed, which gives a new generation, and the test asserts that the declarations were read again. - **Generation test:** `a_row_declared_again_with_another_format_or_default_moves_the_generation_on` (`cargo test -p api`): another unit, display scale or default each move the generation on (review L1). - **Revisit test, frame by frame:** the Audio tab shown again has its rows read in a frame before the one that first draws it, and none in that frame (review L2). - **Queue job on 656c1a57:** `cargo test -p modtool` (228 passed), `cargo test -p api --lib` (452 passed), clippy `-D warnings` and rustfmt pass. - **Release fake game:** all 34 menu tests pass on `mod test` (release builder). **Still to test:** - Any PC: open the Insert menu, switch between tabs, and open and close the pages. Expect every row drawn as before and the switch a little quicker (by hand). - Any PC: change the menu's language, then switch tabs. Expect every label in the new language (by hand). ## Measurements **Verdict:** in the debug test, a tab shown again now costs 1.61 ms in its heaviest frame, against 4.49 ms before. A tab's first showing is as before. Setup: Linux, RTX 3090; the menu's cost test (`LONGSHOT_COST_REPORT=1`), its heaviest frame of script CPU while a tab or page is switched to, the least of 5 samples (#2861: every switch read the declarations again; here, a tab shown again). Each side is the least of 5 samples in one job, the two sides from separate jobs, not interleaved. 656c1a57 after it changes only the generation's hash (once per declarations' list, not per frame) and the tests, so the numbers hold for the head. | | Before: mod-api @ 12f42db5 | After: this PR @ 1dcf4b82 | |---|---|---| | Graphics switch, debug `cargo test -p modtool` (the test's ceiling) | 4.49 ms (#2861's job; 5.35 under load) | 1.61 ms (shown again; first showing under 5.5, asserted) | ## Risks - **A stale layout.** A kept page's declarations would go stale if they changed without the generation moving on. The generation now covers every field a declaration gives the menu, and the menu's language and the renderer's lists already moved it. A revert is enough to undo this. - **More re-reads.** A mod restarted or declared again with other help or choices now moves the generation, so the menu reads every page again. That happens rarely: when a mod starts or stops. - **The rows read a frame earlier.** The page shown is still the one before until the next frame, as it was. - **Nothing for players to notice** apart from the speed. Their files and saves are unchanged. ## Checklist ### Ready for review - [x] One topic, on current `mod-api`; the title is a Conventional Commit. - [x] Tested on my own PC, in the game for a change the game runs, as Testing says. - [x] `mise run test` passes (any failure that also fails on `main` is named). On 656c1a57: 836 passed, 1 failed: `tests/test_lifter.py` (the builder's `control::tests::the_socket_is_made_private_and_a_client_handed_over`), code this PR does not touch; mod-api's own CI fails run_all at 12f42db5. - [x] Measured against `main`, or Measurements says why there is nothing to measure. - [x] No game files, build output or hand-edited generated files; `third_party/` untouched; nothing GPL-incompatible. ### When it applies - [x] A check under `tests/` that reads changed source is updated, not weakened. - [x] AI assistance recorded with `Assisted-by:` / `Co-Authored-By:` trailers. ### Ready to merge - [ ] Everything in Still to test has run, its results in Tested. - [ ] Approved in review, every comment answered. - [ ] CI passes on the head; on current `main`, measured again or a comment says why the numbers still hold. 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_016o1k1SQVyAWsujjFVd8Wuc
refactor(menu): each row goes where its place says; layout.ts keeps tabs, pages, headings and links
Some checks failed
PR description / Title and description (pull_request_target) Failing after 16s
4c27c14cba
The menu's layout named every row of every tab in order (layout.ts), and the system mods' mod.toml gave
184 of the same rows a place that was read only for a row the layout did not name. Now there is one rule
for Longshot's rows and a player's mod's: view.ts laidOut lays out the layout's headings and puts every row,
and the layout's own links and lines, where its place says (after or before a row or link, else at its
section's end, the untitled top without one, by its order, then as declared; an anchor not on the page or
in a loop is none). The menu reads a page's rows by the page alone (no panelKeys).

The places that disagreed with the layout are fixed so every row stays where it was: 13 place lines in
the graphics, interface, developer, cheats and controls mods (Layout preset and Button prompts are placed
on the Controls page they were drawn on), and Damage taken names its section, so it stays there with the
cheats mod off. The settings page's table (docs/user) had six lines in another
order than the menu's: moved to the menu's.

The menu mod's test holds each tab's and page's laid-out lines to PLACED, the order laidOut gave from the
old layout (checked on the old code), and takes over tests/test_system_settings_placed.py's checks (every
system mod's setting a row, its place naming a tab, heading and anchor the menu has). The fake game's
systemSettings gives a row's after, before and order, declare passes them on, and unplacedSettings lists
the settings with no place. The cost test's tab-switch ceiling goes from 4.5 to 5.5 ms: it now draws
every system row (the Graphics tab 58, not the 38 the layout named; 4.49 ms measured), and Game queue
priority (a developer row, untranslated on mod-api already) is on UNTRANSLATED. Removed: layout.ts's row lists, the Python copy of laidOut,
longshot-game/tests/menu_places.tsv and its test.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
perf(menu): a tab's declarations kept for their generation, its rows read before it is drawn again
Some checks failed
PR description / Title and description (pull_request_target) Failing after 7s
289c9c563d
A tab switched to read its declarations and laid them out in one frame, then read its rows and drew its first rows
in the next, every time it was shown. Now the menu keeps each page's declarations laid out until their generation
moves on (a mod started or stopped, the language or the renderer's lists changed). A page shown again reads its
rows in the frame that takes its kept declarations, the frame before it is drawn, so its first drawing frame reads
only the rows that moved since. A page's first showing reads as before: its declarations in one frame, its rows with
its first drawing, never both reads in one frame. laidOut makes the menu's own lines with one shared flags object
and walks an anchor's chain without a set.

The declarations' generation now hashes every field a declaration gives the menu (help, kind and choices, place,
flags, native use), not only the key, label and kind, so a mod declared again with other choices or another place
is read again.

The cost test times a tab shown again (4.5 ms, every system row) and, apart, its first showing (the declarations
given again with a label changed: a new generation, its read asserted; 5.5 ms, the switch's ceiling before). A new
test holds a page shown again to no declarations read until they change.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
HumanBean force-pushed perf/menu-ui-render from 289c9c563d
Some checks failed
PR description / Title and description (pull_request_target) Failing after 7s
to 1dcf4b82bc
Some checks failed
PR description / Title and description (pull_request_target) Failing after 7s
2026-11-30 08:34:59 +00:00
Compare
Author
Owner

Rebased onto mod-api 12f42db5 now that #2861 is merged: no longer stacked, no conflicts; menu tests pass (Graphics shown again 1.74 ms debug).

Rebased onto mod-api 12f42db5 now that #2861 is merged: no longer stacked, no conflicts; menu tests pass (Graphics shown again 1.74 ms debug).
Author
Owner
@review
BeanBot left a comment

Recommendation: Request changes. Cached declarations can retain an old slider format or default after a mod redeclares a setting.

The change keeps laid-out menu pages and reads their rows one frame earlier when revisiting them. That can spread switch work across frames, at the cost of retaining page layouts and relying on complete cache invalidation.

Blocking (2): must change before merging.

  1. L1 game/crates/api/src/services/settings_host.rs:200: The generation omits the declaration's format and default, although the cached declaration contains both. (more on the line) Do: Include the declaration-affecting parts of r.format and r.default in the generation, and add a test that redeclares a previously visited row with changed format and default.
  2. L3: The check PR description / Title and description (pull_request_target) fails on this head and passes on the base branch: Failing after 7s.

Should fix (1): not blocking, but worth doing before merging.

  • L2 game/mods/menu/test/menu.test.ts:1080: The new test counts row reads across the whole switch and does not check that they happen before the drawing frame. (more on the line) Do: Record reads frame by frame on a revisited page and assert that its rows are read before its first drawing frame.

@fix can do L1, L2 and L3.

Questions (1) (not confirmed from the code; they don't hold the recommendation)

  • Do interleaved base and head measurements on the same PC show that the reported switch improvement does not hold under typical conditions?
More: does it help, other systems, switches, what was read

Does it help: Unproven. The reported debug fake-game minimum fell from 4.49 ms to 1.61 ms, but the results are least-of-five samples from different jobs, with no interleaved runs or spread, and do not measure the player-visible game.

Other systems: The menu runs on Windows and Linux and uses declarations from system and third-party mods. The headless Linux run stepped through the tabs but could not show their rendered contents; language changes and visible page switching remain untested.

Switches: None added, changed, or removed.

Reviewed: 1dcf4b82bc against 12f42db5c1: Read the repository rules, contribution standards, PR template, diff, commit, view.ts, and the changed settings-host and menu paths with their callers and data structures. Skimmed the remaining portions of the larger changed files and their tests; output truncation prevented a full read of those files. Tooling's adjacent AGENTS.md and docs/review-checklist.md were absent.; nothing run (a code review).

Checked and fine:

  • The commit records AI assistance with a Co-Authored-By trailer.
  • No generated files, third-party files, game-derived files, API version, or switch table entries are changed.
  • The changed anchor walk retains the prior cycle-detection behavior for the page's lines.

Testing: No commands from the project were run. The author reports passing menu and settings-host tests and a headless Linux smoke run; the supplied CI reports a PR-description check failure on this head.

Overlaps and merge order: None identified from the supplied context.


Automated code review by BeanBot (gpt-6.1-terra), asked for by HumanBean. Nothing was run; a maintainer decides.

**Recommendation: Request changes.** Cached declarations can retain an old slider format or default after a mod redeclares a setting. The change keeps laid-out menu pages and reads their rows one frame earlier when revisiting them. That can spread switch work across frames, at the cost of retaining page layouts and relying on complete cache invalidation. **Blocking (2)**: must change before merging. 1. **L1** `game/crates/api/src/services/settings_host.rs:200`: The generation omits the declaration's format and default, although the cached declaration contains both. (more on the line) **Do:** Include the declaration-affecting parts of r.format and r.default in the generation, and add a test that redeclares a previously visited row with changed format and default. 2. **L3**: The check `PR description / Title and description (pull_request_target)` fails on this head and passes on the base branch: Failing after 7s. **Should fix (1)**: not blocking, but worth doing before merging. - **L2** `game/mods/menu/test/menu.test.ts:1080`: The new test counts row reads across the whole switch and does not check that they happen before the drawing frame. (more on the line) **Do:** Record reads frame by frame on a revisited page and assert that its rows are read before its first drawing frame. `@fix` can do L1, L2 and L3. **Questions (1)** (not confirmed from the code; they don't hold the recommendation) - Do interleaved base and head measurements on the same PC show that the reported switch improvement does not hold under typical conditions? <details> <summary>More: does it help, other systems, switches, what was read</summary> **Does it help:** Unproven. The reported debug fake-game minimum fell from 4.49 ms to 1.61 ms, but the results are least-of-five samples from different jobs, with no interleaved runs or spread, and do not measure the player-visible game. **Other systems:** The menu runs on Windows and Linux and uses declarations from system and third-party mods. The headless Linux run stepped through the tabs but could not show their rendered contents; language changes and visible page switching remain untested. **Switches:** None added, changed, or removed. **Reviewed:** 1dcf4b82bc5f against 12f42db5c145: Read the repository rules, contribution standards, PR template, diff, commit, view.ts, and the changed settings-host and menu paths with their callers and data structures. Skimmed the remaining portions of the larger changed files and their tests; output truncation prevented a full read of those files. Tooling's adjacent AGENTS.md and docs/review-checklist.md were absent.; nothing run (a code review). **Checked and fine:** - The commit records AI assistance with a Co-Authored-By trailer. - No generated files, third-party files, game-derived files, API version, or switch table entries are changed. - The changed anchor walk retains the prior cycle-detection behavior for the page's lines. **Testing:** No commands from the project were run. The author reports passing menu and settings-host tests and a headless Linux smoke run; the supplied CI reports a PR-description check failure on this head. **Overlaps and merge order:** None identified from the supplied context. </details> --- <sub>Automated code review by BeanBot (gpt-6.1-terra), asked for by HumanBean. Nothing was run; a maintainer decides.</sub>
@ -197,2 +198,3 @@
// moves it on, so the menu's kept pages are laid out again)
for r in rows.iter().filter(|r| placed(r)) {
(&r.mod_id, &r.key, &r.label, kind_name_of(&r.kind)).hash(&mut h);
(&r.mod_id, &r.key, &r.label, &r.help, format!("{:?}", r.kind), r.restart, r.preset_exempt).hash(&mut h);
Member

L1 (blocking): The generation omits the declaration's format and default, although the cached declaration contains both.

When: If a mod redeclares a slider with the same key, label, kind and placement but changes its unit or display scale, service_decl_json returns the new format while generation_of remains unchanged. A previously visited page retains the old unit or scale and displays the slider incorrectly; a changed default is likewise retained.

Do: Include the declaration-affecting parts of r.format and r.default in the generation, and add a test that redeclares a previously visited row with changed format and default.

**L1** (blocking): The generation omits the declaration's format and default, although the cached declaration contains both. *When:* If a mod redeclares a slider with the same key, label, kind and placement but changes its unit or display scale, service_decl_json returns the new format while generation_of remains unchanged. A previously visited page retains the old unit or scale and displays the slider incorrectly; a changed default is likewise retained. **Do:** Include the declaration-affecting parts of r.format and r.default in the generation, and add a test that redeclares a previously visited row with changed format and default.
@ -1038,0 +1077,4 @@
await switchTo("graphics");
const again = await switchTo("audio");
assert.equal(again.decls, 0, "shown again: its declarations as laid out before");
assert.ok(again.rows > 0, "its rows read again");
Member

L2 (should fix): The new test counts row reads across the whole switch and does not check that they happen before the drawing frame.

When: Removing the early rowsOf(keys) call while keeping the declaration cache still reads the rows on the drawing frame. The test's rows > 0 assertion passes even though the change's key scheduling improvement is gone.

Do: Record reads frame by frame on a revisited page and assert that its rows are read before its first drawing frame.

**L2** (should fix): The new test counts row reads across the whole switch and does not check that they happen before the drawing frame. *When:* Removing the early rowsOf(keys) call while keeping the declaration cache still reads the rows on the drawing frame. The test's rows > 0 assertion passes even though the change's key scheduling improvement is gone. **Do:** Record reads frame by frame on a revisited page and assert that its rows are read before its first drawing frame.
Author
Owner

Thanks. L1 fixed in 656c1a57: the generation now hashes each row's format (unit, display scale, default text) and default, with a test that a row declared again with another unit, scale or default moves it on. L2 fixed: the revisit test steps frame by frame and asserts the Audio tab's rows are read in a frame before the one that first draws it, and none in that frame. L3: Measurements' columns now name their commits (mod-api @ 12f42db5, this PR @ 1dcf4b82). On the question: the two sides came from separate jobs, not interleaved; the description now says so.

Thanks. L1 fixed in 656c1a57: the generation now hashes each row's format (unit, display scale, default text) and default, with a test that a row declared again with another unit, scale or default moves it on. L2 fixed: the revisit test steps frame by frame and asserts the Audio tab's rows are read in a frame before the one that first draws it, and none in that frame. L3: Measurements' columns now name their commits (mod-api @ 12f42db5, this PR @ 1dcf4b82). On the question: the two sides came from separate jobs, not interleaved; the description now says so.
HumanBean merged commit 1f49aeec39 into mod-api 2026-11-30 19:27:00 +00:00
HumanBean deleted branch perf/menu-ui-render 2026-11-30 19:27:01 +00:00
Sign in to join this conversation.
No reviewers
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!2874
No description provided.