WIP: perf(linux): the code that runs in play in anonymous 2 MB pages, in one piece at the start of the text (HOS_CODE_HUGE) #2921

Closed
HumanBean wants to merge 1 commit from HumanBean/Longshot:perf/code-huge-pages into main
Owner

Summary

On a PC short of memory, the game's code is dropped from RAM and read back from disk while you play. A thread that
needs a dropped page stops until the disk has read it, and on a hard drive that gives 100-500 ms frames. The lab PC
(RX 580, 16 GB RAM, game on a hard drive) shows these stutters. #2893 (HOS_CODE_WARM) cut them from ~18 to ~13 per
16-minute tour by keeping cached code pages warm.

This PR goes further on Linux. At start, it copies all the code the guest profile saw run (about 255 MB) into the
game's own memory, in 2 MB pages, and swaps that copy in place of the file's pages:

  • The kernel can't drop it to disk. Under pressure it can only swap it, and the kernel reclaims the page cache
    first. zram swap-in takes microseconds.
  • Fewer TLB misses. A 2 MB page takes one TLB entry where 512 small pages took 512.

The move runs on a thread of its own while the game starts from the file's pages. The swap is one mremap call: the
same bytes at the same addresses. A thread running that code during the swap waits for the mappings, then runs the
copy.

Why "all the code that ran" and not the hot units. First I checked how the code was laid out:

  • lld's call-graph sort (hfsort) spreads the 65 MB of hot units over the first 210 MB of the 740 MB text. 159 MB of
    other code that ran, and 31 MB of code that never ran, are mixed in.
  • In a tour, the code actually touched was 194-260 MB, spread evenly over the first 256 MB. Hot units were only a
    quarter of it.
  • No order from the profile packed it tighter. By its call graph, its counts or its densities, 90 % of the touched
    code needs the first 230-240 MB.

So this moves everything the profile saw run, not a hot subset.

What changes in the build (game_link_order, Linux only): hfsort still puts the functions it orders first. The
rest now follow in a fixed object order:

  1. the hot and other lifted units (the functions the profile saw run)
  2. the lift's runtime, the host and the rest
  3. host/code_huge_end.c's object
  4. the cold units (functions the profile saw never run)

