-
Notifications
You must be signed in to change notification settings - Fork 3.6k
perf(editor): dedupe alignment snap-lines in O(n) instead of O(n2)#692
Open
ly-wang19 wants to merge 1 commit into
Open
perf(editor): dedupe alignment snap-lines in O(n) instead of O(n2) #692ly-wang19 wants to merge 1 commit into
ly-wang19 wants to merge 1 commit into
Conversation
uniqAlignLines used Array.findIndex inside a forEach (O(n^2)) to merge alignment snap-lines. It runs on every drag/scale mousemove and the line count scales with nearby element edges, so it janks on element-dense canvases. Dedupe via a Map keyed on value (O(n)); a Map preserves first-occurrence order, so output order and range-merge semantics are unchanged. Closes THU-MAIC#691
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.
What & why
uniqAlignLines(lib/utils/element.ts) merges alignment snap-lines withArray.findIndexinside aforEach— O(n2):It runs on every drag/scale mousemove (
useDragElement/useScaleElement), and the snap-line count grows with the number of nearby element edges. On element-dense canvases that’s ~n2 comparisons per pointer move at ~60 Hz → drag jank.Closes #691.
Fix
Dedupe in O(n) via a
Mapkeyed onvalue. AMappreserves first-occurrence insertion order (re-set-ting an existing key keeps its slot), so the output order and the range-merge semantics are unchanged — behavior-preserving, just linear.Test plan
New
tests/utils/uniq-align-lines.test.ts(5 cases): dedupe + outer-bound range merge, first-occurrence order preserved, single line, empty input, and many-duplicates collapse.tsc/prettier/eslintclean.No user-facing strings (no i18n impact). (Separate test file from
element.test.tsto avoid overlap with #675, which also toucheselement.ts.)