Skip to content

Moving many objects: the patch's writes reach the GPU queue joined, not one per object - #3462

Merged
HakanSeven12 merged 2 commits into
HakanSeven12:mainfrom
jespinolao:perf/batched-gpu-writes
Nov 23, 2026
Merged

HakanSeven12 merged 2 commits into
HakanSeven12:mainfrom
jespinolao:perf/batched-gpu-writes

Conversation

@jespinolao

Copy link
Copy Markdown
Contributor

How it shows up

Refs #3231, for its second point: "a lot of lag when copying, moving and stretching groups". There is no file there, so this is a synthetic drawing of 100,800 objects (14,400 cells of five lines, a circle and a text). With the 100,440 in view selected, MOVE takes 1.5 s to show its result on Direct3D 12 and 0.7 s on Vulkan, and Undo takes the same again.

Cause

A move keeps the shape of every object, so the retained GPU buffers are patched in place. Both patches send each object to the queue as a call of its own:

  • the wire arena (WireArena::patch, and the packed variant): one Queue::write_buffer per changed object, 72,000 here;
  • the analytical slots (AnalyticSlots::apply): one per circle, 14,400 here.

wgpu makes a staging buffer for every write_buffer call. On this machine a call takes about 11 µs on Direct3D 12 and 1.5 µs on Vulkan, whatever its size from 4 bytes to 1 KiB, and about as long again when the queue frees the staging buffer. Building the whole wire arena again takes 14 ms for these 72,000 instances (arena-build-detail).

The change

  • src/scene/pipeline/gpu_upload.rs: WriteBatch collects the pieces a patch writes into one buffer and gives them back in buffer order, each run of pieces lying side by side as one write. Pieces over the same bytes go out one by one as they came.
  • wire_arena.rs, both arenas: the instances of a patch go through a batch and reach the queue at the end of the patch. The constants of an appended or resized slab go with the merged constant upload the arena already has (merge_const_ranges).
  • analytic_slots.rs: the same for the block, circle and ellipse buffers. A hidden circle or ellipse is now written whole, as a shown one is, so a run of hidden ones joins too. Only its depth is read while it is hidden (apply_draw_order sends it outside the clip volume), and showing it again writes every field.
  • Objects apart in the buffer still need a write each. rebuild_is_cheaper: when more than 256 separate writes are left and they outnumber a thirty-second of the buffer's instances, the patch says so (false from the arena, Err from the slots) and the existing fallback builds the buffers again. A patch that gives up part-way has written nothing.

Measured

Release builds, Windows 11, RTX 3070, PERF=1. main is 0ca0345 with the lock file of #3459 so that it builds. "Frame" is nav-prepare elapsed, from the command to the prepared frame; each cell is one run.

The drawing of 100,800 objects, Direct3D 12, two runs of each build:

main This PR
MOVE of 100,440 objects: frame 1,540 / 1,552 ms 596 / 639 ms
— its wire stage ([perf] wire) 1,016 / 1,009 ms 65 / 75 ms
— of that, the circles (analytic-patch) 170 / 173 ms 17 / 18 ms
Undo: frame 1,588 / 1,581 ms 600 / 645 ms
MOVE of 14,348 objects, nearly all circles: frame 253 / 262 ms 104 / 102 ms
MOVE of every other object, 50,218: frame 768 / 878 ms 326 / 337 ms
— its wire stage 506 / 598 ms, patch 64 / 69 ms, rebuild

The same on Vulkan, one run of each:

main This PR
MOVE of 100,440 objects: frame 722 ms 574 ms
— its wire stage 170 ms 66 ms
Undo: frame 732 ms 614 ms
MOVE of 14,348 objects: frame 122 ms 105 ms
MOVE of every other object: frame 369 ms 339 ms

A second drawing of 90,000 objects on eight layers (walls, hatches, arcs, circles, texts). A layer of 10,000 circles switched off and on: its circles lie apart in their buffer, between the arcs of another layer, so this is the case that goes to the rebuild.

