Skip to content

Undo of an erase: each restored object's draw depth is found at its handle's place, not by walking the order - #3469

Merged
HakanSeven12 merged 2 commits into
HakanSeven12:mainfrom
jespinolao:perf/draw-depth-restore
Nov 23, 2026
Merged

HakanSeven12 merged 2 commits into
HakanSeven12:mainfrom
jespinolao:perf/draw-depth-restore

Conversation

@jespinolao

Copy link
Copy Markdown
Contributor

How it shows up

Refs #3231 ("lag ... at times more than a few seconds" when working on groups in large files). 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). Select the 100,440 in view, ERASE, then Undo: the objects take 3 seconds to come back. Two seconds of that are one line of the PERF log:

[perf] draw-depth-patch  1958.8ms entries=100800 changes=100440

Assigning the draw depths of twice as many objects from scratch takes 25 ms ([perf] draw-depth 25.3ms entries=201240, after a COPY of the same selection).

Cause

Scene::draw_depth_map keeps each block's draw order as a list of entries with stable labels, and an erased entity's entry stays in it so that Undo gives it the same label back. To restore one, the list was searched from its start:

let restored = cache.blocks.get(&block).and_then(|order| {
    order.iter().find(|entry| entry.handle == value)
});

That is one walk through the block's order for every restored entity: 100,440 walks through 100,800 entries.

The change (src/scene/mod.rs)

The list is sorted by effective key, then by handle, and an entity's key is its own handle unless the block's sort table files it under another (DRAWORDER). So:

  • draw_depth_entry_at_own_key looks for the entry by a binary search for the handle as both key and handle;
  • draw_depth_entry falls back to the walk for an entity the sort table moved, whose entry is elsewhere in the list.

The entry found is the same one as before in every case, so the depths do not change.

Measured

Release builds, Windows 11, RTX 3070, Direct3D 12, 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. One run of each build; a second and a third run of main gave 3,019 and 3,023 ms for the first row.

main This PR
Undo of an ERASE of 100,440 objects: frame 3,054 ms 1,091 ms
— draw-depth-patch 1,949.1 ms 25.0 ms
Undo of an ERASE of 33,480 (every third object): frame 1,552 ms 786 ms
— draw-depth-patch 638.1 ms 7.4 ms
Undo of an ERASE of 14,348 (every seventh): frame 509 ms 239 ms
— draw-depth-patch 277.3 ms 3.2 ms
Redo of a COPY of 100,440, undone before: frame 8,620 ms 2,438 ms
— draw-depth-patch 5,888.9 ms 24.3 ms

The redo walks further because the order then holds 201,240 entries.

The same view captured after each step of a sequence in both builds (MOVE of everything, its undo, ERASE of everything, its undo, MOVE of scattered objects, its undo: 11 captures). The drawing area is the same pixel for pixel in ten of them; in the one after the MOVE, which this change does not touch, two pixels are one level of 255 apart.

After the undo of the ERASE, main This PR
main: the drawing back after the undo this PR: the same image

Tests (src/scene/journal_tests.rs)

  • a_draw_depth_entry_is_found_at_the_place_of_its_own_handle: every entry of a seeded order of 5,000 is found by draw_depth_entry_at_own_key, and a handle the order does not hold gets none. Fails on the first commit, where that function still finds nothing and the walk does the work.
  • a_draw_depth_entry_the_sort_table_moved_is_still_found: two entries filed under another key are not at their handle's place, and draw_depth_entry finds them and every other one.
  • undoing_an_erase_gives_every_entity_its_draw_depth_back: 300 lines, all but each seventh erased and restored; the depth map is the one from before the erase, with no relabelling.
  • The tests are the first commit and the change is the second.
  • 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,758 passed, 1 failed, 49 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 16:08
Undoing an erase gives each entity back the label its block's order
kept for it, and the entry was looked for by walking the order from its
start. These tests ask for an entity the sort table left alone to be
found at the place of its own handle, for one the table moved to be
found all the same, and for an erase and its undo to leave every draw
depth as it was.

The lookup now goes through two functions, of which the first still
finds nothing: the first test fails.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Undoing an erase of 100,000 objects spent two seconds giving them their
draw depths back, where assigning the depths of twice as many from
scratch takes 25 ms. Each restored entity's label is kept in its
block's order, and the order was walked from its start for every one.

The order is sorted by effective key, then by handle, and an entity's
key is its own handle unless the block's sort table files it under
another. The entry is now looked for by a binary search at the place of
the handle; only an entity the sort table moved is still searched for
through the order.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@HakanSeven12
HakanSeven12 merged commit ca298a3 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