Replace full-snapshot undo history with in-place edits and patches - #357
Draft
nabeya11 wants to merge 2 commits into
Draft
Replace full-snapshot undo history with in-place edits and patches#357nabeya11 wants to merge 2 commits into
nabeya11 wants to merge 2 commits into
Conversation
Introduce inverse-edit patches (label/delete/append/replace) with a binary codec and LZF-compressed entry framing. This is the basis for replacing full-snapshot undo history: label edits cost 8B per changed point and deletions stride+4B per removed point instead of a full copy of the cloud. Promote golzf to a direct dependency. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Every edit used to clone the whole cloud in the WASM heap and push a full copy to the JS heap, retaining up to maxHistory+1 snapshots (~750MB for a 150MB cloud). Edits now mutate the cloud in place and record only the inverse patch, compressed on the JS heap. - label/relabel/unlabel record changed labels only (8B/point). - delete compacts in place keeping spare capacity, so undo re-expands without allocation. - merge records an O(1) append patch. - Whole-cloud replacement (re-import, full voxel filter) stores one compressed snapshot; ownership of the old Data moves to history. - The non-js history stub becomes a real implementation (historyMem), making undo behavior testable with plain go test. Randomized round-trip tests assert byte-exact restoration. - pcgol caches an unsafe float32 alias of Data keyed by its base pointer, so length changes are delivered via a fresh PointCloud (newCloudView) sharing the same backing array. Undo depth semantics of max_history are unchanged (N entries = N undos). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #357 +/- ##
==========================================
+ Coverage 38.96% 47.70% +8.73%
==========================================
Files 8 9 +1
Lines 1414 1698 +284
==========================================
+ Hits 551 810 +259
+ Misses 827 823 -4
- Partials 36 65 +29 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull request overview
This PR reworks the editor undo mechanism to avoid full point-cloud cloning per edit by applying edits in place and recording inverse “patches” for undo, with JS/WASM builds storing compressed patch bytes in JS-managed memory to reduce WASM linear-memory residency.
Changes:
- Replace snapshot-based undo history with patch-based undo (including squash support for multi-step commands).
- Introduce patch encoding/decoding + LZF compression framing, and update editor operations to emit inverse patches.
- Add non-JS history implementation and round-trip tests validating undo restores byte-exact point clouds.
Reviewed changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| undo.go | New non-JS in-memory patch history implementation (historyMem). |
| undo_js.go | JS/WASM history now stores packed patch chunks in JS Uint8Arrays and reverts via patch decoding. |
| patch.go | Adds patch types, encoding/decoding, and entry framing + LZF compression utilities. |
| patch_test.go | Unit tests for patch revert behavior and encode/decode + compression framing round-trips. |
| history_test.go | End-to-end randomized editor edit/undo round-trip tests and history depth/squash tests. |
| go.mod | Promotes github.com/zhuyie/golzf to a direct dependency. |
| editor.go | Moves label/delete/merge and SetPointCloud behavior to in-place mutation + inverse patch recording. |
| command.go | Updates voxel-filter selected-path undo grouping to use squashLatest() instead of pop(). |
| command_test.go | Adjusts tests for in-place mutation semantics by creating fresh clouds per test case. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+290
to
+302
| func packEntry(raw []byte) []byte { | ||
| out := make([]byte, 5+len(raw)) | ||
| binary.LittleEndian.PutUint32(out[:4], uint32(len(raw))) | ||
| if len(raw) > 5 { | ||
| if n, err := lzf.Compress(raw, out[5:]); err == nil && n > 0 && n < len(raw) { | ||
| out[4] = entryFlagLZF | ||
| return out[:5+n] | ||
| } | ||
| } | ||
| out[4] = entryFlagRaw | ||
| copy(out[5:], raw) | ||
| return out | ||
| } |
Comment on lines
+323
to
+327
| func packPatch(p patch) []byte { | ||
| var buf bytes.Buffer | ||
| p.encode(&buf) | ||
| return packEntry(buf.Bytes()) | ||
| } |
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.
Problem
Every edit cloned the whole point cloud in the WASM heap and pushed another full copy to the JS heap as undo history. For large PCDs the history alone kept several times the file size resident. Since the WASM linear memory never shrinks, the per-edit clone also turned the peak into permanent residency.
Changes
go test. Added round-trip tests that apply random edit sequences, undo them all, and assert byte-exact restoration.PointCloudobject is mutated after an edit. Existing tests that relied on it being untouched now create a fresh cloud per test case.