WIP: perf(os): HOS_OBJ_CACHE=sets, the per-thread sync object cache as 2048 sets of 8 ways of object numbers (experiment) #1969

Draft
siantm wants to merge 8 commits from siantm/Longshot:perf/obj-cache-sets into main
Contributor

Summary

Every nn::os mutex, event or queue call looks its host object up by guest address (obj_nockpt in
host/nn_os_sync.c), first in a per-thread cache. #1781 made that cache 16,384 entries of four ways (HOS_OBJ_CACHE).
exp/x86-asm-sync solved the same problem in another shape, and the two were never measured against each other. This
PR adds that shape beside #1781's, off unless HOS_OBJ_CACHE=sets:

  • the same 16,384 entries as 2,048 sets of 8 ways, a set one 64-byte line of 8 tags and the 8 objects' numbers (128 KB a
    thread, in place of the default sized cache's 256 KB, made at its first lookup and freed as the thread ends). The
    cache is the host thread's: its pointer a thread-local, and its release the thread's own exit, whatever fiber it runs
    then (on Windows the image's TLS callback, DLL_THREAD_DETACH; on Linux a pthread key's destructor). Not a fiber's
    slot (FLS): a guest fiber is a Win32 fiber (hos_thread.c) that the game may delete while the thread that ran it runs
    on, and its slot's callback would free the cache under that thread;
  • an address may sit in either of two sets (two hashes), which makes a full set rarer, though both of an address's sets
    can fill while others are empty; a miss replaces a way of the address's tag whose object is no longer its, else an
    empty way, else the next of the two sets' 16 ways in turn (a per-thread counter);
  • to fit 8 entries in a line, with sets the table numbers its objects as it makes them (obj_new_locked, chunks of
    1,024 that are never freed, as objects never are), so an entry is a 32-bit number (obj_of_id). Without sets (unset,
    a size, 0, or HOS_NO_OBJ_CACHE), each object is made alone, as on main.

Every lookup that reads an object without table_lock (every cache and find_unlocked) checks it with obj_is, which
reads its key and kind as one identity, of one life, in one load: beside its key and kind an object keeps one 64-bit
word of both (ident: the kind in the top 4 bits over a key under 2^60, a guest address; 0 for a tombstone or a larger
key), written after every change of its kind (obj_kind_set, release) and compared once (acquire) with the word the
lookup makes from its key and kind (obj_ident). An object is given another identity (a spare taken by another address,
a tombstone made again at its address) only as a tombstone, whose word is 0, and gets its new word last. Read separately, as before, a kind read
before a reassignment could pair with a key read after it: an object given to another address and then back to its
own as another kind passed for the old kind (a mutex lookup returning the address's new event), and a spare could
reach holder_ready under mw_lock for the wrong address. main's pointer caches (the sized default, the fixed one)
and find_unlocked read the same two fields separately and use the same check, so they are fixed here too. What a
lookup returns was that address's object of that kind when it read it, and stays so until the guest drops it: objects
are never freed, and a dropped one is reused only after 30 s in the graves and not while a condition variable waiter or a
multi-wait holds it (bury_locked).

HOS_NO_OBJ_CACHE and HOS_OBJ_CACHE are read once into one atomic word (obj_cache_mode_read; two first lookups at
once both store the same value), as is HOS_SYNC_TABLE_LOCK; obj_cache_entries returns OBJ_CACHE_SETS for sets, so
the lookup's path compares no string. A thread whose sets cannot be had (allocation or owner) goes without and does not
ask again. An object that cannot be had ends the process through hos_fatal, not a write through NULL. The hits and
misses count in the existing sync: object cache log line. The switch's row in gpu/vk/switches.def (now a string)
names the new value.

From 3cf76270 on exp/x86-asm-sync, where it replaced the 1,024-entry cache (its HOS_OBJ_CACHE_SMALL is
HOS_OBJ_CACHE=0 on main).

Read first: obj_ident, obj_kind_set, obj_is and the two reassignments in obj_lookup; obj_lookup_sets, obj_set_find, obj_sets_thread_end,
obj_new_locked and obj_nockpt in host/nn_os_sync.c; tests/test_obj_cache_size.py (its reuse, churn and
fiber modes).

Testing

Tested: Windows 11, i7-9700, RTX 4060 (NVIDIA 610.88), llvm-mingw clang 23.1.5; game-free checks and a
micro-benchmark only. This change has not run in the game on this PC yet, with the switch on or off (Still to test).
Each check ran in a hidden console of its own, at most 20 minutes, in two jobs of the PC's job queue (fb3-1969-r4l,
fb3-1969-r4b), on this branch on main @ 9dd41326 (head 712ee7e7; the first job's tests ran at c02418f4, the same tree
before its commit message was reworded):

  • python -I -B tests/test_obj_cache_size.py (with --cc llvm-mingw clang, with CC, and with the compiler found
    from LONGSHOT_MINGW): passes. "ok: obj_cache_entries"; the 4,000-object walk's last pass reaches the table 47 times
    unset, 4,000 times with =0 and =256, once with =sets (the sets case must stay at 10 or under); every lookup the
    table's object over 200,000 random operations; the log line; reuse with sets, unset, 0 and HOS_NO_OBJ_CACHE=1:
    the object reassigned before the lookup and after each of its loads (3 with sets, 2 unset and 0, 6 or 7
    unlocked), three ways, "ok"; churn: "64 threads made and ended: 2621511 bytes in allocations of 64 KB and up before,
    2621511 after"; fiber: "2752646 bytes ... with it, 2752646 once the fiber is deleted (the thread running on); the
    thread's next 4000 lookups reached the table 0 times; 0 bytes written in freed memory; 2621511 after the thread ended,
    2621511 before it". With the same test file (b7554f7b) on 479a4f90's source (the sequence check) it passes too; on
    9baebe0d's source (kind, then key) both cases fail: reuse in all four on the third way ("returned the object now
    its own address's again, of another kind (another's in between) ... reassigned right after the lookup's load 2" unset
    and 0, load 3 with sets, load 6 unlocked), and fiber ("2719878 ... with it, 2588743 once the fiber is deleted
    ...; reached the table 4000 times; 30053 bytes written in freed memory").
  • python -I -B tests/test_multiwait_wake.py: passes ("targeted: ok", "all: ok", the source check;
    __atomic_thread_fence no longer allowed under mw_lock: none is made; the FLS calls no longer).
  • python -I -B tests/test_cv_handoff_threads.py (it builds the real struct and obj_lookup's branch for an address
    made again): passes.
  • sizeof(Obj) (the struct as the tests cut it, llvm-mingw clang for x86-64 Windows): 344 bytes on main and at
    479a4f90, 352 at this head (the identity word: the fields before owner 40 bytes, now 48).
  • host/nn_os_sync.c compiled with the host build's flags (-fsyntax-only, with and without Tracy): no error. (At
    479a4f90, job fb3-1969-r3l, as an object with -ffunction-sections -fdata-sections, its .CRT$XLF section (not
    COMDAT) held the one relocation to obj_sets_thread_end; this round does not touch it.)
  • python -I -B tests/run_all.py --strict --timeout 1200 --changed origin/main (4 files changed, 37 checks read them; 8 at
    once; RX_CPM set, so the Vulkan headers were found): 32 passed, 7 failed, 3 skipped (Linux only). All seven fail on
    main @ 9dd41326 too, run by name in the same job with the same first error: test_crash_dialog (a
    UnicodeDecodeError reading a child's output), test_dev_ab ("'"Skip title screen", &opts.skip_title' not found" in
    the menu source), test_frame_overlap ("the frame ahead line is gone from the log or from the page"),
    test_linux_pressure ("texture_frame() no longer has: pool_pressed = true;"), test_present_queue_default ("srccut:
    '{TAB_DEBUG, "Present queue", ...' is not in gpu/vk/vk_menu.cpp"), test_target_reuse_memory ("'vmaGetAllocationInfo2'
    unexpectedly found"), test_vk_occlusion_queries ("no member named 'policy'" in its ring fixture). None fails on this
    branch alone.

Earlier, before the last two rounds' changes, the same change on main @ 51f5f9da (fff612ab), with main @ e0ef1b57 beside it:

  • python -I -B tests/test_obj_cache_size.py: passes on both. Here also with sets: obj_cache_entries (with sets
    and its near misses), the 4,000-object walk's last pass reaches the table 47 times unset, 4,000 times with
    HOS_OBJ_CACHE=0 and =256, once with =sets; every lookup the table's object over 200,000 random operations; the
    log line.
  • python -I -B tests/test_multiwait_wake.py: passes on both. It now allows __builtin_ctz under mw_lock (the
    compiler's, one instruction, used to walk a set's ways); its other checks and controls are unchanged.
  • python -I -B tests/run_all.py --strict --changed origin/main (35 checks read the 4 changed files): 31 passed, 6
    failed, 3 skipped (Linux only). The six (test_crash_dialog, test_dev_ab, test_frame_overlap,
    test_linux_pressure, test_present_queue_default, test_vk_occlusion_queries) also fail in the whole suite on
    main @ e0ef1b57 on this PC.
  • The micro-benchmark in Measurements.

Still to test:

  • Windows 11, this PC (i7-9700, RTX 4060 (NVIDIA 610.88)): Castle Town's market, 60 s, this PR with HOS_OBJ_CACHE=sets
    and unset, interleaved, expecting fewer misses in the sync: object cache line with sets, fps and frame times within
    the runs' spread or better, and unset the same as main (by hand)
  • Linux: build the host and run python3 -I -B tests/test_obj_cache_size.py, expecting it to pass (by hand)
  • Linux and Windows with other CPUs (an AMD CPU, 6 or fewer logical processors), and the Steam Deck: Castle Town's
    market for two minutes with HOS_OBJ_CACHE=sets against unset, expecting frame times within the runs' spread or
    better (asked of the Test Farm)
  • this PC: main @ 9dd41326 against this head unset and with HOS_OBJ_CACHE=sets, interleaved and warmed: the time to
    make the game's objects at a cold start, resident memory after the threads' churn, and the Market's frame times
    (by hand)
  • this PC: the identity check's cost against main: tests/test_obj_cache_size.py --bench twice each, interleaved,
    main @ 9dd41326 (its own test file), 479a4f90's check (sequence, kind, key, sequence) and this head's (one word),
    the last two with this head's test file (its --bench built without the per-load watch), expecting this head unset
    within the runs' spread of main's unset and below 479a4f90's (by hand: held for the PC's queue of run-alone jobs,
    fb3-1969-bench-r4)

Measurements

Verdict: micro-benchmark only, not measured in play yet: on a walk of 4,000 mutexes among 60,000 objects, the sets
took 9.8 and 10.6 ns a lookup against 14.2 and 13.6 ns for main's default (16,384 entries of four ways) and 11.2 and
11.0 ns for 65,536 entries. HOS_OBJ_CACHE=sets ships off (unset keeps main's default) until a measurement in play
chooses it.

The identity check (micro-benchmark). Round two's check (sequence, kind, key, sequence; 479a4f90) against round
one's (kind, then key; 9baebe0d), both with round two's test file, on i7-9700, Windows 11, llvm-mingw clang 23.1.5
-O2: python -I -B tests/test_obj_cache_size.py --bench (ns a lookup for the walk below, best of 7), twice each,
interleaved, in one timing-cpu job of the PC's job queue (fb3-1969-bench-r3). Median of the two runs (range):

ns a lookup (best of 7), two runs Before, kind then key: this PR @ 9baebe0d After, sequence, kind, key, sequence: this PR @ 479a4f90
HOS_OBJ_CACHE=0 (the fixed 1,024 entries) 28.15 (28.0 to 28.3) 31.4 (31.1 to 31.7)
HOS_OBJ_CACHE=4096 30.2 (30.1 to 30.3) 32.3 (32.2 to 32.4)
unset (16,384 entries, four ways) 16.7 (16.7 to 16.7) 19.45 (19.3 to 19.6)
HOS_OBJ_CACHE=65536 13.65 (13.6 to 13.7) 16.85 (16.7 to 17.0)
HOS_OBJ_CACHE=sets 15.9 (15.8 to 16.0) 18.5 (18.5 to 18.5)

The sequence check cost about 3 ns a lookup in every mode, the default included (+16 % unset), and left sets only
about 5 % ahead of unset. That harness calls a watch function after every atomic load of the cut code, in --bench
too, so a check with more loads also paid for more calls. This head reads one word in one load (a lookup unset makes 2
atomic loads, the mode word and that word, where 479a4f90 made 5) and its --bench is built without the watch; this
head against main @ 9dd41326 and 479a4f90 is held for the PC's run-alone queue (fb3-1969-bench-r4): Still to test.
main itself has not been measured with this harness yet.

Earlier figures. Not measured again since the last three rounds' changes (the identity check above; without sets
objects are made one at a time again, as on main): the figures below are of fff612ab, both arms with that build's
chunk allocator and its key-then-kind check. Cold creation and resident memory after thread churn (main, this head
unset, this head with sets) are not measured yet: Still to test.

Baseline: main's cache sizes as they ran in that PR build (their code unchanged there), so both columns are one build
and one run of tests/test_obj_cache_size.py --bench.
Setup: Windows 11, i7-9700, llvm-mingw clang 23.1.5, -O2; python -I -B tests/test_obj_cache_size.py --bench (the
real table, caches and lookup cut from the source, one thread): ns a lookup for a walk of 4,000 mutexes at a 0x80
stride among 60,000 other objects, best of 7, run twice in one timing-cpu job of the PC's job queue, at fff612ab (this
change on main @ 51f5f9da; the rebases since, now onto 6e1521a7, changed none of its lines: git range-diff "=").