So the code that runs in play is the text from its start up to longshot_code_ran_end: 257 MB, holding all 65 MB of
hot units, all 159 MB of other code that ran, 1.6 MB of host code, and the 29.5 MB of never-ran code hfsort mixes in.
host/code_huge.c moves that span. The link time is unchanged (3 s against main's 4 s here).

Two other layouts were tried and dropped:

  • lld's --symbol-ordering-file: it cost 12-19 s more a link, whether it named 2 symbols or 72,000.
  • Object order without hfsort: the Market ran 3.5 % slower (3 interleaved pairs, every run below main's).

Windows is unchanged.

HOS_CODE_HUGE is on by default on Linux with 12 GB of RAM or more, when transparent huge pages aren't off. =0 keeps
the code in the file's pages; =1 moves it below 12 GB too. Windows builds a stub: large pages there need a privilege
players don't have.

What a crash record shows. A crash in the moved code is still named longshot+0x..., through hos_code_huge_at.
The profilers use dladdr, which reads the program headers, so they are unaffected.

With HOS_CODE_WARM. It reads only file mappings, so it leaves the moved part alone and keeps the rest warm.

Read first: host/code_huge.c, game_link_order in builder/crates/build-compile/src/lib.rs,
tests/test_code_huge.py.

See #2893.

Testing

Tested: RTX 3090 / 620.70.06 / Linux (CachyOS, kernel 7.3.3, btrfs, 32 GB). Runs from main's base (d193d12d)
against this PR's pack, through the job queue:

  • scripts/memtour.py tours from the market-entrance save (8 or 5 areas), with and without memory pressure.
  • dev.py ab in Hyrule Field and the Market.
  • Every run with the move on logs code: 254-256 MB of the game's code that runs in play ... moved to anonymous memory. smaps showed the whole range in 2 MB pages (AnonHugePages) through an unpressured tour.
  • HOS_CODE_HUGE=0 logs that the code stays in the file's pages.
  • No crash and no difference in play. The guest profile's code runs the same, as tests/test_code_huge.py checks
    without the game.

Checks:

  • tests/test_code_huge.py passes with clang and gcc, and 10 times in a row. It links the real marker file and
    code_huge.c in the game's order, after 4 MB of code. It checks the move, the code running from the copy, a second
    thread running that code all through the move, the crash-record lookup, HOS_CODE_HUGE=0, and the span's bounds.
  • cargo test -p build-compile passes, with a new test of game_link_order; also cargo test -p lift.
  • clippy is clean on the change. Two needless_borrows lints at the .ico lines of build-compile's lib.rs and a
    rustfmt diff in toolchain.rs fail on main too, with the pinned 1.99.1.
  • Host C compiles for Linux (clang, gcc) and Windows (llvm-mingw 20261104): code_huge*.c, hos_crash_record.c.
  • run_all.py --strict --changed: the checks reading the changed files pass, except failures that also fail on
    main: test_switch_table (LONGSHOT_LAUNCHER_STARTUP has no row), test_crash_record (posix/display_sdl.c),
    test_bug_report, test_windows_on_linux, and checks this PC lacks the tools for.

Still to test:

  • The lab (RX 580, 16 GB, game on a hard drive), where the gain should be largest: the same 16-minute tour as #2893,
    main against this PR, expecting fewer frames over 100 ms and fewer major faults on code (by hand; it will be
    checked there).
  • Steam Deck (16 GB, ext4): a start and a few minutes of play, expecting the code: ... moved line and nothing else
    different (@Beanstalk).
  • An AMD GPU on Linux (Mesa RADV): the same start and play (@Beanstalk).
  • Windows: unchanged except a stub and two empty functions; a start (@Beanstalk).

Measurements

Verdict: under memory pressure, frames over 100 ms are about halved, in two separate batches on this PC. Nothing
else changes, unpressured or under pressure: fps, 1 % low and p99 are within the runs' spread. The lab (RX 580, 16 GB,
hard drive), where code is really read back from disk, is where the gain should be largest. It will be checked there,
and this stays WIP until then.

Setup: Linux (CachyOS, kernel 7.3.3), RTX 3090, driver 620.70.06, 32 GB, btrfs NVMe. main is d193d12d (with #2893),
built as the base; this PR is its pack over it.

Under pressure. A scripts/memtour.py tour from the market-entrance save: 5 areas, 30 s each, headless, the game's
60 cap. From the start of play, the game's own systemd scope is asked every 2 s to reclaim 400 MB (memory.reclaim, at
swappiness 60: file and anonymous memory as a usual distribution balances them). Nothing outside the game's scope is
touched. Runs were interleaved main / HUGE=0 / HUGE=1, twice. Numbers are per minute of play from the frame trace
(RX_VK_FRAME_TRACE), medians of the two runs. Play windows differ (155-257 s) because the tour sometimes stalls in an
in-game event, on every side alike.

Before: main @ d193d12d After, HOS_CODE_HUGE=0: this PR @ 8909ac9f After: this PR @ 8909ac9f
frames over 100 ms per minute (run 1 / run 2) 3.9 (2.9 / 5.0) 3.3 (4.3 / 2.3) 1.6 (0.7 / 2.6)
frames over 50 ms per minute 21.1 24.2 26.8
1 % low fps 15.7 14.7 14.2
p99 frame time 32.4 ms 33.0 ms 31.4 ms
major faults per minute 4,330 6,000 5,240
code in 2 MB pages 0 0 254 / 144 MB

An earlier batch, with the code in object order and no hfsort (that layout was dropped for the Market result
below), gave the same picture with a busier PC:

main HUGE=0 HUGE=1
frames over 100 ms per minute 8.3 (7.1 / 9.5) 7.9 3.9 (3.5 / 4.3)
p99 frame time 43.6 ms 40.8 ms 37.5 ms
major faults per minute 6,600 7,560 4,810

Unpressured, dev.py ab. 3 interleaved pairs, 60 s each, uncapped, 67 % render resolution, the median:

Before: main @ d193d12d After: this PR @ 8909ac9f Δ
Hyrule Field fps 151.3 (151.3 / 152.1 / 151.2) 150.9 (151.7 / 150.9 / 150.4) -0.2 %, within the spread
Hyrule Field p99 7.28 ms 7.33 ms +0.05 ms
Market fps 124.1 (123.6 / 125.3 / 124.1) 124.2 (124.2 / 122.5 / 124.9) +0.1 %, within the spread
Market p99 9.65 ms 9.81 ms +0.16 ms

On the dropped object-order layout, a Market switch A/B (--env HOS_CODE_HUGE=0, 3 pairs) showed no difference
(-0.9 %, within the spread). So the move itself costs nothing in play; that layout's 3.5 % came from losing hfsort.

The move's cost:

  • Start: the copy runs on its own thread while the game starts, taking 0.46-1.66 s here (longest when the file's
    pages must be read back from btrfs). In a fragmented run, 144 of 256 MB landed in 2 MB pages.
  • RAM: ~255 MB of the game's own memory instead of page cache.

The build: the link takes 3 s (base 4 s). In isolation, the game's link took 1.6-2.2 s with hfsort, 1.2 s with no
sort, and 13.8-20.7 s with an order file. The compile is unchanged, so the players' build time is unchanged.

Code pages touched in a tour (the play window's pagemap union, after the game's file pages were evicted once):
260 MB on main, 76.5 % of it within the first 227 MB.

[hos host] code: 254 MB of the game's code that runs in play (text +0x201a000 to +0x11e1a000) moved to anonymous memory, 254 MB of it in 2 MB pages, in 786 ms (the copy 726, the collapse 50) (HOS_CODE_HUGE=0: left in the file's pages)
[hos host] code: 256 MB of the game's code that runs in play (text +0x1f6c000 to +0x11f6c000) moved to anonymous memory, 144 MB of it in 2 MB pages (MADV_COLLAPSE failed), in 1662 ms (the copy 1644, the collapse 0) (HOS_CODE_HUGE=0: left in the file's pages)
[hos host] code: the game's code stays in the executable file's pages (HOS_CODE_HUGE=0)