Frame main This PR
Direct3D 12: layer off / on 401 / 333 ms 281 / 228 ms
Vulkan: layer off / on 287 / 247 ms 277 / 238 ms

What is left of the MOVE frame is not touched here: about 270 ms to tessellate the moved objects again (resident-patch) and about 190 ms in the command.

Same picture

The same view captured after each step in both builds: MOVE of everything, its undo, ERASE of everything, its undo, MOVE of scattered objects, its undo (11 captures), and on the second drawing an erase of scattered objects, its undo and two layers off and on (6 captures). In the first drawing's captures the drawing area is the same pixel for pixel. In the second drawing's, one pixel is one level of 255 apart between the two builds, the same pixel in all six, the undone states included.

main This PR
After MOVE of scattered objects main: scattered objects moved this PR: the same image
The layer of circles off main: circles hidden this PR: the same image

Tests

The tests are the first commit, with the patches already going through a batch that passes each write on as it came; the change is the second.

  • gpu_upload.rs: pieces_side_by_side_go_out_as_one_write, pieces_apart_stay_apart_and_go_out_in_buffer_order, pieces_over_the_same_bytes_go_out_one_by_one_as_given, an_empty_piece_is_no_write, many_separate_writes_cost_more_than_the_rebuild.
  • analytic_slots.rs: curves_side_by_side_are_one_write, hidden_curves_side_by_side_are_one_write, block_placements_are_joined_within_their_own_arena.
  • gpu_tests.rs, needs a GPU adapter: a_wire_patch_of_many_scattered_objects_asks_for_the_rebuild patches 4,096 lines in both arenas: every one (one write), one in 64 (64 writes) and every other (2,048 writes, which asks for the rebuild). Run here on the RTX 3070 with block_edits_preserve_cache_coordinates_and_arena_partition: both pass.
  • Seven of them fail on the first commit: three of gpu_upload, three of analytic_slots and the GPU one.
  • cargo test --workspace --no-fail-fast, with the lock file of Cargo.lock: gpu-allocator is back on the windows crate wgpu-hal uses, so main builds on Windows #3459: 7,763 passed, 1 failed, 50 ignored. app::control::spellcheck::tests::speller_survives_com_unload_reload_cycles fails the same way on main on this machine.
  • cargo check --target wasm32-unknown-unknown --lib passes.

Limits

OCS main at 0ca0345, Windows 11.

🤖 Generated with Claude Code

jespinolao and others added 2 commits November 22, 2026 15:18
A patch of the wire arena or of the analytical slots sends each changed
object to the queue as a write of its own. These tests ask for pieces
lying side by side to go out as one write, for a hidden circle or
ellipse to be written whole so that a run of them joins too, and for a
patch left with more separate writes than a rebuild costs to say so.

The patches now collect their writes in a batch, which still passes each
one on as it came: the tests fail.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Moving 100,000 objects took 1.5 s to show on Direct3D 12, one second of
it in `Queue::write_buffer`: the wire arena wrote each object's
instances on their own, and the analytical slots each circle. The queue
makes a staging buffer for every call, about 11 µs each there and 1.5 µs
on Vulkan whatever the size, so 86,000 writes cost far more than their
bytes, while building the same buffers again takes a tenth of a second.

A patch now collects its writes and sends them at its end, in buffer
order, each run of pieces lying side by side as one write. The constants
of an appended or resized slab go with the merged constant upload the
arena already had. A hidden circle or ellipse is written whole, as a
shown one is, so a run of hidden ones joins as well; only its depth is
read while it is hidden.

Objects apart in the buffer still need a write each. When more than a
few hundred are left and they outnumber a thirty-second of the buffer's
instances, the patch reports that the rebuild is cheaper, and the
existing fallback builds the buffers again.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@HakanSeven12
HakanSeven12 merged commit e243802 into HakanSeven12:main Nov 23, 2026
5 of 6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants