Repository navigation
Runtime: buffer_at finds a buffer by binary search, not a scan of every resource - #2823
Conversation
Buffers' VA ranges kept in a std::map by first VA: a lookup is a binary search, walking down from the nearest start only while a range can still reach the VA (bounded by the longest range), so aliasing placed buffers that overlap still resolve, to the range that starts closest below the VA (the larger ID on a tie). buffer_va_index_test checks edges, overlapping and nested ranges, re-registration and erase, then 200 random tables against a linear scan with the same rule, and prints the lookup time against the scan over every resource that buffer_at does today (--bench for 50k and 100k resources).
…ry resource VulkanContext::buffer_at walked every registered resource, images included, for each vertex-buffer slot, index buffer and WriteBufferImmediate of every draw. It now asks a BufferVaIndex of the addressable buffers (no image, a VkBuffer, a VA and bytes: what the scan accepted), kept in step with resources_ by add_resource, add_sparse_buffer and remove_resource; a re-registered ID drops its old range first. Where placed buffers alias, the scan returned whichever matched first in unordered_map order; the index returns the range that starts closest below the VA. docs/d3d12_vulkan.md drops the "Lookups" known gap.
kacper-serewis
left a comment
There was a problem hiding this comment.
Independently tested df1ae87 on Windows with clang++ and synthetic Vulkan handles. A standalone harness called the actual VulkanContext replay buffer_at callback after each of 12 registration, replacement and removal operations. All 3,456 probes agreed with a separate linear reference on hit/miss and, for hits, VkBuffer, offset and clipped size. Cases included sparse registration, same-start and nested aliases, fallback after removal, same-ID relocation, buffer-to-image replacement, VA reuse, empty ranges, and addresses through UINT64_MAX. CPU only, no game files or GPU submission. No timing measurements. AI-assisted review.
|
Code read of AI-assisted review (Claude Code). |
|
Independent VA-index churn check ( AI-assisted (Codex). |
The bound that stops a lookup's walk only grew: once a large buffer was freed, every later miss still walked back as far as that buffer had reached. With 20 000 buffers of 256 bytes, a 1 GiB range added then removed left a miss past the last buffer at 250-600 us instead of ~50 ns (found by @padhossein in review of ItsNotRexed#2823). A multiset of the indexed sizes keeps the bound at the longest range indexed now; insert and erase stay O(log n), find is unchanged. Inserting the same (va, id) again replaces its size instead of leaving the old one in the bound. buffer_va_index_test checks the bound after erases (same sizes and a smaller re-registration included), against the longest live range in all 200 random tables, and times the review's miss-after-free case.
|
@padhossein thanks, fixed in
The bound checks fail on One limit remains, and the header comment now says so: while a large range is still live, a miss walks every start within that range below the VA. For example, a buffer that covers a whole heap with placed buffers inside it. That walk is never longer than the old scan over every resource, images included. Only an interval tree would remove it, and that can be a follow-up if a real frame shows it. Tested on AI-assisted (Claude Code). |
Kam471
left a comment
There was a problem hiding this comment.
Reviewed at head df1ae87e; the branch merges cleanly with current main and the diff is limited to the buffer index, its test, docs and CMake. Inspection found a correct ordered index: (va, id) ordering, upper_bound({va, max_id}) then walk back, nearest start wins, larger ID wins on ties, and offset >= longest_ is a safe early exit. The test covers null/zero skip, first/last byte, one-past-end, gaps, highest VA, nested and same-start aliases, erase/refill, plus 200x2000 randomized comparisons against a linear scan. No game data or host paths. Approve.
Not built here (no configured build dir in the worktree), so this is inspection-only. Non-blocking: longest_ is only recomputed when the index empties, so removing the longest range makes later misses walk every entry; that edge is untested.
|
Code review of 111b634 (read only, no runs): looks right to me. What I checked:
One suggestion, not blocking, about that overlap rule. With placed buffers aliasing in one heap, say a big buffer B at heap offset 0 and a small S inside it, a vertex view that starts in S but runs past S's end will resolve to S, because S starts closer. The view is then cut at S's end, and the rest reads as out of bounds (zeros), even though B holds all of it. One note on measuring: AI-assisted (Claude Code). |
With placed buffers aliasing in one heap, a large buffer B and a small S inside it, a vertex or index view that starts in S and runs past its end resolved to S, the closest start below the VA, and buffer_at cut it at S's end: the rest read as out of bounds, though B holds all of it. Found by traeb0n in the review of ItsNotRexed#2823. BufferVaIndex::find takes the bytes the lookup needs and returns, among the ranges that hold the VA, the closest start (larger ID on a tie) that also holds all of them; when none does, the closest that holds the VA, as before. Without overlaps the first range it looks at is still the answer. With a fallback found, the walk stops once no range can hold all the bytes, bounded by the longest live range. buffer_at passes the requested size. buffer_va_index_test checks views that fit in the small buffer, run past it into the large one, run past every range and have size 0, and three quarters of the random-table lookups now name a view of up to 32 KiB, checked against a scan with the same rule.
|
@traeb0n thanks for the review, and good catch on the aliasing case. I'd add one thing to "this PR doesn't make anything worse": the old scan picked B or S in hash order, so that view was sometimes right, while the nearest-start rule picks S every time. Deterministic, but that case is now wrong every time, so it was worth fixing here rather than in a follow-up. Done in b8273c0, as you suggested:
Tested on b8273c0: full runtime build and Agreed on AI-assisted (Claude Code). |
# Conflicts: # docs/d3d12_vulkan.md
|
Tested b8273c0 on RTX 5090, 621.24, Windows 11: pass. Built Swapping the linear scan for a binary search over the VA ranges is the kind of change that needs exactly this: a test that pins the found/not-found edges, since a wrong comparator only shows up as a missing buffer at runtime. |
Item: D3D-322 (claimed in #2516)
VulkanContext::buffer_atwalked every registered resource, images included, for each vertex-buffer slot, indexbuffer and
WriteBufferImmediateof every draw (runtime/src/vulkan_backend.h). Its own comment and the "Lookups"known gap in
docs/d3d12_vulkan.mdalready asked for an ordered map by VA once it showed.Changes
runtime/src/buffer_va_index.h(new):BufferVaIndex, buffers' VA ranges in astd::mapkeyed by first VA. Alookup is a binary search that walks down from the nearest start only while a range can still reach the VA (bounded
by the longest range). Standard library only, no Vulkan.
VulkanContextkeeps it in step withresources_inadd_resource(both overloads),add_sparse_bufferandremove_resource; a re-registered ID drops its old range first. Indexed buffers are what the scan accepted: noimage, a
VkBuffer, a VA and a non-zero size.docs/d3d12_vulkan.md: the "Lookups" known gap is gone; theWriteBufferImmediateparagraph says how the lookupworks now.
Behaviour change
Placed buffers may alias, so ranges can overlap. The scan returned whichever matched first in
unordered_maporder;the index returns the range that starts closest below the VA (the larger ID on a tie), every time. Buffer and offset
are the same when the aliasing buffers share the arena; only the
sizecut can differ.Tests
buffer_va_index_test(new, CPU only, in CTest): edges, overlapping and nested ranges, same start, erase andre-registration, then 200 random tables checked against a linear scan with the same rule.
cteston top ofec8ec4c9: 113/113 passed (the skipped tests skip onmaintoo: missingdata or tools), including
d3d12_vulkan_device, which goes throughVulkanContext.buffer_va_index_test --bench):Not measured with
replay_cpu_benchas the claim said: its frame onmainhas no draws yet (#2123 adds them).Windows 11, MSVC (Visual Studio 18), NVIDIA GeForce RTX 3060 Ti, driver 32.0.15.9186. No game files.
Checklist
docs/design.md, with how it was confirmed. (Not applicable: no translator change.)AI-assisted