HOS_CODE_WARM finds nothing on btrfs. On main here, #2893 logs "0 MB of the game's code kept in memory, of 0 MB
in 0 mappings". It matches mappings by stat()'s device, and btrfs reports a subvolume's own device there, not the one
/proc/self/maps shows. That is #2893's, not this PR's, and is reported separately; it doesn't change the comparison.

Risks

  • RAM. About 255 MB becomes the game's own memory instead of page cache. Page cache held most of the same pages
    while the game played, but it could drop them, and now only swap can take them. Guarded at 12 GB of RAM.
    HOS_CODE_HUGE=0 turns it off.
  • Start. The copy runs beside the game's start, on its own thread. Measured here: 0.46-1.5 s, longer when the
    file's pages must be read back from disk; on a hard drive a few seconds. The game doesn't wait for it.
  • Fragmented memory. When the kernel has no free 2 MB pages, the copy is made in small pages. The code still
    can't be dropped as page cache, but there is no TLB gain, and the log says how much landed in 2 MB pages (110 of
    224 MB in one run here).
  • Tools that name code from /proc/self/maps. They see the moved range as anonymous memory. The crash record
    handles it, and the samplers use dladdr. Something external like perf would show those addresses as
    [anon]; set HOS_CODE_HUGE=0 for such a run.
  • Layout. hfsort still orders what it ordered; only the code it leaves moves (cold units last). Unpressured
    fps measured the same in both scenes.
  • Players notice: nothing to set, saves and settings untouched. The game's log has a new code: line.

Checklist

Ready for review

  • One topic, on current main; 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): the checks reading the changed files ran (see Testing); the whole suite not yet.
  • 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 switch added or changed: it ships at the default the measurements chose, with its row in gpu/vk/switches.def.
  • Host code builds on Windows (llvm-mingw) and Linux, with UTF-8 paths.
  • Docs updated.
  • AI assistance recorded with Assisted-by: / Co-Authored-By: trailers.

Ready to merge

  • Everything in Still to test has run, its results in Tested.
  • Tier 1 hardware it can affect is tested (docs/dev/standards.md, "Hardware tiers").
  • 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_016o1k1SdGuww9Q4o1mvYD9c

## Summary On a PC short of memory, the game's code is dropped from RAM and read back from disk while you play. A thread that needs a dropped page stops until the disk has read it, and on a hard drive that gives 100-500 ms frames. The lab PC (RX 580, 16 GB RAM, game on a hard drive) shows these stutters. #2893 (HOS_CODE_WARM) cut them from ~18 to ~13 per 16-minute tour by keeping cached code pages warm. This PR goes further on Linux. At start, it copies all the code the guest profile saw run (about 255 MB) into the game's own memory, in 2 MB pages, and swaps that copy in place of the file's pages: - **The kernel can't drop it to disk.** Under pressure it can only swap it, and the kernel reclaims the page cache first. zram swap-in takes microseconds. - **Fewer TLB misses.** A 2 MB page takes one TLB entry where 512 small pages took 512. The move runs on a thread of its own while the game starts from the file's pages. The swap is one `mremap` call: the same bytes at the same addresses. A thread running that code during the swap waits for the mappings, then runs the copy. **Why "all the code that ran" and not the hot units.** First I checked how the code was laid out: - lld's call-graph sort (hfsort) spreads the 65 MB of hot units over the first 210 MB of the 740 MB text. 159 MB of other code that ran, and 31 MB of code that never ran, are mixed in. - In a tour, the code actually touched was 194-260 MB, spread evenly over the first 256 MB. Hot units were only a quarter of it. - No order from the profile packed it tighter. By its call graph, its counts or its densities, 90 % of the touched code needs the first 230-240 MB. So this moves everything the profile saw run, not a hot subset. **What changes in the build** (`game_link_order`, Linux only): hfsort still puts the functions it orders first. The rest now follow in a fixed object order: 1. the hot and other lifted units (the functions the profile saw run) 2. the lift's runtime, the host and the rest 3. `host/code_huge_end.c`'s object 4. the cold units (functions the profile saw never run) So the code that runs in play is the text from its start up to `longshot_code_ran_end`: 257 MB, holding all 65 MB of hot units, all 159 MB of other code that ran, 1.6 MB of host code, and the 29.5 MB of never-ran code hfsort mixes in. `host/code_huge.c` moves that span. The link time is unchanged (3 s against main's 4 s here). Two other layouts were tried and dropped: - lld's `--symbol-ordering-file`: it cost 12-19 s more a link, whether it named 2 symbols or 72,000. - Object order without hfsort: the Market ran 3.5 % slower (3 interleaved pairs, every run below main's). Windows is unchanged. `HOS_CODE_HUGE` is on by default on Linux with 12 GB of RAM or more, when transparent huge pages aren't off. `=0` keeps the code in the file's pages; `=1` moves it below 12 GB too. Windows builds a stub: large pages there need a privilege players don't have. **What a crash record shows.** A crash in the moved code is still named `longshot+0x...`, through `hos_code_huge_at`. The profilers use `dladdr`, which reads the program headers, so they are unaffected. **With HOS_CODE_WARM.** It reads only file mappings, so it leaves the moved part alone and keeps the rest warm. Read first: `host/code_huge.c`, `game_link_order` in `builder/crates/build-compile/src/lib.rs`, `tests/test_code_huge.py`. See #2893. ## Testing **Tested:** RTX 3090 / 620.70.06 / Linux (CachyOS, kernel 7.3.3, btrfs, 32 GB). Runs from main's base (d193d12d) against this PR's pack, through the job queue: - `scripts/memtour.py` tours from the market-entrance save (8 or 5 areas), with and without memory pressure. - `dev.py ab` in Hyrule Field and the Market. - Every run with the move on logs `code: 254-256 MB of the game's code that runs in play ... moved to anonymous memory`. smaps showed the whole range in 2 MB pages (AnonHugePages) through an unpressured tour. - `HOS_CODE_HUGE=0` logs that the code stays in the file's pages. - No crash and no difference in play. The guest profile's code runs the same, as `tests/test_code_huge.py` checks without the game. Checks: - `tests/test_code_huge.py` passes with clang and gcc, and 10 times in a row. It links the real marker file and `code_huge.c` in the game's order, after 4 MB of code. It checks the move, the code running from the copy, a second thread running that code all through the move, the crash-record lookup, `HOS_CODE_HUGE=0`, and the span's bounds. - `cargo test -p build-compile` passes, with a new test of `game_link_order`; also `cargo test -p lift`. - clippy is clean on the change. Two `needless_borrows` lints at the `.ico` lines of build-compile's `lib.rs` and a rustfmt diff in `toolchain.rs` fail on `main` too, with the pinned 1.99.1. - Host C compiles for Linux (clang, gcc) and Windows (llvm-mingw 20261104): `code_huge*.c`, `hos_crash_record.c`. - `run_all.py --strict --changed`: the checks reading the changed files pass, except failures that also fail on `main`: `test_switch_table` (LONGSHOT_LAUNCHER_STARTUP has no row), `test_crash_record` (`posix/display_sdl.c`), `test_bug_report`, `test_windows_on_linux`, and checks this PC lacks the tools for. **Still to test:** - The lab (RX 580, 16 GB, game on a hard drive), where the gain should be largest: the same 16-minute tour as #2893, main against this PR, expecting fewer frames over 100 ms and fewer major faults on code (by hand; it will be checked there). - Steam Deck (16 GB, ext4): a start and a few minutes of play, expecting the `code: ... moved` line and nothing else different (@Beanstalk). - An AMD GPU on Linux (Mesa RADV): the same start and play (@Beanstalk). - Windows: unchanged except a stub and two empty functions; a start (@Beanstalk). ## Measurements **Verdict:** under memory pressure, frames over 100 ms are about halved, in two separate batches on this PC. Nothing else changes, unpressured or under pressure: fps, 1 % low and p99 are within the runs' spread. The lab (RX 580, 16 GB, hard drive), where code is really read back from disk, is where the gain should be largest. It will be checked there, and this stays WIP until then. Setup: Linux (CachyOS, kernel 7.3.3), RTX 3090, driver 620.70.06, 32 GB, btrfs NVMe. main is d193d12d (with #2893), built as the base; this PR is its pack over it. **Under pressure.** A `scripts/memtour.py` tour from the market-entrance save: 5 areas, 30 s each, headless, the game's 60 cap. From the start of play, the game's own systemd scope is asked every 2 s to reclaim 400 MB (`memory.reclaim`, at swappiness 60: file and anonymous memory as a usual distribution balances them). Nothing outside the game's scope is touched. Runs were interleaved main / HUGE=0 / HUGE=1, twice. Numbers are per minute of play from the frame trace (RX_VK_FRAME_TRACE), medians of the two runs. Play windows differ (155-257 s) because the tour sometimes stalls in an in-game event, on every side alike. | | Before: main @ d193d12d | After, HOS_CODE_HUGE=0: this PR @ 8909ac9f | After: this PR @ 8909ac9f | |---|---|---|---| | frames over 100 ms per minute (run 1 / run 2) | 3.9 (2.9 / 5.0) | 3.3 (4.3 / 2.3) | 1.6 (0.7 / 2.6) | | frames over 50 ms per minute | 21.1 | 24.2 | 26.8 | | 1 % low fps | 15.7 | 14.7 | 14.2 | | p99 frame time | 32.4 ms | 33.0 ms | 31.4 ms | | major faults per minute | 4,330 | 6,000 | 5,240 | | code in 2 MB pages | 0 | 0 | 254 / 144 MB | An earlier batch, with the code in object order and no hfsort (that layout was dropped for the Market result below), gave the same picture with a busier PC: | | main | HUGE=0 | HUGE=1 | |---|---|---|---| | frames over 100 ms per minute | 8.3 (7.1 / 9.5) | 7.9 | 3.9 (3.5 / 4.3) | | p99 frame time | 43.6 ms | 40.8 ms | 37.5 ms | | major faults per minute | 6,600 | 7,560 | 4,810 | **Unpressured, `dev.py ab`.** 3 interleaved pairs, 60 s each, uncapped, 67 % render resolution, the median: | | Before: main @ d193d12d | After: this PR @ 8909ac9f | Δ | |---|---|---|---| | Hyrule Field fps | 151.3 (151.3 / 152.1 / 151.2) | 150.9 (151.7 / 150.9 / 150.4) | -0.2 %, within the spread | | Hyrule Field p99 | 7.28 ms | 7.33 ms | +0.05 ms | | Market fps | 124.1 (123.6 / 125.3 / 124.1) | 124.2 (124.2 / 122.5 / 124.9) | +0.1 %, within the spread | | Market p99 | 9.65 ms | 9.81 ms | +0.16 ms | On the dropped object-order layout, a Market switch A/B (`--env HOS_CODE_HUGE=0`, 3 pairs) showed no difference (-0.9 %, within the spread). So the move itself costs nothing in play; that layout's 3.5 % came from losing hfsort. **The move's cost:** - Start: the copy runs on its own thread while the game starts, taking 0.46-1.66 s here (longest when the file's pages must be read back from btrfs). In a fragmented run, 144 of 256 MB landed in 2 MB pages. - RAM: ~255 MB of the game's own memory instead of page cache. **The build:** the link takes 3 s (base 4 s). In isolation, the game's link took 1.6-2.2 s with hfsort, 1.2 s with no sort, and 13.8-20.7 s with an order file. The compile is unchanged, so the players' build time is unchanged. **Code pages touched in a tour** (the play window's pagemap union, after the game's file pages were evicted once): 260 MB on main, 76.5 % of it within the first 227 MB. ``` [hos host] code: 254 MB of the game's code that runs in play (text +0x201a000 to +0x11e1a000) moved to anonymous memory, 254 MB of it in 2 MB pages, in 786 ms (the copy 726, the collapse 50) (HOS_CODE_HUGE=0: left in the file's pages) [hos host] code: 256 MB of the game's code that runs in play (text +0x1f6c000 to +0x11f6c000) moved to anonymous memory, 144 MB of it in 2 MB pages (MADV_COLLAPSE failed), in 1662 ms (the copy 1644, the collapse 0) (HOS_CODE_HUGE=0: left in the file's pages) [hos host] code: the game's code stays in the executable file's pages (HOS_CODE_HUGE=0) ``` **HOS_CODE_WARM finds nothing on btrfs.** On main here, #2893 logs "0 MB of the game's code kept in memory, of 0 MB in 0 mappings". It matches mappings by `stat()`'s device, and btrfs reports a subvolume's own device there, not the one /proc/self/maps shows. That is #2893's, not this PR's, and is reported separately; it doesn't change the comparison. ## Risks - **RAM.** About 255 MB becomes the game's own memory instead of page cache. Page cache held most of the same pages while the game played, but it could drop them, and now only swap can take them. Guarded at 12 GB of RAM. `HOS_CODE_HUGE=0` turns it off. - **Start.** The copy runs beside the game's start, on its own thread. Measured here: 0.46-1.5 s, longer when the file's pages must be read back from disk; on a hard drive a few seconds. The game doesn't wait for it. - **Fragmented memory.** When the kernel has no free 2 MB pages, the copy is made in small pages. The code still can't be dropped as page cache, but there is no TLB gain, and the log says how much landed in 2 MB pages (110 of 224 MB in one run here). - **Tools that name code from /proc/self/maps.** They see the moved range as anonymous memory. The crash record handles it, and the samplers use `dladdr`. Something external like `perf` would show those addresses as `[anon]`; set `HOS_CODE_HUGE=0` for such a run. - **Layout.** hfsort still orders what it ordered; only the code it leaves moves (cold units last). Unpressured fps measured the same in both scenes. - **Players notice:** nothing to set, saves and settings untouched. The game's log has a new `code:` line. ## Checklist ### Ready for review - [x] One topic, on current `main`; the title is a Conventional Commit. - [x] 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): the checks reading the changed files ran (see Testing); the whole suite not yet. - [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 switch added or changed: it ships at the default the measurements chose, with its row in `gpu/vk/switches.def`. - [x] Host code builds on Windows (llvm-mingw) and Linux, with UTF-8 paths. - [x] Docs updated. - [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. - [ ] Tier 1 hardware it can affect is tested (docs/dev/standards.md, "Hardware tiers"). - [ ] 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_016o1k1SdGuww9Q4o1mvYD9c
perf(linux): the code that runs in play in anonymous 2 MB pages, in one piece at the start of the text (HOS_CODE_HUGE)
Some checks failed
PR description / Title and description (pull_request_target) Successful in 8s
CI / What the change needs (pull_request) Successful in 10s
CI / Third-party parts and guide screenshots (pull_request) Failing after 14s
CI / Lint (pre-commit, ShellCheck, actionlint) (pull_request) Failing after 13s
CI / Rust (rustfmt, clippy, cargo test, end to end) (pull_request) Failing after 3m25s
CI / Host C for Windows (llvm-mingw, compile only) (pull_request) Successful in 2m13s
CI / Renderer build check (Windows, compile --only renderer, without and with FSR 4) (pull_request) Successful in 5m4s
CI / Renderer build check (Linux, compile --only renderer, without and with FSR 4) (pull_request) Successful in 5m6s
CI / Website build and checks (pull_request) Failing after 34s
CI / Game-free checks (run_all.py, part 1/2) (pull_request) Failing after 22m2s
CI / Game-free checks (run_all.py, part 2/2) (pull_request) Failing after 35m42s
8909ac9fe0
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_016o1k1SdGuww9Q4o1mvYD9c
Author
Owner

