Repository navigation
Conversation
…ning `Batch::add` keeps one entry per shadow buffer and folds each new range into it, but it found that entry with a linear `Arc::ptr_eq` scan over every buffer already seen in the submission. It runs once per `Step::Refresh`, which Marvisak#400 counts at 33-35 k per world frame, so the preparation was quadratic in the number of buffers a submission touches. Keep a `HashMap<usize, usize>` from `Arc::as_ptr` to the entry's position instead. The entry still holds a clone of the `Arc`, so the allocation the key names stays alive until the entry is drained and the address cannot be reused under it; `flush()` clears the index with the vector. New `#[ignore]`d test in the `batch` module, stand-in buffers of one word each (the comparison is a pointer either way), 33 000 refreshes over the same 50/250/1000 buffers, best of 7: buffers scan index ratio 50 0.15 ms 0.20 ms 0.8x 250 0.91 ms 0.21 ms 4.4x 1000 3.18 ms 0.24 ms 13.1x Below about 75 buffers the scan wins by 0.05 ms; above it the scan grows linearly and the index does not. The merge result is identical - same entry, same min/max fold, drained in insertion order. cargo test --workspace --locked, both clippy configurations with -D warnings, cargo xtask policy.
|
Note for whoever reviews this against #776: both touch They also stack: #776 cuts the number of askings, this cuts the per-refresh merge. Neither changes the other's numbers. |
|
Closing this as superseded by let (key, end) = (std::sync::Arc::as_ptr(shadow) as usize, address + len);
if self.current.get(&key).is_some_and(|&(_, a, b)| a <= address && end <= b) {
return;
}
match self.pending.get(&key) { .. }For the record, so the measurement is not lost: on a synthetic world-sized batch (250 buffers, then 1 000) the merge went from 4.4x to 13x faster than the scan, and the same numbers would have applied to a Thanks to whoever landed it - it is the same fix, done there first. Nothing else of mine depends on this PR; #1656 (the copy-back side) is being rebased on top of the new |
A submission's refreshes are merged with a linear scan per refresh, so a frame's preparation is quadratic in the number of buffers. An index makes it constant: 4.4x at 250 buffers, 13x at 1000, and 33 000 refreshes per submission is what #400 measures.
Batch::addkeeps one entry per shadow buffer and folds each new range into it. It found that entry withrefreshes.iter_mut().find(|(s, ..)| Arc::ptr_eq(s, shadow))- a scan over every buffer already seen in the submission - and it is called once perStep::Refresh, which #400 counts at 33-35 k per world frame.What changes
BatchkeepsHashMap<usize, usize>fromArc::as_ptrto the entry's position. Lookup is a hash of one word instead of a scan; the entry still holds a clone of theArc, so the allocation the key names stays alive until the entry is drained, and addresses cannot be reused under it.flush()clears the index with the vector.Measured
New
#[ignore]d test in thebatchmodule, stand-in buffers of one word each (the comparison is a pointer either way, so the shape is the real one), 33 000 refreshes over the same 50/250/1000 buffers, best of 7:Honest about the small end: below about 75 buffers the scan is cheaper than a hash, by 0.05 ms. Above it the scan grows linearly and the index does not. #400 and #783 report a world submission refreshing tens of thousands of ranges over buffers that include "several views into one 174 MB block, among others", so if a submission touches more than ~75 distinct buffers this is 0.7-2.9 ms of every frame's preparation; if it touches fewer, it costs 0.05 ms. A one-line counter of
batch.refreshes.len()in thecopystatline would settle which - happy to add it if you want the number before deciding.Run it with:
Correctness
The merge result is identical: same entry, same
min/maxfold, entries still drained in insertion order.cargo test --workspace --locked, both clippy configurations with-D warnings,cargo xtask policy.No game data used.