Repository navigation
fix(undo): keep newest entries when stack is full - #13462
Conversation
| (let [result (conj (if (empty? col) [] col) op)] | ||
| (if (>= (count result) max-stack-length) | ||
| (subvec result 0 (/ max-stack-length 2)) | ||
| (subvec result (- (count result) (quot max-stack-length 2))) |
There was a problem hiding this comment.
🔴 Capped history retains unbounded old entries
After the first truncation, conj-op appends to a suffix subvector while retaining its discarded backing entries. Each later truncation advances the suffix, so repeated edits grow worker memory without bound despite the capped stack.
Learn more
A ClojureScript subvector is a view over its backing vector. When a full history stack takes its newest half, the returned stack still references all older entries in that backing vector. The next conj appends at the subvector's end in that backing vector; the next truncation repeats the process. Both undo and redo use conj-op, so sustained editing or undoing retains discarded history payloads in the worker.
Example: With a cap of 10, the tenth edit keeps five visible entries backed by ten. Five more edits fill the stack again; the next suffix retains five visible entries backed by fifteen. Repeating this cycle keeps increasing the backing vector even though the visible stack has at most nine entries.
Recommended fix: Materialize the retained suffix into a fresh vector when truncating, and test repeated truncation rather than only the visible stack count.
| (subvec result (- (count result) (quot max-stack-length 2))) | |
| (vec (subvec result (- (count result) (quot max-stack-length 2)))) |
Was this helpful? React with 👍 or 👎 to provide feedback.
|
Direction fix is correct — the old code kept the oldest half and dropped the most recent ops at the cap. One nit (Devin Review flagged it too): |
tiensonqin
left a comment
There was a problem hiding this comment.
Thanks for the fix! 🚢 👍
A full undo or redo stack kept its oldest half and dropped the newest half, including the entry just pushed. After the 250th undoable change, changes 126 to 250 had no entries: undo stepped back to change 251, then jumped to change 125 while 126 to 250 stayed applied. Keep the newest half instead.
a72a479 to
48d87fa
Compare
logseq#13476, logseq#13462 and logseq#13458 are in upstream master e2ca31b. logseq#13528 joins.
logseq#13476, logseq#13462 and logseq#13458 are in upstream master e2ca31b. logseq#13528 joins.
logseq#13476, logseq#13462 and logseq#13458 are in upstream master e2ca31b. logseq#13528 joins.
logseq#13476, logseq#13462 and logseq#13458 are in upstream master e2ca31b. logseq#13528 joins.
logseq#13476, logseq#13462 and logseq#13458 are in upstream master e2ca31b. logseq#13528 joins.
logseq#13476, logseq#13462 and logseq#13458 are in upstream master e2ca31b. logseq#13528 joins.
logseq#13476, logseq#13462 and logseq#13458 are in upstream master e2ca31b. logseq#13528 joins.
logseq#13476, logseq#13462 and logseq#13458 are in upstream master e2ca31b. logseq#13528 joins.
What changes
When the worker's undo stack reaches
max-stack-length(250),conj-opkeeps the oldest half and drops the newest half, including the entry it was pushing. After the 250th undoable change, the stack holds changes 1 to 125. The next change is pushed on top. Undo then takes back that change, then jumps to change 125, while changes 126 to 250 stay applied. The redo stack has the same cap.The fix keeps the newest half. Each undo then meets the state that the entry above it left.
Tests
full-undo-stack-keeps-newest-entries-testinfrontend.worker.undo-redo-test, withmax-stack-lengthredefined to 10: 12 title edits, then undo 7 times. On master the titles readv11 v10 v4 v3 v2 v1 child: after 2 undos it jumps back 6 edits. With the fix they readv11 v10 v9 v8 v7 v6 v5, and the stack is empty after that.frontend.worker.undo-redo-testwith the fix: 43 tests, 221 assertions, 0 failures.