Skip to content

perf(editor): delete selection without pre-read - #13479

Closed
yuxi-liu-wired wants to merge 2 commits into
logseq:masterfrom
yuxi-liu-wired:perf/delete-without-preread
Closed

yuxi-liu-wired wants to merge 2 commits into
logseq:masterfrom
yuxi-liu-wired:perf/delete-without-preread

Conversation

@yuxi-liu-wired

@yuxi-liu-wired yuxi-liu-wired commented Sep 27, 2026 •

Copy link
Copy Markdown
Contributor

What changes

Deleting selected blocks (Backspace or Delete on a selection, and the delete step of Cut) went through cut-selection-blocks in src/main/frontend/handler/editor.cljs. It read the selected blocks from the worker (db-async/<get-blocks) only to keep the top-level blocks of the selection and to skip a selection made only of recycle roots, then delete-blocks! sent :delete-blocks. The editor moved to the block above when the worker replied (run-response-editor-callback! in src/main/frontend/db/transact.cljs), although the reply of a delete carries no editor rows (operation-row-uuids returns none for :delete-blocks). A key pressed before the reply found no editor and was lost.

  1. The delete is sent without a read. The window sends the uuids of the selected rows in selection order, with the facts it reads from the DOM as before: [:delete-blocks [uuids {:selection {:original-ids {...}}}]]. :original-ids maps a rendered linked block to its linking block (originalblockid, the same lookup block-handler/get-top-level-blocks uses), and on mobile :delete-journals? true is added. logseq.outliner.op/apply-ops! resolves an op with :selection against the db into the ops the window used to send:

    • a block whose parent comes earlier in the selection is dropped, levelled by the same outliner-core/blocks-with-level;
    • the linking block is deleted in place of a rendered linked block;
    • a selection whose remaining blocks are all recycle roots resolves to no op. As in the window's check, only a block's own :logseq.property/deleted-at counts: the :block/parent of a <get-blocks row is a ref summary without that attribute, so ldb/recycled? never saw a recycled parent there;
    • on mobile, selected journal pages become :delete-page ops.

    The recorded outliner ops, which undo and sync read, are the ones the window sent before (unit tests below compare them). Page nodes are still deleted as nodes (db-test#1242). Callers of :delete-blocks without :selection are unchanged.

  2. The editor moves when the op is sent. delete-blocks! marks its editor callback with :editor/edit-block-on-send?, and apply-outliner-ops schedules it when it queues the request instead of running it after the reply; the reply then only applies its delta. The target is found in the DOM as before (edit-previous-window-block-fn on the row above the first selected row). The window changes no data: the deleted rows leave when the worker's delta arrives, as before. cut-selection-blocks no longer waits for the reply, so cut-blocks-and-clear-selections! clears the selection right after the send.

    The callback runs at the next animation frame (on-next-frame!, which the file already has), after the key event is fully dispatched. Two other timings failed on the way:

    • In a microtask, the editor opened while the Backspace was still being dispatched. The shortcut handlers are separate KeyboardShortcutHandlers on window, and the editing-only one (:editor/backspace, :editor/delete) receives the key after the one that handles the selection, so it also deleted the last character of the block above.
    • In a setTimeout 0 task, Chromium dispatched a key pressed 10-35 ms after the Backspace before the timer task (a renderer trace shows the delete's keydown task, a frame, the next keydowns, then the timer), and that key found no editor: Shift+ArrowUp landed in 15 of 30 tries on the desktop app.
  3. The worker renders the resources of a deleted block as nil (a separate commit, first on the branch). :thread-api/get-render-snapshots failed its whole batch when one resource named a block the worker no longer had, and the page fell to the "Something went wrong" screen. A delete op can reach the worker before a snapshot batch the renderer queued earlier, since the loader sends one batch at a time. Master has the same race in its code; I have not seen it fail there (240 runs of the steps below), and this branch sends the op sooner and keeps the page working while the delete is in flight. In 60 runs of the delete steps on the e2e build, with the unit suite running beside them, 2 ended on that screen after :block-display-properties failed for the block just deleted ("Missing renderer resource entity"). render-snapshots now catches a resource failure whose owner uuid (the key's second element, the owner frontend.db.subs/apply-delta-store reads) names no entity, and returns a nil value watching [:entity owner], as missing-view-data and block-breadcrumb already do for their missing targets; every window consumer of these resources treats nil as not loaded. A failing resource of a live block still fails. block-comment-summary-resource-rejects-invalid-uuid-and-thread-test expected a random uuid to fail; that case moved to the new test.

When the worker refuses the delete (a built-in node; the worker broadcasts its notification as before) or fails, the rows stay and the editor stays on the block above; the error is logged as :editor/delete-selection-failed. Before this change the editor did not move and the selection stayed. The editor is not moved back, since a key typed into it after the delete would be lost again. A selection of recycle roots, or of blocks the worker no longer has, now also leaves the editor on the block above, where before nothing happened.

:ui/page-id is no longer computed for this op: apply-outliner-ops removes it before the worker call and no code reads it.

The desktop app, release compiles of the renderer, the db worker and the Electron main from 1 commit each (master 16c4ed1a0 and this branch), a new graph without sync, a page of 4 blocks. Each delete selects the last block with Escape and presses Backspace, as delete-test does; each key waits until no worker request has been in flight for 400 ms. Median (range), sessions in the order master, branch, branch, master, 30 deletes each:

  • Backspace to the editor focused on the block above: master 92.4 ms (76.6 - 164.7) and 93.1 ms (75.3 - 166.8), this branch 14.5 ms (8.0 - 49.3) and 13.4 ms (7.5 - 45.9).
  • Backspace to the deleted row gone from the page (the worker's delta applied): master 83.0 ms (67.9 - 152.3) and 84.3 ms (65.9 - 137.5), this branch 75.0 ms (61.1 - 99.0) and 77.0 ms (57.8 - 122.7). The row leaves about 8 ms sooner, since the read before the op is gone.
  • Insert and save in the same sessions (they keep their own paths): insert 90.5 and 87.9 ms on master, 89.1 and 84.3 ms here; save 81.6 and 82.1 ms on master, 78.9 and 76.1 ms here.

A key pressed right after the Backspace, as fast as the test driver sends it, 30 times each, 1 session per build (a second master session gave the same counts):

  • the letter x: it lands in the editor of the block above in 0 of 30 on master (pressed 5.6 - 21.7 ms after the Backspace), 30 of 30 here (7.6 - 30.7 ms);
  • Shift+ArrowUp: it selects the block above in 0 of 30 on master (6.3 - 18.2 ms), 30 of 30 here (13.4 - 73.7 ms). The driver sends the next key after the renderer has handled the previous one, so on this branch it comes after the frame that opens the editor.

Those branch sessions ran the delete change alone. The branch with both commits, 1 more session of each kind: 13.8 ms (7.5 - 51.2) to the editor, and the letter and Shift+ArrowUp landed 30 of 30 each. No branch session logged a console error.

The e2e web build (clj-e2e defines, release compile with --debug), master 16c4ed1a0 against this branch, 3 runs side by side on this machine:

  • outliner-basic-test's delete steps as master has them (Shift+ArrowUp right after the Backspace, without a wait for the editor), repeated 20 times per run on a fresh page: master lost the first Shift+ArrowUp in 8 of 60 iterations (b3's editor mounted after that key), this branch in 0 of 180 (run with the whole unit suite beside it). Backspace to b3's editor mounted, median (p90, max): master 72 ms (330, 504), this branch 20 ms (76, 138). Both builds also end some iterations with no editor (master 3 of 60, this branch 7 of 180), each after the test's second Escape, the race test(e2e): wait out editor races in delete-test #13474 fixes in the test. No iteration ended on the error screen.
  • outliner-basic-test: master 3 of 5 runs pass (failures in consecutive-backspace-does-not-restore-deleted-blocks-test, backspace-empty-first-child-keeps-empty-parent-subtree-test, delete-end-test, enter-then-tab-on-collapsed-block-expands-it-test), this branch 4 of 4.
  • editor-basic-test: master 0 of 2 runs pass (move-blocks-cmdk, page-ref-navigate-persists-edit-buffer-with-open-popup-test, move-blocks-mod+shift+m), this branch 2 of 2.

Tests

  • New in logseq.outliner.op-test (deps/outliner/test): the worker keeps the top-level blocks in selection order and records [:delete-blocks [[a11 a b] {}]] for a selection [a11 a a1 b]; a selection of a recycle root resolves to no op, while a block under a recycle root and a recycle root beside a live block are deleted; a rendered linked block deletes its linking block and keeps the target; with :delete-journals? a journal page becomes a :delete-page op and goes to the recycle bin. On master these 4 tests fail (8 failures).
  • New apply-outliner-ops-runs-an-edit-on-send-callback-when-the-op-is-sent-test and apply-outliner-ops-keeps-an-edit-on-send-when-the-worker-fails-test in frontend.db.transact-test: the request is out and the editor has not moved within the task that sent it; the callback runs at the next animation frame, once, before the reply; it still runs when the worker fails, and the failure reaches the caller. On master they fail (4 failures). A check of the same kind failed against the microtask version.
  • delete-selection-sends-the-selection-and-focuses-the-previous-block-at-send in frontend.handler.editor-async-test replaces the test that asserted the editor moved only from the reply: no worker read before the op, the op carries the selected ids, the editor moves before the reply and not again after it. On master it fails (3 failures). cut-selection-blocks-uses-inner-text-property-value-test in frontend.handler.editor-test now checks that Cut sends the inner property-value block without a read (2 failures on master).
  • New resources-of-a-deleted-block-do-not-fail-their-snapshot-batch-test in frontend.worker.handler.render-resource-test: a batch with the display properties and comment summary of a deleted block beside the display properties of a live one renders the first 2 as nil and the third as before; a bad request for a live block still throws. On master it errors.
  • The db-test#1242 tests (delete-selected-child-page-unlinks-namespace-and-keeps-content, delete-selected-block-still-removes-its-children) pass through the worker path.
  • The whole unit suite with TZ=Etc/GMT+12 (2518 tests, 12547 assertions, logseq.outliner.op-test included) and its time-zone-dependent namespaces with TZ=Pacific/Kiritimati (1483 tests, 9100 assertions): 0 failures, 0 errors.
  • clj-kondo on the 10 changed files: 0 errors, 0 warnings.

Allow edits from maintainers.


Devin Review

get-render-snapshots failed its whole batch when one resource named a
block the worker no longer has, and the page fell to its error screen.
A delete op can reach the worker before a snapshot batch that the
renderer queued earlier, so a resource whose owner uuid (the key's
second element, the owner the renderer's delta handling reads) names
no entity now renders as nil and watches that entity, as view-data and
block-breadcrumb already do for missing targets.
Deleting selected blocks read them from the worker first, only to keep
the top-level blocks and to skip a selection of recycle roots. The op
now carries the selected ids with a :selection option and the worker
resolves it inside :delete-blocks into the ops the window used to send,
so the recorded ops are unchanged. The editor moves to the block above
when the op is sent instead of when the worker replies; the deleted
rows still leave when the delta arrives.

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Devin Review found 1 potential issue.

1 flag not posted on this PR by your GitHub settings — view it in Devin Review. (Configure)

Devin Review

Comment on lines +46 to +49
(catch :default error
(if-let [owner (deleted-owner-uuid db resource-key)]
[#{[:entity owner]} nil]
(throw error)))))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Malformed resource requests silently succeed

When an owner UUID is absent, render-resource turns malformed resource parameters into a successful nil snapshot. Invalid requests no longer fail validation, hiding caller errors.

Learn more

Resource renderers validate more than owner existence. For example, block-display-properties validates its display context after resolving the owner. If the owner is missing and the context is malformed, owner lookup throws first. The broad catch converts that failure to nil and skips validation. The same catch also converts any other renderer failure for a missing owner into a successful snapshot.

Example: Request [:block-display-properties missing-uuid {:page-title? true}]. The owner is absent and the context lacks required flags. The response contains a ready nil resource instead of reporting the invalid context.

Recommended fix: Validate resource parameters independently of owner existence before applying the missing-owner fallback. Limit recovery to the specific missing-owner exception, rather than catching every renderer error for a missing UUID.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

@yuxi-liu-wired

Copy link
Copy Markdown
Contributor Author

Continued in #13547 together with #13210 and #13213: they share files, so they now merge once. The commits are the same, on current master.

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.

1 participant