fix: memoize per-drag derived values to keep drag.dragover cheap#2555
Draft
christianhg wants to merge 1 commit into
Draft
fix: memoize per-drag derived values to keep drag.dragover cheap#2555christianhg wants to merge 1 commit into
drag.dragover cheap#2555christianhg wants to merge 1 commit into
Conversation
The drop-indicator and `deserialization.success` guards both compute `getDragSelection`, `getSelectedBlocks`, and `isSelectingEntireBlocks` on every `drag.dragover` event. These derive from `dragOrigin.selection` which is stable for the entire drag, but the recomputation is O(N) over the dragged range every event. A single drag with "select all" on a 1000-block document was spending ~10ms per dragover (~600+ events per drag). Memoize the derived values on a `WeakMap` keyed on `dragOrigin`. The first event populates the cache, subsequent events do an O(1) lookup. `getFocusBlock` for the drop position is the only per-event tree walk.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
🦋 Changeset detectedLatest commit: 45655c7 The changes in this PR will be included in the next version bump. This PR includes changesets to release 11 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Contributor
📦 Bundle Stats —
|
| Metric | Value | vs main (c2ec808) |
|---|---|---|
| Internal (raw) | 733.2 KB | -144 B, -0.0% |
| Internal (gzip) | 140.0 KB | +67 B, +0.0% |
| Bundled (raw) | 1.33 MB | -147 B, -0.0% |
| Bundled (gzip) | 300.2 KB | +63 B, +0.0% |
| Import time | 94ms | -0ms, -0.4% |
@portabletext/editor/behaviors
| Metric | Value | vs main (c2ec808) |
|---|---|---|
| Internal (raw) | 467 B | - |
| Internal (gzip) | 207 B | - |
| Bundled (raw) | 424 B | - |
| Bundled (gzip) | 171 B | - |
| Import time | 2ms | -0ms, -0.4% |
@portabletext/editor/plugins
| Metric | Value | vs main (c2ec808) |
|---|---|---|
| Internal (raw) | 3.1 KB | - |
| Internal (gzip) | 967 B | - |
| Bundled (raw) | 2.9 KB | - |
| Bundled (gzip) | 899 B | - |
| Import time | 8ms | -0ms, -1.0% |
@portabletext/editor/selectors
| Metric | Value | vs main (c2ec808) |
|---|---|---|
| Internal (raw) | 76.5 KB | - |
| Internal (gzip) | 13.7 KB | - |
| Bundled (raw) | 71.9 KB | - |
| Bundled (gzip) | 12.6 KB | - |
| Import time | 7ms | -0ms, -0.6% |
@portabletext/editor/utils
| Metric | Value | vs main (c2ec808) |
|---|---|---|
| Internal (raw) | 27.8 KB | - |
| Internal (gzip) | 5.5 KB | - |
| Bundled (raw) | 25.4 KB | - |
| Bundled (gzip) | 5.1 KB | - |
| Import time | 6ms | -0ms, -0.3% |
🗺️ . · ./behaviors · ./plugins · ./selectors · ./utils · Artifacts
Details
- Import time regressions over 10% are flagged with
⚠️ - Sizes shown as raw / gzip 🗜️. Internal bytes = own code only. Total bytes = with all dependencies. Import time = Node.js cold-start median.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The drop-indicator and
deserialization.successguards both computegetDragSelection,getSelectedBlocks, andisSelectingEntireBlockson everydrag.dragoverevent. These derive fromdragOrigin.selectionwhich is stable for the entire drag, but the recomputation is O(N) over the dragged range every event.A single drag with "select all" on a 1000-block document was spending ~10ms per dragover (~600+ events per drag). Most of that cost is in
getSelectedBlockswalking the keyed range andisSelectingEntireBlocksrecomputing block boundaries — none of which can change while the drag is in flight.Memoize the derived values on a
WeakMapkeyed ondragOrigin. The first event populates the cache, subsequent events do an O(1) lookup.getFocusBlockfor the drop position is the only per-event tree walk that's actually necessary.The
dragOriginobject reference is stable for the entire drag (assigned once ininternalDragatdrag.dragstart, returned by reference on everygetSnapshot()call), so the WeakMap entry is reachable until the drag ends andinternalDragis cleared.Behavior is unchanged. Cross-browser dnd tests green on chromium, firefox, and webkit.
Measurements
1000-block document, 100 dragover events per scenario:
The remaining cost per dragover is
getFocusBlockfor the drop position which depends on the drop position changing every event.