perf(gpu): look up root views in O(log n) and gate pending table remirrors #242

Manually merged
github merged 1 commit from github-bot/gh-242 into main 2026-11-27 04:00:46 +00:00 AGit
Owner

@⁠Sheffy007 on GitHub · 2026-11-27 03:49 UTC · original
Sheffy007:perf/native-host-pool-lookup-and-remirror → main · +157 −10 · 6 files · 2 commits · merged 2026-11-27 03:57 UTC

Summary

Reduces CPU time spent holding the native host TITLE lock during ExecuteCommandLists (NativeHost::execute), addressing two world-frame submission bottlenecks reported on #15 and #179:

  1. Direct O(log n) pool lookup in NativeHost::root_view:
    • World streaming frequently maps/unmaps tiles between draws, bumping PoolTable::generation(). Calling self.mapping_list() inside root_view rebuilt the entire ~96k-entry mapping snapshot (~1/3 of submit-thread CPU samples in #15) just to resolve a single address.
    • Adds PoolTable::mapping_at (BTreeMap::range(..=addr).next_back(), zero heap allocation) and feeds it directly to addr::mapped_root_view.
  2. Epoch-gated retries in NativeHost::remirror_tables (NativeHeap::pending_mirror):
    • Since 73793f4, unmapped buffer descriptors and unrealized/undecodable table entries were left out of NativeHeap::mirrored, causing every subsequent draw/dispatch to re-decode them and re-encode null_view into the Metal heap even when nothing had changed.
    • Tracks (record, pool_generation, resources_epoch) in NativeHeap::pending_mirror so unchanged entries are skipped until the descriptor record, PoolTable::generation(), or registered resources (resources_epoch) actually advance.

Tests

  • native::memory::tests::mapping_at_finds_single_address_without_snapshot
  • native::host::tests::root_view_looks_up_pool_table_without_rebuilding_mapping_list
  • native::tests::submission_retries_an_unchanged_buffer_descriptor_after_mapping (verifies buffer_writes is not repeated before mapping and retries once mapped)
  • native::tests::submission_retries_an_unrealized_resource_descriptor_once_registered
  • cargo clippy --workspace --all-targets --target x86_64-pc-windows-gnullvm -- -D warnings (and --all-features) and cargo xtask policy pass.
**@⁠Sheffy007** on GitHub · 2026-11-27 03:49 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/242) `Sheffy007:perf/native-host-pool-lookup-and-remirror` → `main` · +157 −10 · 6 files · 2 commits · merged 2026-11-27 03:57 UTC ## Summary Reduces CPU time spent holding the native host `TITLE` lock during `ExecuteCommandLists` (`NativeHost::execute`), addressing two world-frame submission bottlenecks reported on #15 and #179: 1. **Direct `O(log n)` pool lookup in `NativeHost::root_view`:** - World streaming frequently maps/unmaps tiles between draws, bumping `PoolTable::generation()`. Calling `self.mapping_list()` inside `root_view` rebuilt the entire ~96k-entry mapping snapshot (~1/3 of submit-thread CPU samples in #15) just to resolve a single address. - Adds `PoolTable::mapping_at` (`BTreeMap::range(..=addr).next_back()`, zero heap allocation) and feeds it directly to `addr::mapped_root_view`. 2. **Epoch-gated retries in `NativeHost::remirror_tables` (`NativeHeap::pending_mirror`):** - Since `73793f4`, unmapped buffer descriptors and unrealized/undecodable table entries were left out of `NativeHeap::mirrored`, causing every subsequent draw/dispatch to re-decode them and re-encode `null_view` into the Metal heap even when nothing had changed. - Tracks `(record, pool_generation, resources_epoch)` in `NativeHeap::pending_mirror` so unchanged entries are skipped until the descriptor record, `PoolTable::generation()`, or registered resources (`resources_epoch`) actually advance. ## Tests - `native::memory::tests::mapping_at_finds_single_address_without_snapshot` - `native::host::tests::root_view_looks_up_pool_table_without_rebuilding_mapping_list` - `native::tests::submission_retries_an_unchanged_buffer_descriptor_after_mapping` (verifies `buffer_writes` is not repeated before mapping and retries once mapped) - `native::tests::submission_retries_an_unrealized_resource_descriptor_once_registered` - `cargo clippy --workspace --all-targets --target x86_64-pc-windows-gnullvm -- -D warnings` (and `--all-features`) and `cargo xtask policy` pass.
Query PoolTable::mapping_at directly in NativeHost::root_view so root SRV/UAV lookups do not rebuild the full mapping list whenever background streaming bumps the pool table generation between draws.

In NativeHost::remirror_tables, record (record, pool_generation, resources_epoch) in NativeHeap::pending_mirror when a table entry cannot be mirrored yet (unmapped buffer address or unrealized resource), avoiding repeated null-view heap writes and decodes at every draw until the record, pool table, or registered resources change.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 03:50:18 +00:00
github manually merged commit 7f70c8da5a into main 2026-11-27 04:00:46 +00:00
Author
Owner

@⁠LakshanSivakumar on GitHub · 2026-11-27 03:57 UTC · original · approved these changes

Reviewed and tested together with the other open PRs on main (macOS x86_64 workspace tests, the setup shell tests, and Windows clippy). The ordered lookup matches the old scan, since mappings never overlap. I pushed one small fix to the remirror gate: mirror() read memory.generation() and resources_epoch after resolve had failed. The pool table is written from title threads, so a pool mapped between the failed resolve and that read would record the entry as failed at the new generation, and it would stay null until some later change. Both values are now taken before resolve. Thanks!

**@⁠LakshanSivakumar** on GitHub · 2026-11-27 03:57 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/242#pullrequestreview-6083282787) · **approved these changes** Reviewed and tested together with the other open PRs on main (macOS x86_64 workspace tests, the setup shell tests, and Windows clippy). The ordered lookup matches the old scan, since mappings never overlap. I pushed one small fix to the remirror gate: `mirror()` read `memory.generation()` and `resources_epoch` after `resolve` had failed. The pool table is written from title threads, so a pool mapped between the failed resolve and that read would record the entry as failed at the new generation, and it would stay null until some later change. Both values are now taken before `resolve`. Thanks!
This discussion has been locked. Commenting is limited to contributors.
No description provided.