Tested in game: RTX 3090 / 620.70.06 / Linux (CachyOS 7.3.3, btrfs, 32 GB): main d193d12d against this PR's pack, through the job queue. Memtours from the market-entrance save with and without memory pressure (the game's own scope reclaiming 400 MB every 2 s), and dev.py ab in Hyrule Field and the Market. Every start with the move on logged 254-256 MB moved to anonymous memory (254 MB in 2 MB pages; 144 MB in one fragmented run), no crash, play the same. Under pressure, frames over 100 ms per minute 3.9 on main against 1.6 here; unpressured fps within the spread in both scenes. The lab (RX 580, HDD, 16 GB) is where the gain should be largest; it will be checked there.

Tested in game: RTX 3090 / 620.70.06 / Linux (CachyOS 7.3.3, btrfs, 32 GB): main d193d12d against this PR's pack, through the job queue. Memtours from the market-entrance save with and without memory pressure (the game's own scope reclaiming 400 MB every 2 s), and dev.py ab in Hyrule Field and the Market. Every start with the move on logged 254-256 MB moved to anonymous memory (254 MB in 2 MB pages; 144 MB in one fragmented run), no crash, play the same. Under pressure, frames over 100 ms per minute 3.9 on main against 1.6 here; unpressured fps within the spread in both scenes. The lab (RX 580, HDD, 16 GB) is where the gain should be largest; it will be checked there.
Author
Owner