ns a lookup (best of 7), two runs Before, main's sizes: this PR @ fff612ab After, HOS_OBJ_CACHE=sets: this PR @ fff612ab
unset (16,384 entries, four ways) against sets (16,384 entries, 2,048 sets of 8) 14.2, 13.6 9.8, 10.6
HOS_OBJ_CACHE=65536 11.2, 11.0
HOS_OBJ_CACHE=4096 25.6, 25.9
HOS_OBJ_CACHE=0 (the fixed 1,024 entries) 23.0, 22.9
bench: HOS_OBJ_CACHE 0     : 23.0 ns a lookup (a walk of 4000 mutexes among 60000 objects; best of 7)
bench: HOS_OBJ_CACHE 4096  : 25.6 ns a lookup (a walk of 4000 mutexes among 60000 objects; best of 7)
bench: HOS_OBJ_CACHE unset : 14.2 ns a lookup (a walk of 4000 mutexes among 60000 objects; best of 7)
bench: HOS_OBJ_CACHE 65536 : 11.2 ns a lookup (a walk of 4000 mutexes among 60000 objects; best of 7)
bench: HOS_OBJ_CACHE sets  : 9.8 ns a lookup (a walk of 4000 mutexes among 60000 objects; best of 7)
bench: HOS_OBJ_CACHE 0     : 22.9 ns a lookup (a walk of 4000 mutexes among 60000 objects; best of 7)
bench: HOS_OBJ_CACHE 4096  : 25.9 ns a lookup (a walk of 4000 mutexes among 60000 objects; best of 7)
bench: HOS_OBJ_CACHE unset : 13.6 ns a lookup (a walk of 4000 mutexes among 60000 objects; best of 7)
bench: HOS_OBJ_CACHE 65536 : 11.0 ns a lookup (a walk of 4000 mutexes among 60000 objects; best of 7)
bench: HOS_OBJ_CACHE sets  : 10.6 ns a lookup (a walk of 4000 mutexes among 60000 objects; best of 7)

Risks

Off by default: unset, main's 16,384-entry cache runs as before, objects made one at a time as before. What changes
for everyone: every cache and find_unlocked check an object's identity with its identity word (the fix above), and
an object that cannot be allocated ends the process with a message instead of a crash. The check's cost is on every
cached lookup, sets or not: a hit reads one word of the object (main reads its key and its kind, two loads) and makes
the word to compare from the key and kind (a shift, an or and a test of the key's top bits); every change of an
object's kind (under table_lock) adds one store. Round two's check measured about 3 ns a lookup over round one's in
every mode (Measurements); this one against main is not measured yet (Still to test). An object is 8 bytes larger
(344 to 352 bytes on Windows: the word). A guest address of 2^60 or more (none on the Switch, whose addresses are under
2^39) is never a cache hit: its lookup takes table_lock every time.
With HOS_OBJ_CACHE=sets each host thread that looks an object up holds 128 KB (the default's 256 KB not made), freed by
the thread's exit callback (the image's TLS callback on Windows: it runs for every thread that ends, the game's and
the host's, and does nothing for one without a cache), and objects are made in chunks of 1,024. A wrong entry would
hand a lookup another object; an entry is only taken while its object is still that address's of that kind
(obj_is), and tests/test_obj_cache_size.py compares every lookup with the table over 200,000 random operations and
reassigns the object a lookup reads at every point of the lookup. Turn it off by leaving HOS_OBJ_CACHE unset; a
revert is enough. Players notice nothing.

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).
  • 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.
  • A check under tests/ that reads changed source is updated, not weakened.
  • Host code builds on Windows (llvm-mingw) and Linux, with UTF-8 paths.
  • 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.
## Summary Every `nn::os` mutex, event or queue call looks its host object up by guest address (`obj_nockpt` in `host/nn_os_sync.c`), first in a per-thread cache. #1781 made that cache 16,384 entries of four ways (`HOS_OBJ_CACHE`). `exp/x86-asm-sync` solved the same problem in another shape, and the two were never measured against each other. This PR adds that shape beside #1781's, off unless `HOS_OBJ_CACHE=sets`: - the same 16,384 entries as 2,048 sets of 8 ways, a set one 64-byte line of 8 tags and the 8 objects' numbers (128 KB a thread, in place of the default sized cache's 256 KB, made at its first lookup and freed as the thread ends). The cache is the host thread's: its pointer a thread-local, and its release the thread's own exit, whatever fiber it runs then (on Windows the image's TLS callback, `DLL_THREAD_DETACH`; on Linux a pthread key's destructor). Not a fiber's slot (FLS): a guest fiber is a Win32 fiber (`hos_thread.c`) that the game may delete while the thread that ran it runs on, and its slot's callback would free the cache under that thread; - an address may sit in either of two sets (two hashes), which makes a full set rarer, though both of an address's sets can fill while others are empty; a miss replaces a way of the address's tag whose object is no longer its, else an empty way, else the next of the two sets' 16 ways in turn (a per-thread counter); - to fit 8 entries in a line, with `sets` the table numbers its objects as it makes them (`obj_new_locked`, chunks of 1,024 that are never freed, as objects never are), so an entry is a 32-bit number (`obj_of_id`). Without `sets` (unset, a size, `0`, or `HOS_NO_OBJ_CACHE`), each object is made alone, as on `main`. Every lookup that reads an object without `table_lock` (every cache and `find_unlocked`) checks it with `obj_is`, which reads its key and kind as one identity, of one life, in one load: beside its key and kind an object keeps one 64-bit word of both (`ident`: the kind in the top 4 bits over a key under 2^60, a guest address; 0 for a tombstone or a larger key), written after every change of its kind (`obj_kind_set`, release) and compared once (acquire) with the word the lookup makes from its key and kind (`obj_ident`). An object is given another identity (a spare taken by another address, a tombstone made again at its address) only as a tombstone, whose word is 0, and gets its new word last. Read separately, as before, a kind read before a reassignment could pair with a key read after it: an object given to another address and then back to its own as another kind passed for the old kind (a mutex lookup returning the address's new event), and a spare could reach `holder_ready` under `mw_lock` for the wrong address. `main`'s pointer caches (the sized default, the fixed one) and `find_unlocked` read the same two fields separately and use the same check, so they are fixed here too. What a lookup returns was that address's object of that kind when it read it, and stays so until the guest drops it: objects are never freed, and a dropped one is reused only after 30 s in the graves and not while a condition variable waiter or a multi-wait holds it (`bury_locked`). `HOS_NO_OBJ_CACHE` and `HOS_OBJ_CACHE` are read once into one atomic word (`obj_cache_mode_read`; two first lookups at once both store the same value), as is `HOS_SYNC_TABLE_LOCK`; `obj_cache_entries` returns `OBJ_CACHE_SETS` for `sets`, so the lookup's path compares no string. A thread whose sets cannot be had (allocation or owner) goes without and does not ask again. An object that cannot be had ends the process through `hos_fatal`, not a write through NULL. The hits and misses count in the existing `sync: object cache` log line. The switch's row in `gpu/vk/switches.def` (now a string) names the new value. From 3cf76270 on `exp/x86-asm-sync`, where it replaced the 1,024-entry cache (its `HOS_OBJ_CACHE_SMALL` is `HOS_OBJ_CACHE=0` on `main`). Read first: `obj_ident`, `obj_kind_set`, `obj_is` and the two reassignments in `obj_lookup`; `obj_lookup_sets`, `obj_set_find`, `obj_sets_thread_end`, `obj_new_locked` and `obj_nockpt` in `host/nn_os_sync.c`; `tests/test_obj_cache_size.py` (its `reuse`, `churn` and `fiber` modes). ## Testing **Tested:** Windows 11, i7-9700, RTX 4060 (NVIDIA 610.88), llvm-mingw clang 23.1.5; game-free checks and a micro-benchmark only. This change has not run in the game on this PC yet, with the switch on or off (Still to test). Each check ran in a hidden console of its own, at most 20 minutes, in two jobs of the PC's job queue (fb3-1969-r4l, fb3-1969-r4b), on this branch on `main` @ 9dd41326 (head 712ee7e7; the first job's tests ran at c02418f4, the same tree before its commit message was reworded): - `python -I -B tests/test_obj_cache_size.py` (with `--cc` llvm-mingw clang, with `CC`, and with the compiler found from `LONGSHOT_MINGW`): passes. "ok: obj_cache_entries"; the 4,000-object walk's last pass reaches the table 47 times unset, 4,000 times with `=0` and `=256`, once with `=sets` (the sets case must stay at 10 or under); every lookup the table's object over 200,000 random operations; the log line; `reuse` with `sets`, unset, `0` and `HOS_NO_OBJ_CACHE=1`: the object reassigned before the lookup and after each of its loads (3 with `sets`, 2 unset and `0`, 6 or 7 unlocked), three ways, "ok"; `churn`: "64 threads made and ended: 2621511 bytes in allocations of 64 KB and up before, 2621511 after"; `fiber`: "2752646 bytes ... with it, 2752646 once the fiber is deleted (the thread running on); the thread's next 4000 lookups reached the table 0 times; 0 bytes written in freed memory; 2621511 after the thread ended, 2621511 before it". With the same test file (b7554f7b) on 479a4f90's source (the sequence check) it passes too; on 9baebe0d's source (kind, then key) both cases fail: `reuse` in all four on the third way ("returned the object now its own address's again, of another kind (another's in between) ... reassigned right after the lookup's load 2" unset and `0`, load 3 with `sets`, load 6 unlocked), and `fiber` ("2719878 ... with it, 2588743 once the fiber is deleted ...; reached the table 4000 times; 30053 bytes written in freed memory"). - `python -I -B tests/test_multiwait_wake.py`: passes ("targeted: ok", "all: ok", the source check; `__atomic_thread_fence` no longer allowed under `mw_lock`: none is made; the FLS calls no longer). - `python -I -B tests/test_cv_handoff_threads.py` (it builds the real struct and `obj_lookup`'s branch for an address made again): passes. - `sizeof(Obj)` (the struct as the tests cut it, llvm-mingw clang for x86-64 Windows): 344 bytes on `main` and at 479a4f90, 352 at this head (the identity word: the fields before `owner` 40 bytes, now 48). - `host/nn_os_sync.c` compiled with the host build's flags (`-fsyntax-only`, with and without Tracy): no error. (At 479a4f90, job fb3-1969-r3l, as an object with `-ffunction-sections -fdata-sections`, its `.CRT$XLF` section (not COMDAT) held the one relocation to `obj_sets_thread_end`; this round does not touch it.) - `python -I -B tests/run_all.py --strict --timeout 1200 --changed origin/main` (4 files changed, 37 checks read them; 8 at once; `RX_CPM` set, so the Vulkan headers were found): 32 passed, 7 failed, 3 skipped (Linux only). All seven fail on `main` @ 9dd41326 too, run by name in the same job with the same first error: `test_crash_dialog` (a `UnicodeDecodeError` reading a child's output), `test_dev_ab` ("'"Skip title screen", &opts.skip_title' not found" in the menu source), `test_frame_overlap` ("the frame ahead line is gone from the log or from the page"), `test_linux_pressure` ("texture_frame() no longer has: pool_pressed = true;"), `test_present_queue_default` ("srccut: '{TAB_DEBUG, "Present queue", ...' is not in gpu/vk/vk_menu.cpp"), `test_target_reuse_memory` ("'vmaGetAllocationInfo2' unexpectedly found"), `test_vk_occlusion_queries` ("no member named 'policy'" in its ring fixture). None fails on this branch alone. Earlier, before the last two rounds' changes, the same change on `main` @ 51f5f9da (fff612ab), with `main` @ e0ef1b57 beside it: - `python -I -B tests/test_obj_cache_size.py`: passes on both. Here also with `sets`: `obj_cache_entries` (with `sets` and its near misses), the 4,000-object walk's last pass reaches the table 47 times unset, 4,000 times with `HOS_OBJ_CACHE=0` and `=256`, once with `=sets`; every lookup the table's object over 200,000 random operations; the log line. - `python -I -B tests/test_multiwait_wake.py`: passes on both. It now allows `__builtin_ctz` under `mw_lock` (the compiler's, one instruction, used to walk a set's ways); its other checks and controls are unchanged. - `python -I -B tests/run_all.py --strict --changed origin/main` (35 checks read the 4 changed files): 31 passed, 6 failed, 3 skipped (Linux only). The six (`test_crash_dialog`, `test_dev_ab`, `test_frame_overlap`, `test_linux_pressure`, `test_present_queue_default`, `test_vk_occlusion_queries`) also fail in the whole suite on `main` @ e0ef1b57 on this PC. - The micro-benchmark in Measurements. **Still to test:** - Windows 11, this PC (i7-9700, RTX 4060 (NVIDIA 610.88)): Castle Town's market, 60 s, this PR with `HOS_OBJ_CACHE=sets` and unset, interleaved, expecting fewer misses in the `sync: object cache` line with `sets`, fps and frame times within the runs' spread or better, and unset the same as `main` (by hand) - Linux: build the host and run `python3 -I -B tests/test_obj_cache_size.py`, expecting it to pass (by hand) - Linux and Windows with other CPUs (an AMD CPU, 6 or fewer logical processors), and the Steam Deck: Castle Town's market for two minutes with `HOS_OBJ_CACHE=sets` against unset, expecting frame times within the runs' spread or better (asked of the Test Farm) - this PC: `main` @ 9dd41326 against this head unset and with `HOS_OBJ_CACHE=sets`, interleaved and warmed: the time to make the game's objects at a cold start, resident memory after the threads' churn, and the Market's frame times (by hand) - this PC: the identity check's cost against `main`: `tests/test_obj_cache_size.py --bench` twice each, interleaved, `main` @ 9dd41326 (its own test file), 479a4f90's check (sequence, kind, key, sequence) and this head's (one word), the last two with this head's test file (its `--bench` built without the per-load watch), expecting this head unset within the runs' spread of `main`'s unset and below 479a4f90's (by hand: held for the PC's queue of run-alone jobs, fb3-1969-bench-r4) ## Measurements **Verdict:** micro-benchmark only, not measured in play yet: on a walk of 4,000 mutexes among 60,000 objects, the sets took 9.8 and 10.6 ns a lookup against 14.2 and 13.6 ns for `main`'s default (16,384 entries of four ways) and 11.2 and 11.0 ns for 65,536 entries. `HOS_OBJ_CACHE=sets` ships off (unset keeps `main`'s default) until a measurement in play chooses it. **The identity check (micro-benchmark).** Round two's check (sequence, kind, key, sequence; 479a4f90) against round one's (kind, then key; 9baebe0d), both with round two's test file, on i7-9700, Windows 11, llvm-mingw clang 23.1.5 `-O2`: `python -I -B tests/test_obj_cache_size.py --bench` (ns a lookup for the walk below, best of 7), twice each, interleaved, in one `timing-cpu` job of the PC's job queue (fb3-1969-bench-r3). Median of the two runs (range): | ns a lookup (best of 7), two runs | Before, kind then key: this PR @ 9baebe0d | After, sequence, kind, key, sequence: this PR @ 479a4f90 | |---|---|---| | `HOS_OBJ_CACHE=0` (the fixed 1,024 entries) | 28.15 (28.0 to 28.3) | 31.4 (31.1 to 31.7) | | `HOS_OBJ_CACHE=4096` | 30.2 (30.1 to 30.3) | 32.3 (32.2 to 32.4) | | unset (16,384 entries, four ways) | 16.7 (16.7 to 16.7) | 19.45 (19.3 to 19.6) | | `HOS_OBJ_CACHE=65536` | 13.65 (13.6 to 13.7) | 16.85 (16.7 to 17.0) | | `HOS_OBJ_CACHE=sets` | 15.9 (15.8 to 16.0) | 18.5 (18.5 to 18.5) | The sequence check cost about 3 ns a lookup in every mode, the default included (+16 % unset), and left `sets` only about 5 % ahead of unset. That harness calls a watch function after every atomic load of the cut code, in `--bench` too, so a check with more loads also paid for more calls. This head reads one word in one load (a lookup unset makes 2 atomic loads, the mode word and that word, where 479a4f90 made 5) and its `--bench` is built without the watch; this head against `main` @ 9dd41326 and 479a4f90 is held for the PC's run-alone queue (fb3-1969-bench-r4): Still to test. `main` itself has not been measured with this harness yet. **Earlier figures.** Not measured again since the last three rounds' changes (the identity check above; without `sets` objects are made one at a time again, as on `main`): the figures below are of fff612ab, both arms with that build's chunk allocator and its key-then-kind check. Cold creation and resident memory after thread churn (`main`, this head unset, this head with `sets`) are not measured yet: Still to test. Baseline: `main`'s cache sizes as they ran in that PR build (their code unchanged there), so both columns are one build and one run of `tests/test_obj_cache_size.py --bench`. Setup: Windows 11, i7-9700, llvm-mingw clang 23.1.5, `-O2`; `python -I -B tests/test_obj_cache_size.py --bench` (the real table, caches and lookup cut from the source, one thread): ns a lookup for a walk of 4,000 mutexes at a 0x80 stride among 60,000 other objects, best of 7, run twice in one `timing-cpu` job of the PC's job queue, at fff612ab (this change on `main` @ 51f5f9da; the rebases since, now onto 6e1521a7, changed none of its lines: `git range-diff` "="). | ns a lookup (best of 7), two runs | Before, main's sizes: this PR @ fff612ab | After, HOS_OBJ_CACHE=sets: this PR @ fff612ab | |---|---|---| | unset (16,384 entries, four ways) against `sets` (16,384 entries, 2,048 sets of 8) | 14.2, 13.6 | 9.8, 10.6 | | `HOS_OBJ_CACHE=65536` | 11.2, 11.0 | | | `HOS_OBJ_CACHE=4096` | 25.6, 25.9 | | | `HOS_OBJ_CACHE=0` (the fixed 1,024 entries) | 23.0, 22.9 | | ``` bench: HOS_OBJ_CACHE 0 : 23.0 ns a lookup (a walk of 4000 mutexes among 60000 objects; best of 7) bench: HOS_OBJ_CACHE 4096 : 25.6 ns a lookup (a walk of 4000 mutexes among 60000 objects; best of 7) bench: HOS_OBJ_CACHE unset : 14.2 ns a lookup (a walk of 4000 mutexes among 60000 objects; best of 7) bench: HOS_OBJ_CACHE 65536 : 11.2 ns a lookup (a walk of 4000 mutexes among 60000 objects; best of 7) bench: HOS_OBJ_CACHE sets : 9.8 ns a lookup (a walk of 4000 mutexes among 60000 objects; best of 7) bench: HOS_OBJ_CACHE 0 : 22.9 ns a lookup (a walk of 4000 mutexes among 60000 objects; best of 7) bench: HOS_OBJ_CACHE 4096 : 25.9 ns a lookup (a walk of 4000 mutexes among 60000 objects; best of 7) bench: HOS_OBJ_CACHE unset : 13.6 ns a lookup (a walk of 4000 mutexes among 60000 objects; best of 7) bench: HOS_OBJ_CACHE 65536 : 11.0 ns a lookup (a walk of 4000 mutexes among 60000 objects; best of 7) bench: HOS_OBJ_CACHE sets : 10.6 ns a lookup (a walk of 4000 mutexes among 60000 objects; best of 7) ``` ## Risks Off by default: unset, `main`'s 16,384-entry cache runs as before, objects made one at a time as before. What changes for everyone: every cache and `find_unlocked` check an object's identity with its identity word (the fix above), and an object that cannot be allocated ends the process with a message instead of a crash. The check's cost is on every cached lookup, sets or not: a hit reads one word of the object (`main` reads its key and its kind, two loads) and makes the word to compare from the key and kind (a shift, an or and a test of the key's top bits); every change of an object's kind (under `table_lock`) adds one store. Round two's check measured about 3 ns a lookup over round one's in every mode (Measurements); this one against `main` is not measured yet (Still to test). An object is 8 bytes larger (344 to 352 bytes on Windows: the word). A guest address of 2^60 or more (none on the Switch, whose addresses are under 2^39) is never a cache hit: its lookup takes `table_lock` every time. With `HOS_OBJ_CACHE=sets` each host thread that looks an object up holds 128 KB (the default's 256 KB not made), freed by the thread's exit callback (the image's TLS callback on Windows: it runs for every thread that ends, the game's and the host's, and does nothing for one without a cache), and objects are made in chunks of 1,024. A wrong entry would hand a lookup another object; an entry is only taken while its object is still that address's of that kind (`obj_is`), and `tests/test_obj_cache_size.py` compares every lookup with the table over 200,000 random operations and reassigns the object a lookup reads at every point of the lookup. Turn it off by leaving `HOS_OBJ_CACHE` unset; a revert is enough. Players notice nothing. ## Checklist ### Ready for review - [x] 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). - [ ] 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 - [ ] A switch added or changed: it ships at the default the measurements chose, with its row in `gpu/vk/switches.def`. - [x] A check under `tests/` that reads changed source is updated, not weakened. - [ ] Host code builds on Windows (llvm-mingw) and Linux, with UTF-8 paths. - [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.
perf(os): HOS_OBJ_CACHE=sets, the per-thread sync object cache as 2048 sets of 8 ways of object numbers (experiment)
Some checks failed
PR description / Description follows the template (pull_request_target) Successful in 14s
Website / Website on Cloudflare (pull_request_target) Has been skipped
CI / Conventional Commit title (pull_request) Has been cancelled
CI / Python source checks (ubuntu-24.04, part 1/2) (pull_request) Has been cancelled
CI / Python source checks (ubuntu-24.04, part 2/2) (pull_request) Has been cancelled
CI / SPIR-V fixtures (global narrow load/store, direct surface atomics, F2I immediate) (pull_request) Has been cancelled
CI / Workflow lint (actionlint) (pull_request) Has been cancelled
CI / Website build and checks (pull_request) Has been cancelled
Guide screenshots / Guide images and local UI source review (pull_request) Has been cancelled
Third-party parts / As their notes say (pull_request) Has been cancelled
Third-party parts / The same as upstream (pull_request) Has been cancelled
b8c6309640
Beside #1781's sized cache (HOS_OBJ_CACHE, 16384 entries of four ways by default), a second shape of the same 16384
entries to compare it with: 2048 sets of 8 ways, a set one 64-byte line of 8 tags and the 8 objects' numbers (128 KB a
thread, made at its first lookup), an address in either of two sets by two hashes, so no set overflows before the
cache is nearly full; a miss takes a stale way of the address's, else an empty one, else one at random. The table now
numbers its objects as it makes them (chunks of 1024, never freed, as objects never are), so an entry is a 32-bit
number and a set holds eight. Counted in the existing sync: object cache log line. Off unless HOS_OBJ_CACHE=sets,
which obj_cache_entries reads with the size, once (OBJ_CACHE_SETS; no string compare on the lookup's path).

tests/test_obj_cache_size.py runs the sets too: obj_cache_entries("sets") and its near misses, the 4,000-object walk's
later passes miss under 2 %, every lookup is the table's object over 200,000 random operations with objects re-made,
buried and reused, the log line; --bench times it beside the other sizes. tests/test_multiwait_wake.py allows
__builtin_ctz under mw_lock (the compiler's, one instruction: the set's ways are found with it).

From 3cf76270 on exp/x86-asm-sync, where it replaced the 1,024-entry cache (its HOS_OBJ_CACHE_SMALL switch is main's
HOS_OBJ_CACHE=0 now).

Assisted-by: Claude Code
BeanBot left a comment

Recommendation: Request changes. The new cache can return a recycled object for the wrong guest address and leaks its allocation when a thread exits. The synthetic walk is faster, but the benchmark leaves the allocator change shared by both sides and there are no in-game results.

Adds an opt-in, two-choice cache for guest synchronization objects. It cuts misses on the synthetic walk from 47 to 1 per 4,000 lookups and uses a 128 KiB heap cache per participating thread instead of the default's 256 KiB. It adds ID decoding and changes object allocation even for players who leave the switch unset.

@fix will do (15)

  1. L1 host/nn_os_sync.c:350: The cache validates key and kind from separate object lifetimes. (more on the line)
  2. L2 host/nn_os_sync.c:360: Thread exit loses the only reference to each sets allocation, and the code discards the original pointer immediately after alignment. (more on the line)
  3. L3 host/nn_os_sync.c:394: The new mode dispatch reads the inherited plain shared off and entries variables while another thread can initialize them. (more on the line)
  4. L4 host/nn_os_sync.c:445: The new allocator can return NULL if both the chunk and individual allocation fail. (more on the line)
  5. L5 (minor) host/nn_os_sync.c:352: A matching tag with a different live key is treated as a stale entry. (more on the line)
  6. L6 (minor) host/nn_os_sync.c:361: After a cache allocation fails, every lookup on that thread retries the same 128 KiB allocation, including lookups under mw_lock. (more on the line)
  7. L7 (minor) host/nn_os_sync.c:325: The inherited cache logger now also serves sets. It reads obj_cache_logged_ns and obj_cache_made through volatile accesses while other threads update them with Interlocked operations. Use atomic loads for those reads. The looks and misses totals also need a coherent snapshot so a thread adding a batch between the two exchanges cannot put its misses in a different reporting interval from its lookups.
  8. L8 (minor) host/nn_os_sync.c:373: Hits return before checking whether the thread's counters are due. (more on the line)
  9. L9 tests/test_obj_cache_size.py:140: The sets case also passes if the new dispatch line is removed. (more on the line)
  10. L10 tests/test_obj_cache_size.py:143: The fixture claims to exercise a buried object's memory being reused at a different address. (more on the line)
  11. L11 (minor) tests/test_obj_cache_size.py:221: The child process inherits HOS_KEEP_DEAD_OBJECTS and HOS_SYNC_TABLE_LOCK, both of which are read by the extracted code. (more on the line)
  12. L12 (minor) tests/test_obj_cache_size.py:194: Direct runs default to clang, and RUN_ALL_ARGS forces clang for suite runs. A supported Linux setup with CC=gcc and no clang cannot run this check through either entry point without overriding the test command. Honor CC when --cc is absent, use an available supported compiler as the fallback, and remove the suite's hardcoded override.
  13. L13 (minor) gpu/vk/switches.def:135: The switch row still declares int although sets is an accepted literal. (more on the line)
  14. L14 (minor): The Risks section calls the sets cache 128 KB more per thread. It replaces the 256 KiB sized-cache heap allocation. The description also promises that sets fill only near overall capacity and that victims are random. Both candidate sets can fill while others are empty, and the code rotates through victim slots. Correct the memory comparison and describe the replacement policy in both the PR and the source comment.
  15. L15 (its part an edit can do) host/nn_os_sync.c:445: Keep single-object allocation outside sets mode so the experimental switch also gates the allocator change. Measure the selected mode's gain in play.

For the author (1) (a run, a measurement or a decision)

  • L15 (minor) host/nn_os_sync.c:445: The same-build microbenchmark isolates cache shape but cannot establish the cost of chunk allocation for everyone. (more on the line)

Checks: Still running: CI / Conventional Commit title (pull_request), CI / Python source checks (ubuntu-24.04, part 1/2) (pull_request), CI / Python source checks (ubuntu-24.04, part 2/2) (pull_request), CI / SPIR-V fixtures (global narrow load/store, direct surface atomics, F2I immediate) (pull_request), CI / Workflow lint (actionlint) (pull_request), CI / Website build and checks (pull_request), Guide screenshots / Guide images and local UI source review (pull_request), Third-party parts / As their notes say (pull_request), Third-party parts / The same as upstream (pull_request).

More: does it help, other systems, switches, what was read

Does it help: Unproven in play. The PR reports 9.8 and 10.6 ns per lookup with sets versus 14.2 and 13.6 ns unset, 22–31% lower, on an i7-9700 with llvm-mingw Clang at fff612ab. Each figure is the best of seven, and both modes use the PR's allocator. There are no matched runs against the merge base for frame times, 1% lows, startup time or RAM.

Other systems: The lookup serves mutexes, condition variables, events, timers, semaphores, queues, reader/writer locks and WaitAny, including system events used by video, vsync and applets. Cache misses can allocate while holding table_lock, including on a WaitAny scan under mw_lock. The host path is shared across GPU vendors on Windows and Linux, including Steam Deck. The source shows no new virtual-core scheduling point and no direct change to the renderer API, shaders, VRAM, saves, input mappings or build flags. Gameplay, wake timing and memory pressure remain unverified.

Switches: HOS_OBJ_CACHE adds the exact value sets. Unset, empty and invalid values still select the 16,384-entry four-way cache. Numeric sizes and 0 retain their meanings, and HOS_NO_OBJ_CACHE still overrides cache lookup. The row lists sets but labels the value type int. Keeping the existing cache as the comparison default makes sense for the experiment. The shared allocator and shipping default still need evidence on supported hardware.

Reviewed: b8c6309640 against 6e1521a70a: Read host/nn_os_sync.c, tests/test_obj_cache_size.py and tests/test_multiwait_wake.py in full. Read the full diff and commit, AGENTS.md, CONTRIBUTING.md, the Forgejo template and developer standards. Read the changed switches.def row and related rows, the switch reader, test runner, Linux lock shim, thread exit and host event callers. Skimmed the rest of the switch catalog, related source checks and recorded object-cache runs. The older PR test heads were unavailable locally. There was no local review checklist or adjacent Tooling checkout. No translations or generated files changed.; nothing run (a code review).

Checked and fine:

  • The chunk index is bounded, IDs cannot wrap at the limit, and the new ID survives both object reset paths. Chunk pointers are published with release/acquire, and each object's ID is initialized before that object is published.
  • Tag and way indexes stay within their arrays. Both trailing-zero calls have nonzero inputs, and the builtin adds no explicit BMI instruction requirement. Short switch strings are safe under short-circuit evaluation.
  • The WaitAny source check only adds the compiler builtin to its allowlist. The existing negative controls remain, and the new lookup introduces no virtual-core checkpoint.
  • The diff contains authored source and synthetic data, with no game files, generated output, third-party or renderer API changes. The commit has an Assisted-by trailer, and the diff passes the whitespace check.

Testing: Read-only review. The author reports the two focused fixtures passing on Windows at f2c967b1 and seven broader-suite failures reproduced on its then-base. The author reports an unchanged rebase to b8c63096. Linux, gameplay and Forgejo CI results are pending.

Overlaps and merge order: No overlapping open PR was found in the supplied evidence or local history. #1781 is already on main. The description identifies exp/x86-asm-sync as the source of the experiment.


Automated code review by BeanBot (gpt-6.2-sol), on the pull request's opening. Nothing was run; a maintainer decides.

**Recommendation: Request changes.** The new cache can return a recycled object for the wrong guest address and leaks its allocation when a thread exits. The synthetic walk is faster, but the benchmark leaves the allocator change shared by both sides and there are no in-game results. Adds an opt-in, two-choice cache for guest synchronization objects. It cuts misses on the synthetic walk from 47 to 1 per 4,000 lookups and uses a 128 KiB heap cache per participating thread instead of the default's 256 KiB. It adds ID decoding and changes object allocation even for players who leave the switch unset. **`@fix` will do (15)** 1. **L1** `host/nn_os_sync.c:350`: The cache validates key and kind from separate object lifetimes. (more on the line) 2. **L2** `host/nn_os_sync.c:360`: Thread exit loses the only reference to each sets allocation, and the code discards the original pointer immediately after alignment. (more on the line) 3. **L3** `host/nn_os_sync.c:394`: The new mode dispatch reads the inherited plain shared off and entries variables while another thread can initialize them. (more on the line) 4. **L4** `host/nn_os_sync.c:445`: The new allocator can return NULL if both the chunk and individual allocation fail. (more on the line) 5. **L5** (minor) `host/nn_os_sync.c:352`: A matching tag with a different live key is treated as a stale entry. (more on the line) 6. **L6** (minor) `host/nn_os_sync.c:361`: After a cache allocation fails, every lookup on that thread retries the same 128 KiB allocation, including lookups under mw_lock. (more on the line) 7. **L7** (minor) `host/nn_os_sync.c:325`: The inherited cache logger now also serves sets. It reads obj_cache_logged_ns and obj_cache_made through volatile accesses while other threads update them with Interlocked operations. Use atomic loads for those reads. The looks and misses totals also need a coherent snapshot so a thread adding a batch between the two exchanges cannot put its misses in a different reporting interval from its lookups. 8. **L8** (minor) `host/nn_os_sync.c:373`: Hits return before checking whether the thread's counters are due. (more on the line) 9. **L9** `tests/test_obj_cache_size.py:140`: The sets case also passes if the new dispatch line is removed. (more on the line) 10. **L10** `tests/test_obj_cache_size.py:143`: The fixture claims to exercise a buried object's memory being reused at a different address. (more on the line) 11. **L11** (minor) `tests/test_obj_cache_size.py:221`: The child process inherits HOS_KEEP_DEAD_OBJECTS and HOS_SYNC_TABLE_LOCK, both of which are read by the extracted code. (more on the line) 12. **L12** (minor) `tests/test_obj_cache_size.py:194`: Direct runs default to clang, and RUN_ALL_ARGS forces clang for suite runs. A supported Linux setup with CC=gcc and no clang cannot run this check through either entry point without overriding the test command. Honor CC when --cc is absent, use an available supported compiler as the fallback, and remove the suite's hardcoded override. 13. **L13** (minor) `gpu/vk/switches.def:135`: The switch row still declares int although sets is an accepted literal. (more on the line) 14. **L14** (minor): The Risks section calls the sets cache 128 KB more per thread. It replaces the 256 KiB sized-cache heap allocation. The description also promises that sets fill only near overall capacity and that victims are random. Both candidate sets can fill while others are empty, and the code rotates through victim slots. Correct the memory comparison and describe the replacement policy in both the PR and the source comment. 15. **L15** (its part an edit can do) `host/nn_os_sync.c:445`: Keep single-object allocation outside sets mode so the experimental switch also gates the allocator change. Measure the selected mode's gain in play. **For the author (1)** (a run, a measurement or a decision) - **L15** (minor) `host/nn_os_sync.c:445`: The same-build microbenchmark isolates cache shape but cannot establish the cost of chunk allocation for everyone. (more on the line) **Checks:** Still running: `CI / Conventional Commit title (pull_request)`, `CI / Python source checks (ubuntu-24.04, part 1/2) (pull_request)`, `CI / Python source checks (ubuntu-24.04, part 2/2) (pull_request)`, `CI / SPIR-V fixtures (global narrow load/store, direct surface atomics, F2I immediate) (pull_request)`, `CI / Workflow lint (actionlint) (pull_request)`, `CI / Website build and checks (pull_request)`, `Guide screenshots / Guide images and local UI source review (pull_request)`, `Third-party parts / As their notes say (pull_request)`, `Third-party parts / The same as upstream (pull_request)`. <details> <summary>More: does it help, other systems, switches, what was read</summary> **Does it help:** Unproven in play. The PR reports 9.8 and 10.6 ns per lookup with sets versus 14.2 and 13.6 ns unset, 22–31% lower, on an i7-9700 with llvm-mingw Clang at fff612ab. Each figure is the best of seven, and both modes use the PR's allocator. There are no matched runs against the merge base for frame times, 1% lows, startup time or RAM. **Other systems:** The lookup serves mutexes, condition variables, events, timers, semaphores, queues, reader/writer locks and WaitAny, including system events used by video, vsync and applets. Cache misses can allocate while holding table_lock, including on a WaitAny scan under mw_lock. The host path is shared across GPU vendors on Windows and Linux, including Steam Deck. The source shows no new virtual-core scheduling point and no direct change to the renderer API, shaders, VRAM, saves, input mappings or build flags. Gameplay, wake timing and memory pressure remain unverified. **Switches:** HOS_OBJ_CACHE adds the exact value sets. Unset, empty and invalid values still select the 16,384-entry four-way cache. Numeric sizes and 0 retain their meanings, and HOS_NO_OBJ_CACHE still overrides cache lookup. The row lists sets but labels the value type int. Keeping the existing cache as the comparison default makes sense for the experiment. The shared allocator and shipping default still need evidence on supported hardware. **Reviewed:** b8c630964074 against 6e1521a70a32: Read host/nn_os_sync.c, tests/test_obj_cache_size.py and tests/test_multiwait_wake.py in full. Read the full diff and commit, AGENTS.md, CONTRIBUTING.md, the Forgejo template and developer standards. Read the changed switches.def row and related rows, the switch reader, test runner, Linux lock shim, thread exit and host event callers. Skimmed the rest of the switch catalog, related source checks and recorded object-cache runs. The older PR test heads were unavailable locally. There was no local review checklist or adjacent Tooling checkout. No translations or generated files changed.; nothing run (a code review). **Checked and fine:** - The chunk index is bounded, IDs cannot wrap at the limit, and the new ID survives both object reset paths. Chunk pointers are published with release/acquire, and each object's ID is initialized before that object is published. - Tag and way indexes stay within their arrays. Both trailing-zero calls have nonzero inputs, and the builtin adds no explicit BMI instruction requirement. Short switch strings are safe under short-circuit evaluation. - The WaitAny source check only adds the compiler builtin to its allowlist. The existing negative controls remain, and the new lookup introduces no virtual-core checkpoint. - The diff contains authored source and synthetic data, with no game files, generated output, third-party or renderer API changes. The commit has an Assisted-by trailer, and the diff passes the whitespace check. **Testing:** Read-only review. The author reports the two focused fixtures passing on Windows at f2c967b1 and seven broader-suite failures reproduced on its then-base. The author reports an unchanged rebase to b8c63096. Linux, gameplay and Forgejo CI results are pending. **Overlaps and merge order:** No overlapping open PR was found in the supplied evidence or local history. #1781 is already on main. The description identifies exp/x86-asm-sync as the source of the experiment. </details> --- <sub>Automated code review by BeanBot (gpt-6.2-sol), on the pull request's opening. Nothing was run; a maintainer decides.</sub>
@ -133,3 +133,3 @@
SW(HOS_NVN_WATCH_RT, diag, string, "off", "Logs who sets the render target at that guest address (hex), as guest call chains.")
SW(HOS_NVN_WATCH_TEX, diag, string, "off", "Logs who binds that texture handle (hex), as guest call chains, each once.")
SW(HOS_OBJ_CACHE, tune, int, "16384: each thread's cache of the host objects behind guest mutexes, events and queues has that many entries, four ways a set with a mixing hash, and a sync: object cache line is logged every 10 s", "<entries> (256 to 65536, a power of two): that many entries; =0: the fixed per-thread cache of 1024 direct-mapped entries, as before.")
SW(HOS_OBJ_CACHE, tune, int, "16384: each thread's cache of the host objects behind guest mutexes, events and queues has that many entries, four ways a set with a mixing hash, and a sync: object cache line is logged every 10 s", "<entries> (256 to 65536, a power of two): that many entries; =0: the fixed per-thread cache of 1024 direct-mapped entries, as before; =sets (experiment): 2048 sets of 8 ways of object numbers, an address in either of two sets, 128 KB a thread, to compare with the default.")
Member

L13 (minor): The switch row still declares int although sets is an accepted literal. Use string metadata for the mixed input and say that numeric entries are rounded up to a power of two.

**L13** (minor): The switch row still declares int although sets is an accepted literal. Use string metadata for the mixed input and say that numeric entries are rounded up to a power of two.
@ -308,6 +394,7 @@ static Obj *obj_nockpt(uint64_t key, int kind) {
static int off = -1, entries = -1;
Member

L3: The new mode dispatch reads the inherited plain shared off and entries variables while another thread can initialize them. Two threads entering their first object lookup together can race even if both compute the same values. Publish the resolved mode atomically or initialize it before any lookup thread starts. Apply the same treatment to locked_reads on the first concurrent table miss.

**L3**: The new mode dispatch reads the inherited plain shared off and entries variables while another thread can initialize them. Two threads entering their first object lookup together can race even if both compute the same values. Publish the resolved mode atomically or initialize it before any lookup thread starts. Apply the same treatment to locked_reads on the first concurrent table miss.
@ -304,0 +347,4 @@
for (unsigned m = obj_set_ways(s, tag); m; m &= m - 1) {
const unsigned i = (unsigned)__builtin_ctz(m);
Obj *o = obj_of_id(s->id[i]);
if (o && __atomic_load_n(&o->key, __ATOMIC_RELAXED) == key && __atomic_load_n(&o->kind, __ATOMIC_ACQUIRE) == kind)
Member

L1: The cache validates key and kind from separate object lifetimes. A cache entry for A can still point at a buried object after the table installs A's replacement. If a reader sees the old object's key A just before it is reassigned to B, then sees B's published kind, it can return B as A. The pointer cache has the same pattern. Validate a single published identity or use a versioned snapshot shared by the cache paths, and cover concurrent spare reuse in the test.

**L1**: The cache validates key and kind from separate object lifetimes. A cache entry for A can still point at a buried object after the table installs A's replacement. If a reader sees the old object's key A just before it is reassigned to B, then sees B's published kind, it can return B as A. The pointer cache has the same pattern. Validate a single published identity or use a versioned snapshot shared by the cache paths, and cover concurrent spare reuse in the test.
@ -304,0 +349,4 @@
Obj *o = obj_of_id(s->id[i]);
if (o && __atomic_load_n(&o->key, __ATOMIC_RELAXED) == key && __atomic_load_n(&o->kind, __ATOMIC_ACQUIRE) == kind)
return o;
*stale = i;
Member

L5 (minor): A matching tag with a different live key is treated as a stale entry. Two live addresses with the same low 32 bits and a shared candidate set can keep evicting each other even when there are empty ways. Prefer an empty way over an ambiguous tag match and only give stale priority to entries whose stored object can be shown to have changed. Add a forced tag-collision case.

**L5** (minor): A matching tag with a different live key is treated as a stale entry. Two live addresses with the same low 32 bits and a shared candidate set can keep evicting each other even when there are empty ways. Prefer an empty way over an ambiguous tag match and only give stale priority to entries whose stored object can be shown to have changed. Add a forced tag-collision case.
@ -304,0 +357,4 @@
static _Thread_local ObjSet *sets;
static _Thread_local unsigned victim;
if (!sets) {
void *raw = calloc(((size_t)1 << OBJ_SET_BITS) * sizeof(ObjSet) + 63, 1); /* (the thread's, for its life) */
Member

L2: Thread exit loses the only reference to each sets allocation, and the code discards the original pointer immediately after alignment. Each short-lived guest worker that makes a lookup leaves 128 KiB plus 63 bytes behind. Give the allocation a thread-exit owner on Windows and Linux and retain the allocation base for cleanup. Host-side event producers can enter this path too.

**L2**: Thread exit loses the only reference to each sets allocation, and the code discards the original pointer immediately after alignment. Each short-lived guest worker that makes a lookup leaves 128 KiB plus 63 bytes behind. Give the allocation a thread-exit owner on Windows and Linux and retain the allocation base for cleanup. Host-side event producers can enter this path too.
@ -304,0 +358,4 @@
static _Thread_local unsigned victim;
if (!sets) {
void *raw = calloc(((size_t)1 << OBJ_SET_BITS) * sizeof(ObjSet) + 63, 1); /* (the thread's, for its life) */
if (!raw) return obj_lookup(key, kind);
Member

L6 (minor): After a cache allocation fails, every lookup on that thread retries the same 128 KiB allocation, including lookups under mw_lock. During sustained memory pressure, a hot mutex loop becomes an allocator call plus a table lookup per access. Keep a per-thread fallback state or retry outside the hot lookup path.

**L6** (minor): After a cache allocation fails, every lookup on that thread retries the same 128 KiB allocation, including lookups under mw_lock. During sustained memory pressure, a hot mutex loop becomes an allocator call plus a table lookup per access. Keep a per-thread fallback state or retry outside the hot lookup path.
@ -304,0 +370,4 @@
obj_cache_tl_looks++;
Obj *o = obj_set_find(s1, tag, key, kind, &st1);
if (!o) o = obj_set_find(s2, tag, key, kind, &st2);
if (o) return o; /* (a hit: no shared write) */
Member

L8 (minor): Hits return before checking whether the thread's counters are due. A thread that warms a small cache and then hits indefinitely can account for none of those lookups in the reported miss rate. The sized cache shares this behavior. Schedule counter publication at the threshold on hits too, keeping the actual flush outside mw_lock, and test an all-hit stretch.

**L8** (minor): Hits return before checking whether the thread's counters are due. A thread that warms a small cache and then hits indefinitely can account for none of those lookups in the reported miss rate. The sized cache shares this behavior. Schedule counter publication at the threshold on hits too, keeping the actual flush outside mw_lock, and test an all-hit stretch.
@ -356,3 +443,3 @@
__atomic_store_n(&o->kind, 0, __ATOMIC_RELAXED);
} else {
o = (Obj *)calloc(1, sizeof *o);
o = obj_new_locked();
Member

L4: The new allocator can return NULL if both the chunk and individual allocation fail. The caller immediately initializes locks through that pointer, repeating the old unchecked allocation failure. Check the result and use the host's allocation failure reporting without introducing a retry or virtual-core wait while the lookup locks are held.

**L4**: The new allocator can return NULL if both the chunk and individual allocation fail. The caller immediately initializes locks through that pointer, repeating the old unchecked allocation failure. Check the result and use the host's allocation failure reporting without introducing a retry or virtual-core wait while the lookup locks are held.
Member

L15 (minor): The same-build microbenchmark isolates cache shape but cannot establish the cost of chunk allocation for everyone. For one new object, the first chunk requests hundreds of KiB under table_lock instead of a single object. Before merge, compare the actual base with head unset and head with sets, after warm-up in interleaved runs. Report cold and warm load times, RAM, cache counts, average FPS, 1% lows, median and p99 frame times in the Market and a heavy scene. The required author play run and Tier 1 Windows, Linux and Steam Deck results are still missing.

**L15** (minor): The same-build microbenchmark isolates cache shape but cannot establish the cost of chunk allocation for everyone. For one new object, the first chunk requests hundreds of KiB under table_lock instead of a single object. Before merge, compare the actual base with head unset and head with sets, after warm-up in interleaved runs. Report cold and warm load times, RAM, cache counts, average FPS, 1% lows, median and p99 frame times in the Market and a heavy scene. The required author play run and Tier 1 Windows, Linux and Steam Deck results are still missing.
@ -136,3 +138,3 @@
const int entries = obj_cache_entries(size);
if (!entries) require(last_pass > SLOTS / 2, "=0: the fixed cache misses a 4,000-object walk pass after pass");
else if (entries >= 16384) require(last_pass <= SLOTS / 50, "sized: the walk's later passes miss under 2 %");
else if (entries >= 16384 || entries == OBJ_CACHE_SETS) require(last_pass <= SLOTS / 50, "sized or sets: the walk's later passes miss under 2 %");
Member

L9: The sets case also passes if the new dispatch line is removed. The sentinel is nonzero, so the lookup falls into the sized cache with entries equal to 1. That variant always indexes the first four ways of its five-entry allocation, so the long walk fails today, but routing sets to the ordinary 16,384-entry default passes the same 80-miss bound and all later assertions. Give sets a workload or threshold that distinguishes it from the default and verify it rejects a dispatch that falls back to that default.

**L9**: The sets case also passes if the new dispatch line is removed. The sentinel is nonzero, so the lookup falls into the sized cache with entries equal to 1. That variant always indexes the first four ways of its five-entry allocation, so the long walk fails today, but routing sets to the ordinary 16,384-entry default passes the same 80-miss bound and all later assertions. Give sets a workload or threshold that distinguishes it from the default and verify it rejects a dispatch that falls back to that default.
@ -139,3 +140,4 @@
else if (entries >= 16384 || entries == OBJ_CACHE_SETS) require(last_pass <= SLOTS / 50, "sized or sets: the walk's later passes miss under 2 %");
else require(last_pass > SLOTS / 2, "a cache smaller than the walk misses again: the size is honoured");
/* objects changing under the cache: compared with the table itself */
enum { KEYS = 3000 };
Member

L10: The fixture claims to exercise a buried object's memory being reused at a different address. Its clock stays fixed through the random operations, and the later 20-second advance is below the 30-second grave delay. It exercises same-address reinitialization but never takes a spare. Force burial, advance past the delay, assert the old ID has been reassigned, then look up the old address and compare it with the table.

**L10**: The fixture claims to exercise a buried object's memory being reused at a different address. Its clock stays fixed through the random operations, and the later 20-second advance is below the 30-second grave delay. It exercises same-address reinitialization but never takes a spare. Force burial, advance past the delay, assert the old ID has been reassigned, then look up the old address and compare it with the table.
@ -218,2 +218,4 @@
runs = [("entries", None)] + ([("bench", v) for v in ("0", "4096", None, "65536", "sets")] if a.bench else
[("check", v) for v in (None, "0", "256", "sets")])
for mode, value in runs:
env = dict(os.environ)
Member

L11 (minor): The child process inherits HOS_KEEP_DEAD_OBJECTS and HOS_SYNC_TABLE_LOCK, both of which are read by the extracted code. For example, a developer's HOS_KEEP_DEAD_OBJECTS=1 would disable the grave path that the reuse check needs. Set or clear both explicitly for each fixture case.

**L11** (minor): The child process inherits HOS_KEEP_DEAD_OBJECTS and HOS_SYNC_TABLE_LOCK, both of which are read by the extracted code. For example, a developer's HOS_KEEP_DEAD_OBJECTS=1 would disable the grave path that the reuse check needs. Set or clear both explicitly for each fixture case.
futondestroyer left a comment

Recommendation: Request changes. The new sets cache can return a reused object for the wrong address and leaves its allocation behind on thread exit. Both blockers are covered by the existing exact-head review.

  • obj_set_find can read the original key before reassignment and the new object's kind after reassignment. The acquire on kind comes after the key read. An ID identifies the storage, which stays allocated while the spare can change owners. A false hit can reach holder_ready under mw_lock, where it can consult or consume the other object's event. The pointer caches already have the same key/kind pattern. Fix how the lookup obtains a consistent identity and protects the selected lifetime through use. Cover concurrent spare reuse and a same-kind replacement with controlled interleavings. The single-thread fixture's random operations establish neither condition.
  • Keep the allocation base and release the sets cache when its thread exits on both hosts. Verify repeated guest thread creation and exit with memory accounting. This cache alone can leave about 128 KiB per participating thread behind.

Does it help? The Windows i7-9700 fixture reports 9.8 and 10.6 ns per lookup with sets versus 14.2 and 13.6 ns with the default cache, best of seven, for a single-thread walk of 4,000 objects among 60,000 records. Both arms used this PR's object allocator. Chunk allocation runs even with sets unset or HOS_NO_OBJ_CACHE set. Compare exact main, candidate unset, and candidate sets for cold creation, resident memory after churn, and warmed gameplay frame times before choosing a default. Author gameplay is still pending.

Reviewed. b8c630964074ecbfeac68f5f95533828f933fe6a against 6e1521a70a32603ed60a8263e4072625885669b7. Reviewed all four changed files and the object table, reuse, cache, and multi-wait callers. The author reports passing focused Windows fixtures on f2c967b1 and seven broad-suite failures reproduced on its then-base. The merge bases differ in none of the four changed files. Exact-head CI and cross-platform gameplay evidence are pending. No PR code run in this review.

**Recommendation: Request changes.** The new sets cache can return a reused object for the wrong address and leaves its allocation behind on thread exit. Both blockers are covered by the [existing exact-head review](https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/1969#issuecomment-62343). - `obj_set_find` can read the original key before reassignment and the new object's kind after reassignment. The acquire on kind comes after the key read. An ID identifies the storage, which stays allocated while the spare can change owners. A false hit can reach `holder_ready` under `mw_lock`, where it can consult or consume the other object's event. The pointer caches already have the same key/kind pattern. Fix how the lookup obtains a consistent identity and protects the selected lifetime through use. Cover concurrent spare reuse and a same-kind replacement with controlled interleavings. The single-thread fixture's random operations establish neither condition. - Keep the allocation base and release the sets cache when its thread exits on both hosts. Verify repeated guest thread creation and exit with memory accounting. This cache alone can leave about 128 KiB per participating thread behind. **Does it help?** The Windows i7-9700 fixture reports 9.8 and 10.6 ns per lookup with `sets` versus 14.2 and 13.6 ns with the default cache, best of seven, for a single-thread walk of 4,000 objects among 60,000 records. Both arms used this PR's object allocator. Chunk allocation runs even with `sets` unset or `HOS_NO_OBJ_CACHE` set. Compare exact main, candidate unset, and candidate `sets` for cold creation, resident memory after churn, and warmed gameplay frame times before choosing a default. Author gameplay is still pending. **Reviewed.** `b8c630964074ecbfeac68f5f95533828f933fe6a` against `6e1521a70a32603ed60a8263e4072625885669b7`. Reviewed all four changed files and the object table, reuse, cache, and multi-wait callers. The author reports passing focused Windows fixtures on `f2c967b1` and seven broad-suite failures reproduced on its then-base. The merge bases differ in none of the four changed files. Exact-head CI and cross-platform gameplay evidence are pending. No PR code run in this review.
siantm force-pushed perf/obj-cache-sets from b8c6309640
Some checks failed
PR description / Description follows the template (pull_request_target) Successful in 14s
Website / Website on Cloudflare (pull_request_target) Has been skipped
CI / Conventional Commit title (pull_request) Has been cancelled
CI / Python source checks (ubuntu-24.04, part 1/2) (pull_request) Has been cancelled
CI / Python source checks (ubuntu-24.04, part 2/2) (pull_request) Has been cancelled
CI / SPIR-V fixtures (global narrow load/store, direct surface atomics, F2I immediate) (pull_request) Has been cancelled
CI / Workflow lint (actionlint) (pull_request) Has been cancelled
CI / Website build and checks (pull_request) Has been cancelled
Guide screenshots / Guide images and local UI source review (pull_request) Has been cancelled
Third-party parts / As their notes say (pull_request) Has been cancelled
Third-party parts / The same as upstream (pull_request) Has been cancelled
to 9baebe0d44
Some checks failed
Website / Website on Cloudflare (pull_request_target) Has been skipped
beanstalk/approval waiting for a BigBean's 👍 on the request
PR description / Description follows the template (pull_request_target) Successful in 23s
CI / Conventional Commit title (pull_request) Has been cancelled
CI / Python source checks (ubuntu-24.04, part 1/2) (pull_request) Has been cancelled
CI / Python source checks (ubuntu-24.04, part 2/2) (pull_request) Has been cancelled
CI / SPIR-V fixtures (global narrow load/store, direct surface atomics, F2I immediate) (pull_request) Has been cancelled
CI / Workflow lint (actionlint) (pull_request) Has been cancelled
CI / Website build and checks (pull_request) Has been cancelled
Guide screenshots / Guide images and local UI source review (pull_request) Has been cancelled
Third-party parts / As their notes say (pull_request) Has been cancelled
Third-party parts / The same as upstream (pull_request) Has been cancelled
2026-11-21 04:46:02 +00:00
Compare
Author
Contributor

Pushed 9baebe0d (rebased onto main @ f1f2dc5b): every object cache (sets, and main's sized and fixed caches) and find_unlocked read an object's kind before its key; a thread's sets are freed as it ends (Windows FLS, Linux pthread key); objects are made in chunks only with sets; the switches are read once through atomics; new reuse and churn modes in tests/test_obj_cache_size.py. Summary, Testing, Measurements (the figures predate this round; cold creation and memory after churn not measured yet) and Risks follow; the Test Farm line no longer holds a mention.

Pushed 9baebe0d (rebased onto `main` @ f1f2dc5b): every object cache (sets, and `main`'s sized and fixed caches) and `find_unlocked` read an object's kind before its key; a thread's sets are freed as it ends (Windows FLS, Linux pthread key); objects are made in chunks only with `sets`; the switches are read once through atomics; new `reuse` and `churn` modes in `tests/test_obj_cache_size.py`. Summary, Testing, Measurements (the figures predate this round; cold creation and memory after churn not measured yet) and Risks follow; the Test Farm line no longer holds a mention.
Author
Contributor

@futondestroyer, on your review, head 9baebe0d (rebased onto main @ f1f2dc5b; the PR's own commit changed only where main's new HOS_OCB_HOLD_MS row sits beside its switch row, a context conflict):

  • Consistent identity, lifetime through use: fixed in 9baebe0d. Every cache now checks an entry's object with obj_is: kind first (acquire), then key. A spare's reuse stores kind 0, the new key, then the new kind (release), so a lookup that reads the new kind reads the new key and one that reads the old life's kind (0 since it was dropped) fails. main's pointer caches (the sized default, the fixed one) and find_unlocked had the same key-then-kind order; the fix there is the same two loads swapped, so it is in this commit rather than a separate change. Lifetime: an object is never freed (the numbered chunks neither), and a dropped one is reused only after 30 s in the graves and never while a condition variable waiter or a multi-wait holds it (bury_locked), so what a lookup returns stays that address's until the guest drops it, as on main. Test (a72f1502): tests/test_obj_cache_size.py mode reuse, run with sets, unset and 0, controlled interleaving: the table gives the spare to a new address right after the lookup's first atomic read of the stale entry's object (the harness hooks each load), with the same kind and with another, and before the lookup as the control; it checks the spare (same number) went to the new address and the lookup returned the old address's own object. At a72f1502, without the fix, the same-kind case fails in all three caches ("a lookup of 0x5000001000 returned the spare now 0x70000de130's"); with it all pass.
  • Allocation base, freed on thread exit on both hosts: fixed in 9baebe0d: the base is kept and handed to an FLS slot's callback on Windows (ExitThread too) or a pthread key's destructor on Linux (the POSIX layer's ExitThread is pthread_exit), slot or key made once; a thread whose cache or owner cannot be had goes without and is not retried. Test: mode churn makes and ends 64 threads one after another, each looking 4,000 objects up, with calloc/free of 64 KB and up counted: live bytes after equal live bytes before. At a72f1502 it fails (2,588,743 bytes live before, 10,981,383 after: 64 caches of 128 KB left); at the head both are 2,588,743. (The sized default's per-thread cache still stays behind on main as its comment says; left for a separate change.)
  • Chunk allocation with sets unset / HOS_NO_OBJ_CACHE: avoided rather than stated: objects are numbered in chunks only with HOS_OBJ_CACHE=sets; otherwise each is made alone, as on main (9baebe0d).
  • Measurements (exact main, candidate unset, candidate sets: cold creation, resident memory after churn, warmed gameplay): not run. The fixture's --bench measures lookups only, and its old figures predate this round's changes (the description says so); the gameplay comparison is asked of the game runs on this PC, pending.

Checks: tests/test_obj_cache_size.py and tests/test_multiwait_wake.py pass; run_all.py --strict --changed origin/main: 31 passed, 7 failed, 3 skipped, all 7 failing on main @ f1f2dc5b too (Testing in the description).

@futondestroyer, on your [review](https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/1969#issuecomment-62904), head 9baebe0d (rebased onto `main` @ f1f2dc5b; the PR's own commit changed only where `main`'s new `HOS_OCB_HOLD_MS` row sits beside its switch row, a context conflict): - **Consistent identity, lifetime through use**: fixed in 9baebe0d. Every cache now checks an entry's object with `obj_is`: kind first (acquire), then key. A spare's reuse stores kind 0, the new key, then the new kind (release), so a lookup that reads the new kind reads the new key and one that reads the old life's kind (0 since it was dropped) fails. `main`'s pointer caches (the sized default, the fixed one) and `find_unlocked` had the same key-then-kind order; the fix there is the same two loads swapped, so it is in this commit rather than a separate change. Lifetime: an object is never freed (the numbered chunks neither), and a dropped one is reused only after 30 s in the graves and never while a condition variable waiter or a multi-wait holds it (`bury_locked`), so what a lookup returns stays that address's until the guest drops it, as on `main`. Test (a72f1502): `tests/test_obj_cache_size.py` mode `reuse`, run with `sets`, unset and `0`, controlled interleaving: the table gives the spare to a new address right after the lookup's first atomic read of the stale entry's object (the harness hooks each load), with the same kind and with another, and before the lookup as the control; it checks the spare (same number) went to the new address and the lookup returned the old address's own object. At a72f1502, without the fix, the same-kind case fails in all three caches ("a lookup of 0x5000001000 returned the spare now 0x70000de130's"); with it all pass. - **Allocation base, freed on thread exit on both hosts**: fixed in 9baebe0d: the base is kept and handed to an FLS slot's callback on Windows (ExitThread too) or a pthread key's destructor on Linux (the POSIX layer's ExitThread is `pthread_exit`), slot or key made once; a thread whose cache or owner cannot be had goes without and is not retried. Test: mode `churn` makes and ends 64 threads one after another, each looking 4,000 objects up, with calloc/free of 64 KB and up counted: live bytes after equal live bytes before. At a72f1502 it fails (2,588,743 bytes live before, 10,981,383 after: 64 caches of 128 KB left); at the head both are 2,588,743. (The sized default's per-thread cache still stays behind on `main` as its comment says; left for a separate change.) - **Chunk allocation with `sets` unset / `HOS_NO_OBJ_CACHE`**: avoided rather than stated: objects are numbered in chunks only with `HOS_OBJ_CACHE=sets`; otherwise each is made alone, as on `main` (9baebe0d). - **Measurements** (exact `main`, candidate unset, candidate `sets`: cold creation, resident memory after churn, warmed gameplay): not run. The fixture's `--bench` measures lookups only, and its old figures predate this round's changes (the description says so); the gameplay comparison is asked of the game runs on this PC, pending. Checks: `tests/test_obj_cache_size.py` and `tests/test_multiwait_wake.py` pass; `run_all.py --strict --changed origin/main`: 31 passed, 7 failed, 3 skipped, all 7 failing on `main` @ f1f2dc5b too (Testing in the description).
Author
Contributor

BeanBot's review, head 9baebe0d:

  • L1 (62331), L2 (62332), L15 (62342, the edit): as in the reply to the maintainer (kind-then-key in every cache and find_unlocked; sets freed at thread exit; chunks only with sets). The play measurement L15 asks for is pending.
  • L3 (62333): HOS_NO_OBJ_CACHE/HOS_OBJ_CACHE read once into one atomic word (obj_cache_mode_read), HOS_SYNC_TABLE_LOCK likewise.
  • L4 (62334): an object that cannot be allocated ends the process through hos_fatal, no retry, no core wait.
  • L6 (62336): a thread whose sets fail is marked and not retried.
  • L9 (62338): the sets walk must miss at most 10 of 4,000 on its last pass (the four-way default misses 47).
  • L10 (62339): mode reuse buries, ages past 30 s, asserts the spare's number went to the new address and compares the old address's lookup.
  • L11 (62340): each run clears HOS_KEEP_DEAD_OBJECTS and HOS_SYNC_TABLE_LOCK too.
  • L12: --cc, then CC, then LONGSHOT_MINGW's clang, then cc/clang/gcc; RUN_ALL_ARGS no longer forces clang.
  • L13 (62341), L14: the row is a string, says rounded up and the memory (128 KB against the default's 256 KB); the comment and description give the replacement policy as the code does it.
  • Not changed (minor): L5 (a tag shared by two live keys still counts as stale), L7 (the logger's volatile reads, shared with main's sized cache), L8 (an all-hit stretch flushes no counts, as main's sized cache).
BeanBot's [review](https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/1969#issuecomment-62343), head 9baebe0d: - L1 ([62331](https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/1969#issuecomment-62331)), L2 ([62332](https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/1969#issuecomment-62332)), L15 ([62342](https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/1969#issuecomment-62342), the edit): as in the reply to the maintainer (kind-then-key in every cache and `find_unlocked`; sets freed at thread exit; chunks only with `sets`). The play measurement L15 asks for is pending. - L3 ([62333](https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/1969#issuecomment-62333)): `HOS_NO_OBJ_CACHE`/`HOS_OBJ_CACHE` read once into one atomic word (`obj_cache_mode_read`), `HOS_SYNC_TABLE_LOCK` likewise. - L4 ([62334](https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/1969#issuecomment-62334)): an object that cannot be allocated ends the process through `hos_fatal`, no retry, no core wait. - L6 ([62336](https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/1969#issuecomment-62336)): a thread whose sets fail is marked and not retried. - L9 ([62338](https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/1969#issuecomment-62338)): the sets walk must miss at most 10 of 4,000 on its last pass (the four-way default misses 47). - L10 ([62339](https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/1969#issuecomment-62339)): mode `reuse` buries, ages past 30 s, asserts the spare's number went to the new address and compares the old address's lookup. - L11 ([62340](https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/1969#issuecomment-62340)): each run clears `HOS_KEEP_DEAD_OBJECTS` and `HOS_SYNC_TABLE_LOCK` too. - L12: `--cc`, then `CC`, then `LONGSHOT_MINGW`'s clang, then cc/clang/gcc; `RUN_ALL_ARGS` no longer forces clang. - L13 ([62341](https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/1969#issuecomment-62341)), L14: the row is a string, says rounded up and the memory (128 KB against the default's 256 KB); the comment and description give the replacement policy as the code does it. - Not changed (minor): L5 (a tag shared by two live keys still counts as stale), L7 (the logger's volatile reads, shared with `main`'s sized cache), L8 (an all-hit stretch flushes no counts, as `main`'s sized cache).
Author
Contributor

@Beanstalk This adds a switch, HOS_OBJ_CACHE, off by default: with HOS_OBJ_CACHE=sets, the per-thread cache of guest sync objects is 2048 sets of 8 ways (host/nn_os_sync.c). It has not run in the game yet. Please play Castle Town Market for two minutes, the PR against main on the same PC, on the PR's side with HOS_OBJ_CACHE=sets and with it unset, on a Linux PC with an AMD processor, a Windows PC with an AMD processor, a PC with 6 or fewer logical processors, and the Steam Deck. No crash; set, fewer misses in the log's sync object cache line than unset and frame time p99 equal to unset or better, within the run-to-run spread; unset, frame times as main's. Please report the object cache line's miss counts.

@Beanstalk This adds a switch, HOS_OBJ_CACHE, off by default: with HOS_OBJ_CACHE=sets, the per-thread cache of guest sync objects is 2048 sets of 8 ways (host/nn_os_sync.c). It has not run in the game yet. Please play Castle Town Market for two minutes, the PR against main on the same PC, on the PR's side with HOS_OBJ_CACHE=sets and with it unset, on a Linux PC with an AMD processor, a Windows PC with an AMD processor, a PC with 6 or fewer logical processors, and the Steam Deck. No crash; set, fewer misses in the log's sync object cache line than unset and frame time p99 equal to unset or better, within the run-to-run spread; unset, frame times as main's. Please report the object cache line's miss counts.
Member

@siantm only members of BigBeans can start the farm.

One of them can approve this request by reacting 👍 to it or to this comment. This request was for 9baebe0, and the PR has changed since (479a4f9), so a 👍 no longer approves it. Comment @Beanstalk again to ask for the new commit.

@siantm only members of `BigBeans` can start the farm. ~~One of them can approve this request by reacting 👍 to it or to this comment.~~ This request was for `9baebe0`, and the PR has changed since (`479a4f9`), so a 👍 no longer approves it. Comment `@Beanstalk` again to ask for the new commit.
Author
Contributor

The three renderer checks (test_present_queue_default, test_target_reuse_memory, test_vk_occlusion_queries) did not compile in the earlier run (no Vulkan headers on that job's path); rerun with them in job fb3-vk-1, at the head 9baebe0d and on main @ f1f2dc5b: all three fail at both with the same first error, so they fail on main too; Tested's sentence and counts corrected.

The three renderer checks (`test_present_queue_default`, `test_target_reuse_memory`, `test_vk_occlusion_queries`) did not compile in the earlier run (no Vulkan headers on that job's path); rerun with them in job fb3-vk-1, at the head 9baebe0d and on `main` @ f1f2dc5b: all three fail at both with the same first error, so they fail on `main` too; Tested's sentence and counts corrected.
futondestroyer left a comment

Recommendation: Request changes. The revised cache still needs coherent object identity and a Windows cleanup lifetime that matches its owner.

  1. The cache pointer belongs to the host thread, but the new release callback follows the Windows fiber that registered the allocation. The game can delete a guest fiber while the host thread and its thread-local pointer remain active. That leaves the cache pointer referring to released memory. Use the same owner for lookup state and its cleanup. The thread churn fixture uses ordinary threads and does not cover the guest fiber lifecycle. Windows FLS callback documentation · Windows fiber storage documentation
  2. obj_is still reads kind and key independently. Reading kind first fixes the particular reuse ordering in the new fixture, but it can still combine a kind observed before a reassignment with a key observed afterward. The new predicate also runs in the default pointer cache and the unlocked table path. Validate a coherent lifetime before returning a cache hit, and extend the regression coverage to check reuse across the full lookup.

The parameter cache now publishes atomically, allocation failure falls back without repeated cache allocation, and chunk allocation is limited to sets mode. The author reports Windows fixture coverage at the new head. Earlier single-thread cache timing favored sets on that workload. Production memory and frame-time evidence and Linux validation remain open. Defer the existing Market request until the source issues are fixed. Author follow-up · Existing Farm request

Reviewed. 9baebe0d44c1bf9f46b67569fed9524240731fce against f1f2dc5bd2821cef061798ba40a043d281ac1707. Reviewed the interdiff, the four changed-file paths, and the object table, reclamation, thread and fiber owners. Both source-only Graphify maps verified and queried. Source only.

**Recommendation: Request changes.** The revised cache still needs coherent object identity and a Windows cleanup lifetime that matches its owner. 1. The cache pointer belongs to the host thread, but the new release callback follows the Windows fiber that registered the allocation. The game can delete a guest fiber while the host thread and its thread-local pointer remain active. That leaves the cache pointer referring to released memory. Use the same owner for lookup state and its cleanup. The thread churn fixture uses ordinary threads and does not cover the guest fiber lifecycle. [Windows FLS callback documentation](https://learn.microsoft.com/en-us/windows/win32/api/winnt/nc-winnt-pfls_callback_function) · [Windows fiber storage documentation](https://learn.microsoft.com/en-us/windows/win32/procthread/fibers) 2. `obj_is` still reads kind and key independently. Reading kind first fixes the particular reuse ordering in the new fixture, but it can still combine a kind observed before a reassignment with a key observed afterward. The new predicate also runs in the default pointer cache and the unlocked table path. Validate a coherent lifetime before returning a cache hit, and extend the regression coverage to check reuse across the full lookup. The parameter cache now publishes atomically, allocation failure falls back without repeated cache allocation, and chunk allocation is limited to sets mode. The author reports Windows fixture coverage at the new head. Earlier single-thread cache timing favored sets on that workload. Production memory and frame-time evidence and Linux validation remain open. Defer the existing Market request until the source issues are fixed. [Author follow-up](https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/1969#issuecomment-63560) · [Existing Farm request](https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/1969#issuecomment-63562) **Reviewed.** `9baebe0d44c1bf9f46b67569fed9524240731fce` against `f1f2dc5bd2821cef061798ba40a043d281ac1707`. Reviewed the interdiff, the four changed-file paths, and the object table, reclamation, thread and fiber owners. Both source-only Graphify maps verified and queried. Source only.
@ -197,0 +206,4 @@
object is never freed, and a dropped one is reused only after 30 s in the graves, not while a condition variable
waiter or a multi-wait holds it (bury_locked) */
static inline int obj_is(Obj *o, uint64_t key, int kind) {
return __atomic_load_n(&o->kind, __ATOMIC_ACQUIRE) == kind && __atomic_load_n(&o->key, __ATOMIC_RELAXED) == key;
Member

The kind-first check still samples two separate properties across object reuse. The cache hit needs one coherent identity for the object's current lifetime, including the default cache and unlocked table readers.

The kind-first check still samples two separate properties across object reuse. The cache hit needs one coherent identity for the object's current lifetime, including the default cache and unlocked table readers.
@ -304,0 +380,4 @@
}
static int obj_sets_own(void *raw) { /* 1: raw is freed as this thread ends */
return InitOnceExecuteOnce(&obj_sets_once, obj_sets_slot_make, NULL, NULL) && obj_sets_slot != FLS_OUT_OF_INDEXES &&
FlsSetValue(obj_sets_slot, raw);
Member

This registers a thread-local allocation with a fiber-local destructor. Deleting a guest fiber can free the allocation while the host thread still holds its cache pointer. Keep the pointer and its cleanup at the same lifetime and cover Windows guest fiber teardown. The thread-churn fixture does not exercise that owner.

This registers a thread-local allocation with a fiber-local destructor. Deleting a guest fiber can free the allocation while the host thread still holds its cache pointer. Keep the pointer and its cleanup at the same lifetime and cover Windows guest fiber teardown. The thread-churn fixture does not exercise that owner.
siantm force-pushed perf/obj-cache-sets from 9baebe0d44
Some checks failed
Website / Website on Cloudflare (pull_request_target) Has been skipped
beanstalk/approval waiting for a BigBean's 👍 on the request
PR description / Description follows the template (pull_request_target) Successful in 23s
CI / Conventional Commit title (pull_request) Has been cancelled
CI / Python source checks (ubuntu-24.04, part 1/2) (pull_request) Has been cancelled
CI / Python source checks (ubuntu-24.04, part 2/2) (pull_request) Has been cancelled
CI / SPIR-V fixtures (global narrow load/store, direct surface atomics, F2I immediate) (pull_request) Has been cancelled
CI / Workflow lint (actionlint) (pull_request) Has been cancelled
CI / Website build and checks (pull_request) Has been cancelled
Guide screenshots / Guide images and local UI source review (pull_request) Has been cancelled
Third-party parts / As their notes say (pull_request) Has been cancelled
Third-party parts / The same as upstream (pull_request) Has been cancelled
to 479a4f906a
Some checks failed
Website / Website on Cloudflare (pull_request) Blocked by required conditions
PR description / Description follows the template (pull_request_target) Successful in 4s
beanstalk/approval waiting for a BigBean's 👍 on the request
Website / Website on Cloudflare (pull_request_target) Has been skipped
CI / Conventional Commit title (pull_request) Has been cancelled
CI / Python source checks (ubuntu-24.04, part 1/2) (pull_request) Has been cancelled
CI / Python source checks (ubuntu-24.04, part 2/2) (pull_request) Has been cancelled
CI / SPIR-V fixtures (global narrow load/store, direct surface atomics, F2I immediate) (pull_request) Has been cancelled
CI / Workflow lint (actionlint) (pull_request) Has been cancelled
CI / Website build and checks (pull_request) Has been cancelled
Guide screenshots / Guide images and local UI source review (pull_request) Has been cancelled
Third-party parts / As their notes say (pull_request) Has been cancelled
Third-party parts / The same as upstream (pull_request) Has been cancelled
2026-11-21 10:56:07 +00:00
Compare
Author
Contributor

Pushed 479a4f90 (rebased onto main @ 9dd41326, the earlier commits unchanged): a sets cache is now its host thread's and freed by the thread's exit callback (the image's TLS callback on Windows), not a fiber's FLS slot (03ea7d4a); every lookup without table_lock reads an object's key and kind as one identity through a per-object sequence (a seqlock, 479a4f90); tests/test_obj_cache_size.py gains the fiber mode and reassigns the object at every point of a lookup in reuse, now also on the unlocked table path (28f27eed). Summary, Testing (the new base), Measurements and Risks (the check's cost) follow.

Pushed 479a4f90 (rebased onto `main` @ 9dd41326, the earlier commits unchanged): a sets cache is now its host thread's and freed by the thread's exit callback (the image's TLS callback on Windows), not a fiber's FLS slot (03ea7d4a); every lookup without `table_lock` reads an object's key and kind as one identity through a per-object sequence (a seqlock, 479a4f90); `tests/test_obj_cache_size.py` gains the `fiber` mode and reassigns the object at every point of a lookup in `reuse`, now also on the unlocked table path (28f27eed). Summary, Testing (the new base), Measurements and Risks (the check's cost) follow.
Author
Contributor

@futondestroyer, on your review, head 479a4f90 (rebased onto main @ 9dd41326; git range-diff: the three earlier commits unchanged, no conflict: #2012's spin_begin change is elsewhere in the file):

  • Cache pointer and its release on one owner (65540): fixed in 03ea7d4a. Both are now the host thread's: the pointer stays a thread-local, and the FLS slot is gone; on Windows the image's TLS callback (DLL_THREAD_DETACH, run by the loader on the ending thread with its thread-locals still there, whatever fiber it runs, ExitThread too) frees it from its base; on Linux the pthread key's destructor, as before. Keeping the pointer in FLS instead would give each guest fiber its own 128 KB and a FlsGetValue call per lookup, and a fiber can resume on another thread (hos_thread.c, fibers), so the thread is the owner that matches. Test (28f27eed): tests/test_obj_cache_size.py mode fiber (Windows): a thread makes its cache on a guest fiber, switches back, DeleteFibers it and looks the same 4,000 objects up again; freed allocations of 64 KB and up are kept filled with 0xA5. At 28f27eed (and at 9baebe0d itself with the new test file) it fails: "2719878 bytes ... with it, 2588743 once the fiber is deleted (the thread running on); the thread's next 4000 lookups reached the table 4000 times; 30053 bytes written in freed memory". At the head: 2719878 after the deletion, 0 lookups to the table, 0 bytes written, and 2588743 after the thread ended (= before). churn (64 threads) still passes.
  • One coherent identity (65541): fixed in 479a4f90, a seqlock per object. obj_lookup steps the object's new seq to odd (then a release fence) before it gives it another identity (a spare taken, a tombstone made again at its address) and back to even (release) after; obj_is reads seq (acquire), kind, key, an acquire fence, seq again, and takes the pair only if seq was even and unchanged. The sets cache, the sized default, the fixed cache and find_unlocked all go through obj_is. Test (28f27eed): mode reuse now reassigns the object the lookup will read through the table's own paths before the lookup and right after each of its atomic loads in turn (every load the cut code makes outside table_lock), three ways (another address, same kind; another address, another kind; another address and then back to its own as another kind), with sets, unset, 0 and HOS_NO_OBJ_CACHE=1 (the unlocked table path); the lookup must return the table's object of that address and kind, or the old one only if it read its identity wholly before the change. At 28f27eed and at 9baebe0d all four fail on the third way, e.g. unset "a lookup of 0x70009ce770 (a mutex) returned the object now its own address's again, of another kind ... reassigned right after the lookup's load 2"; at the head every position passes (6 loads with sets, 5 unset and 0, 9 to 10 unlocked).
  • Cost of the check (Risks): on a hit two more loads of the object's first fields and a fence that is a compiler barrier on x86-64. Its micro-benchmark against 9baebe0d's check is held for the PC's run-alone queue; pending.
  • Production memory, frame times, Linux: not run here; the game runs on this PC (main, this head unset, this head with sets) are asked again at this head of the person who does them, pending. The deferred Market request to the Test Farm is handed back with this head now that the two source points are fixed; it is not resent here.

Checks (Testing in the description): tests/test_obj_cache_size.py (three ways of choosing the compiler) and tests/test_multiwait_wake.py pass; host/nn_os_sync.c compiles with the host build's flags, with and without Tracy, and its object has the callback in .CRT$XLF (not COMDAT); run_all.py --strict --changed origin/main: 32 passed, 7 failed, 3 skipped, all 7 failing on main @ 9dd41326 too with the same first error (named in Testing).

@futondestroyer, on your [review](https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/1969#issuecomment-65542), head 479a4f90 (rebased onto `main` @ 9dd41326; `git range-diff`: the three earlier commits unchanged, no conflict: #2012's `spin_begin` change is elsewhere in the file): - **Cache pointer and its release on one owner** ([65540](https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/1969#issuecomment-65540)): fixed in 03ea7d4a. Both are now the host thread's: the pointer stays a thread-local, and the FLS slot is gone; on Windows the image's TLS callback (`DLL_THREAD_DETACH`, run by the loader on the ending thread with its thread-locals still there, whatever fiber it runs, `ExitThread` too) frees it from its base; on Linux the pthread key's destructor, as before. Keeping the pointer in FLS instead would give each guest fiber its own 128 KB and a `FlsGetValue` call per lookup, and a fiber can resume on another thread (`hos_thread.c`, fibers), so the thread is the owner that matches. Test (28f27eed): `tests/test_obj_cache_size.py` mode `fiber` (Windows): a thread makes its cache on a guest fiber, switches back, `DeleteFiber`s it and looks the same 4,000 objects up again; freed allocations of 64 KB and up are kept filled with 0xA5. At 28f27eed (and at 9baebe0d itself with the new test file) it fails: "2719878 bytes ... with it, 2588743 once the fiber is deleted (the thread running on); the thread's next 4000 lookups reached the table 4000 times; 30053 bytes written in freed memory". At the head: 2719878 after the deletion, 0 lookups to the table, 0 bytes written, and 2588743 after the thread ended (= before). `churn` (64 threads) still passes. - **One coherent identity** ([65541](https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/1969#issuecomment-65541)): fixed in 479a4f90, a seqlock per object. `obj_lookup` steps the object's new `seq` to odd (then a release fence) before it gives it another identity (a spare taken, a tombstone made again at its address) and back to even (release) after; `obj_is` reads seq (acquire), kind, key, an acquire fence, seq again, and takes the pair only if seq was even and unchanged. The sets cache, the sized default, the fixed cache and `find_unlocked` all go through `obj_is`. Test (28f27eed): mode `reuse` now reassigns the object the lookup will read through the table's own paths before the lookup and right after each of its atomic loads in turn (every load the cut code makes outside `table_lock`), three ways (another address, same kind; another address, another kind; another address and then back to its own as another kind), with `sets`, unset, `0` and `HOS_NO_OBJ_CACHE=1` (the unlocked table path); the lookup must return the table's object of that address and kind, or the old one only if it read its identity wholly before the change. At 28f27eed and at 9baebe0d all four fail on the third way, e.g. unset "a lookup of 0x70009ce770 (a mutex) returned the object now its own address's again, of another kind ... reassigned right after the lookup's load 2"; at the head every position passes (6 loads with sets, 5 unset and `0`, 9 to 10 unlocked). - **Cost of the check** (Risks): on a hit two more loads of the object's first fields and a fence that is a compiler barrier on x86-64. Its micro-benchmark against 9baebe0d's check is held for the PC's run-alone queue; pending. - **Production memory, frame times, Linux**: not run here; the game runs on this PC (main, this head unset, this head with `sets`) are asked again at this head of the person who does them, pending. The deferred Market request to the Test Farm is handed back with this head now that the two source points are fixed; it is not resent here. Checks (Testing in the description): `tests/test_obj_cache_size.py` (three ways of choosing the compiler) and `tests/test_multiwait_wake.py` pass; `host/nn_os_sync.c` compiles with the host build's flags, with and without Tracy, and its object has the callback in `.CRT$XLF` (not COMDAT); `run_all.py --strict --changed origin/main`: 32 passed, 7 failed, 3 skipped, all 7 failing on `main` @ 9dd41326 too with the same first error (named in Testing).
Author
Contributor

@Beanstalk This adds a switch, HOS_OBJ_CACHE, off by default: with HOS_OBJ_CACHE=sets, the per-thread cache of guest sync objects is 2048 sets of 8 ways (host/nn_os_sync.c). It has not run in the game yet. This head is on main 9dd41326: please compare with main at that merge base. Please start from the market-entrance save and play Castle Town Market for two minutes, the PR against main on the same PC, on the PR's side with HOS_OBJ_CACHE=sets and with it unset, on a Linux PC with an AMD processor, a Windows PC with an AMD processor, a PC with 6 or fewer logical processors, and the Steam Deck. No crash or hang; set, fewer misses in the log's "sync: object cache" line than unset and frame time p99 equal to unset or better, within the run-to-run spread; unset, frame times as main's. Please report the object cache line's miss counts.

@Beanstalk This adds a switch, HOS_OBJ_CACHE, off by default: with HOS_OBJ_CACHE=sets, the per-thread cache of guest sync objects is 2048 sets of 8 ways (host/nn_os_sync.c). It has not run in the game yet. This head is on main 9dd41326: please compare with main at that merge base. Please start from the market-entrance save and play Castle Town Market for two minutes, the PR against main on the same PC, on the PR's side with HOS_OBJ_CACHE=sets and with it unset, on a Linux PC with an AMD processor, a Windows PC with an AMD processor, a PC with 6 or fewer logical processors, and the Steam Deck. No crash or hang; set, fewer misses in the log's "sync: object cache" line than unset and frame time p99 equal to unset or better, within the run-to-run spread; unset, frame times as main's. Please report the object cache line's miss counts.
Member

@siantm only members of BigBeans can start the farm.

One of them can approve this request by reacting 👍 to it or to this comment. This request was for 479a4f9, and the PR has changed since (712ee7e), so a 👍 no longer approves it. Comment @Beanstalk again to ask for the new commit.

@siantm only members of `BigBeans` can start the farm. ~~One of them can approve this request by reacting 👍 to it or to this comment.~~ This request was for `479a4f9`, and the PR has changed since (`712ee7e`), so a 👍 no longer approves it. Comment `@Beanstalk` again to ask for the new commit.
The harness set an object's kind to 0 itself to make a tombstone (the reuse mode's drops and the check mode's
finalizations); it now calls drop, cut from host/nn_os_sync.c with the table, so a tombstone is whatever drop makes. The
reuse mode's watch (a call after every atomic load of the cut code) is left out of the --bench build (OBJ_BENCH): it
measured the watch with the loads, a call for each, which made a check with more loads look dearer than it is in the
game, where nothing follows a load.

tests/test_obj_cache_size.py passes as before; reuse and fiber still fail on 9baebe0d's source.

Assisted-by: Claude Code
perf(os): an object's key and kind are one word, which obj_is reads with one load (in place of the sequence)
Some checks are pending
CI / Conventional Commit title (pull_request) Blocked by required conditions
CI / Python source checks (ubuntu-24.04, part 1/2) (pull_request) Blocked by required conditions
CI / Python source checks (ubuntu-24.04, part 2/2) (pull_request) Blocked by required conditions
CI / SPIR-V fixtures (global narrow load/store, direct surface atomics, F2I immediate) (pull_request) Blocked by required conditions
CI / Workflow lint (actionlint) (pull_request) Blocked by required conditions
CI / Website build and checks (pull_request) Blocked by required conditions
Guide screenshots / Guide images and local UI source review (pull_request) Blocked by required conditions
Third-party parts / As their notes say (pull_request) Blocked by required conditions
Third-party parts / The same as upstream (pull_request) Blocked by required conditions
PR description / Description follows the template (pull_request_target) Successful in 4s
beanstalk/approval waiting for a BigBean's 👍 on the request
Website / Website on Cloudflare (pull_request_target) Has been skipped
712ee7e76b
obj_is read a sequence, the kind, the key and the sequence again (four loads and an acquire fence) to take the key and
kind of one life; the micro-benchmark (built with the test's call after each load) put that at about 3 ns a lookup in
every cache mode, the default included. An object now keeps its key and kind as one 64-bit word as well (ident: the
kind in the top 4 bits over a key under 2^60, a guest address; 0 for a tombstone or a larger key), and obj_is compares
that word, read once (acquire), with the one the lookup makes from its key and kind: one load, so a kind never pairs
with another identity's key. Every write of a kind goes through obj_kind_set, which writes the word after it (release);
an object gets another identity only as a tombstone, whose word is 0, and gets the new word last, so the sequence, its
steps and the fences go. An object with a key of 2^60 or more is never a cache hit (its word is 0) and is found under
table_lock. An object grows by 8 bytes (the word; the sequence's 4 went). tests/test_multiwait_wake.py no longer allows
__atomic_thread_fence under mw_lock (none is made).

tests/test_obj_cache_size.py reuse (sets, unset, 0, HOS_NO_OBJ_CACHE=1), fiber, churn and check pass.

Assisted-by: Claude Code
Author
Contributor

Pushed 712ee7e7 (on main @ 9dd41326, the earlier commits unchanged): obj_is now reads an object's key and kind as one 64-bit word in one load, in place of round two's sequence check (sequence, kind, key, sequence), which the micro-benchmark put at about 3 ns a lookup in every cache mode (712ee7e7); the object cache test makes its tombstones through drop and builds --bench without its per-load watch (b7554f7b). Description: Summary (the identity word), Testing (jobs fb3-1969-r4l and fb3-1969-r4b), Measurements (the sequence check's figures; this head against main held for the run-alone queue) and Risks (the check's cost, 8 bytes an object).

Pushed 712ee7e7 (on `main` @ 9dd41326, the earlier commits unchanged): `obj_is` now reads an object's key and kind as one 64-bit word in one load, in place of round two's sequence check (sequence, kind, key, sequence), which the micro-benchmark put at about 3 ns a lookup in every cache mode (712ee7e7); the object cache test makes its tombstones through `drop` and builds `--bench` without its per-load watch (b7554f7b). Description: Summary (the identity word), Testing (jobs fb3-1969-r4l and fb3-1969-r4b), Measurements (the sequence check's figures; this head against `main` held for the run-alone queue) and Risks (the check's cost, 8 bytes an object).
Author
Contributor

@futondestroyer, on your review (65541, one coherent identity), head 712ee7e7: round two's sequence check (sequence, kind, key, sequence; 479a4f90) cost about 3 ns a lookup in every cache mode, the default included, in the micro-benchmark (i7-9700, Windows 11, ns a lookup, best of 7, two runs each: unset 16.7/16.7 → 19.6/19.3, HOS_OBJ_CACHE=65536 13.7/13.6 → 16.7/17.0, sets 15.8/16.0 → 18.5/18.5, =0 28.0/28.3 → 31.7/31.1 against 9baebe0d's kind-then-key check), so 712ee7e7 replaces it with one 64-bit identity word per object (the kind in the top 4 bits over the key), written after every change of kind under table_lock and read by obj_is in one load (acquire); an object only takes another identity as a tombstone, whose word is 0, and gets its new word last, so a kind never pairs with another identity's key. tests/test_obj_cache_size.py reuse (sets, unset, 0, HOS_NO_OBJ_CACHE=1, a reassignment before the lookup and after each of its loads) and fiber still fail on 9baebe0d's source and pass at the head (b7554f7b only makes the harness's tombstones through drop and builds --bench without its per-load watch, which had charged a call to every load). The cost: 8 bytes an object (344 → 352 bytes on Windows); this head's timing against main @ 9dd41326 and 479a4f90 is held for the PC's run-alone queue, pending (Measurements says so).

@futondestroyer, on your [review](https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/1969#issuecomment-65542) ([65541](https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/1969#issuecomment-65541), one coherent identity), head 712ee7e7: round two's sequence check (sequence, kind, key, sequence; 479a4f90) cost about 3 ns a lookup in every cache mode, the default included, in the micro-benchmark (i7-9700, Windows 11, ns a lookup, best of 7, two runs each: unset 16.7/16.7 → 19.6/19.3, `HOS_OBJ_CACHE=65536` 13.7/13.6 → 16.7/17.0, `sets` 15.8/16.0 → 18.5/18.5, `=0` 28.0/28.3 → 31.7/31.1 against 9baebe0d's kind-then-key check), so 712ee7e7 replaces it with one 64-bit identity word per object (the kind in the top 4 bits over the key), written after every change of kind under `table_lock` and read by `obj_is` in one load (acquire); an object only takes another identity as a tombstone, whose word is 0, and gets its new word last, so a kind never pairs with another identity's key. `tests/test_obj_cache_size.py` `reuse` (sets, unset, `0`, `HOS_NO_OBJ_CACHE=1`, a reassignment before the lookup and after each of its loads) and `fiber` still fail on 9baebe0d's source and pass at the head (b7554f7b only makes the harness's tombstones through `drop` and builds `--bench` without its per-load watch, which had charged a call to every load). The cost: 8 bytes an object (344 → 352 bytes on Windows); this head's timing against `main` @ 9dd41326 and 479a4f90 is held for the PC's run-alone queue, pending (Measurements says so).
Author
Contributor

@Beanstalk This adds a switch, HOS_OBJ_CACHE, off by default: with HOS_OBJ_CACHE=sets, the per-thread cache of guest sync objects is 2048 sets of 8 ways (host/nn_os_sync.c); in every cache mode, a lookup now checks the object's identity with one word in one load. It has not run in the game yet. Please start from the market-entrance save and play Castle Town Market for two minutes, the PR against main on the same PC, on the PR's side with HOS_OBJ_CACHE=sets and with it unset, on a Linux PC with an AMD processor, a Windows PC with an AMD processor, a PC with 6 or fewer logical processors, and the Steam Deck. No crash or hang; set, fewer misses in the log's "sync: object cache" line than unset and frame time p99 equal to unset or better, within the run-to-run spread; unset, frame times as main's. Please report the object cache line's miss counts.

@Beanstalk This adds a switch, HOS_OBJ_CACHE, off by default: with HOS_OBJ_CACHE=sets, the per-thread cache of guest sync objects is 2048 sets of 8 ways (host/nn_os_sync.c); in every cache mode, a lookup now checks the object's identity with one word in one load. It has not run in the game yet. Please start from the market-entrance save and play Castle Town Market for two minutes, the PR against main on the same PC, on the PR's side with HOS_OBJ_CACHE=sets and with it unset, on a Linux PC with an AMD processor, a Windows PC with an AMD processor, a PC with 6 or fewer logical processors, and the Steam Deck. No crash or hang; set, fewer misses in the log's "sync: object cache" line than unset and frame time p99 equal to unset or better, within the run-to-run spread; unset, frame times as main's. Please report the object cache line's miss counts.
Member

@siantm only members of BigBeans can start the farm. One of them can approve this request by reacting 👍 to it or to this comment.

@siantm only members of `BigBeans` can start the farm. One of them can approve this request by reacting 👍 to it or to this comment.
Some checks are pending
CI / Conventional Commit title (pull_request) Blocked by required conditions
CI / Python source checks (ubuntu-24.04, part 1/2) (pull_request) Blocked by required conditions
CI / Python source checks (ubuntu-24.04, part 2/2) (pull_request) Blocked by required conditions
CI / SPIR-V fixtures (global narrow load/store, direct surface atomics, F2I immediate) (pull_request) Blocked by required conditions
CI / Workflow lint (actionlint) (pull_request) Blocked by required conditions
CI / Website build and checks (pull_request) Blocked by required conditions
Guide screenshots / Guide images and local UI source review (pull_request) Blocked by required conditions
Third-party parts / As their notes say (pull_request) Blocked by required conditions
Third-party parts / The same as upstream (pull_request) Blocked by required conditions
PR description / Description follows the template (pull_request_target) Successful in 4s
beanstalk/approval waiting for a BigBean's 👍 on the request
Website / Website on Cloudflare (pull_request_target) Has been skipped
This pull request is marked as a work in progress.
Some workflows are waiting to be reviewed.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u 'perf/obj-cache-sets:siantm-perf/obj-cache-sets'
git switch 'siantm-perf/obj-cache-sets'
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
4 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!1969
No description provided.