Lab test (RX 580 / RADV Mesa 26.2.6 / Linux, 16 GB, game on a hard drive, btrfs, zram at swappiness 150): no gain. It is a little worse, so this stays WIP.

Build: this PR's commit duplicated onto main e6a71a1d. That main already has #2916, so HOS_CODE_WARM works on btrfs on both sides. It was a full game build on the lab. The A/B is HOS_CODE_HUGE=0 against the default on that one build. I ran the 16-minute tour (Kokiri, Lost Woods, Zora's River, Hyrule Field, Kakariko, 2 laps), headless and uncapped, after a warm-up run, interleaved off / on / on / off.

HUGE=0 (off1 / off2) default, moved (on1 / on2)
in play: frames over 100 ms 9 / 6 15 / 13
in play: frames over 50 ms 36 / 35 44 / 39
in play: fps, 1 % low 94.7 / 91.3, 31.8 / 31.2 93.5 / 94.8, 30.0 / 30.7
entry (first 5 s): frames over 100 ms 5 / 3 2 / 3
frames over 50 ms in play put down to faults with disk reads 4 / 4 11 / 7
major faults, whole run 12.3 k / 13.8 k 24.6 k / 12.9 k
read_bytes, whole run 7.3 / 7.2 GB 7.9 / 8.1 GB
texture-load bursts with a frame over 100 ms (burst2) 8 of 126 10 of 123

The code: lines:

  • 248-250 MB moved, all of it in 2 MB pages.
  • The move took 3.6 s and 4.5 s when the code had to come from the hard drive (the warm-up and on2), and 79 ms when it was cached (on1).

What I think happens: with the move, the process carries about 250 MB more anonymous memory (end of run: AnonHugePages 567 MB against 240 MB, Swap 2.29 GB against 1.57 GB). At 16 GB with swappiness 150, that pushes other memory into zram and the rest of the page cache (data, the 486 MB of text left in the file) out to disk. So the disk-read faults move elsewhere instead of going away. The memory-pressure win measured on the 32 GB PC doesn't carry over. Ideas: a lower default threshold, moving only the hot units, or turning it off when swappiness is high or zram is in use. All of those would need another lab round.

Boxes: nothing new settled from the lab. The run didn't crash, and the screenshots weren't checked for visual differences (headless tour, no reference).

**Lab test (RX 580 / RADV Mesa 26.2.6 / Linux, 16 GB, game on a hard drive, btrfs, zram at swappiness 150): no gain. It is a little worse, so this stays WIP.** Build: this PR's commit duplicated onto main e6a71a1d. That main already has [#2916](https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/2916), so HOS_CODE_WARM works on btrfs on both sides. It was a full game build on the lab. The A/B is `HOS_CODE_HUGE=0` against the default on that one build. I ran the 16-minute tour (Kokiri, Lost Woods, Zora's River, Hyrule Field, Kakariko, 2 laps), headless and uncapped, after a warm-up run, interleaved off / on / on / off. | | HUGE=0 (off1 / off2) | default, moved (on1 / on2) | |---|---|---| | in play: frames over 100 ms | 9 / 6 | 15 / 13 | | in play: frames over 50 ms | 36 / 35 | 44 / 39 | | in play: fps, 1 % low | 94.7 / 91.3, 31.8 / 31.2 | 93.5 / 94.8, 30.0 / 30.7 | | entry (first 5 s): frames over 100 ms | 5 / 3 | 2 / 3 | | frames over 50 ms in play put down to faults with disk reads | 4 / 4 | 11 / 7 | | major faults, whole run | 12.3 k / 13.8 k | 24.6 k / 12.9 k | | read_bytes, whole run | 7.3 / 7.2 GB | 7.9 / 8.1 GB | | texture-load bursts with a frame over 100 ms (burst2) | 8 of 126 | 10 of 123 | The `code:` lines: - 248-250 MB moved, all of it in 2 MB pages. - The move took 3.6 s and 4.5 s when the code had to come from the hard drive (the warm-up and on2), and 79 ms when it was cached (on1). What I think happens: with the move, the process carries about 250 MB more anonymous memory (end of run: AnonHugePages 567 MB against 240 MB, Swap 2.29 GB against 1.57 GB). At 16 GB with swappiness 150, that pushes other memory into zram and the rest of the page cache (data, the 486 MB of text left in the file) out to disk. So the disk-read faults move elsewhere instead of going away. The memory-pressure win measured on the 32 GB PC doesn't carry over. Ideas: a lower default threshold, moving only the hot units, or turning it off when swappiness is high or zram is in use. All of those would need another lab round. Boxes: nothing new settled from the lab. The run didn't crash, and the screenshots weren't checked for visual differences (headless tour, no reference).
Author
Owner

Dropped: on the RX 580 lab (16 GB, zram) it made in-play hitches worse (frames over 100 ms 9/6 off vs 15/13 on; disk-read-fault frames 4/4 vs 11/7), because the ~250 MB of anonymous huge pages pushes other memory to zram and page cache to disk. It only helped on a PC with spare RAM. Numbers: #2921 (comment)

Dropped: on the RX 580 lab (16 GB, zram) it made in-play hitches worse (frames over 100 ms 9/6 off vs 15/13 on; disk-read-fault frames 4/4 vs 11/7), because the ~250 MB of anonymous huge pages pushes other memory to zram and page cache to disk. It only helped on a PC with spare RAM. Numbers: https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/2921#issuecomment-94504
HumanBean closed this pull request 2026-12-01 00:54:47 +00:00
Some checks failed
PR description / Title and description (pull_request_target) Successful in 8s
CI / What the change needs (pull_request) Successful in 10s
CI / Third-party parts and guide screenshots (pull_request) Failing after 14s
CI / Lint (pre-commit, ShellCheck, actionlint) (pull_request) Failing after 13s
CI / Rust (rustfmt, clippy, cargo test, end to end) (pull_request) Failing after 3m25s
CI / Host C for Windows (llvm-mingw, compile only) (pull_request) Successful in 2m13s
CI / Renderer build check (Windows, compile --only renderer, without and with FSR 4) (pull_request) Successful in 5m4s
CI / Renderer build check (Linux, compile --only renderer, without and with FSR 4) (pull_request) Successful in 5m6s
CI / Website build and checks (pull_request) Failing after 34s
CI / Game-free checks (run_all.py, part 1/2) (pull_request) Failing after 22m2s
CI / Game-free checks (run_all.py, part 2/2) (pull_request) Failing after 35m42s

Pull request closed

Sign in to join this conversation.
No reviewers
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
MagicBeanDev/Longshot!2921
No description provided.