Skip to content

Replace full-snapshot undo history with in-place edits and patches - #357

Draft
nabeya11 wants to merge 2 commits into
masterfrom
perf/patch-undo
Draft

Replace full-snapshot undo history with in-place edits and patches#357
nabeya11 wants to merge 2 commits into
masterfrom
perf/patch-undo

Conversation

@nabeya11

@nabeya11 nabeya11 commented Aug 6, 2026

Copy link
Copy Markdown
Member

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

  • Edits are applied in place and the history records only the inverse patch of each edit. Only whole-cloud replacement (re-import, full voxel_grid) keeps a compressed snapshot.
  • Patches are serialized, LZF-compressed and stored in the JS heap (Uint8Array) to keep the WASM linear memory from growing — the same storage location as before.
  • Deletion compacts the cloud in place keeping the spare capacity, and undo re-expands it with a backward walk without extra allocation.
  • The non-js stub (historyDummy) is replaced by a real implementation (historyMem), making undo behavior testable with plain go test. Added round-trip tests that apply random edit sequences, undo them all, and assert byte-exact restoration.
  • With in-place edits, the pre-edit PointCloud object is mutated after an edit. Existing tests that relied on it being untouched now create a fresh cloud per test case.

nabeya11 and others added 2 commits August 2, 2026 15:27
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

codecov Bot commented Aug 6, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 79.61957% with 75 lines in your changes missing coverage. Please review.
✅ Project coverage is 47.70%. Comparing base (009b647) to head (7c2347c).

Files with missing lines Patch % Lines
patch.go 76.56% 33 Missing and 27 partials ⚠️
editor.go 90.00% 8 Missing ⚠️
undo.go 80.64% 4 Missing and 2 partials ⚠️
command.go 0.00% 1 Missing ⚠️
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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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 thread patch.go
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 thread patch.go
Comment on lines +323 to +327
func packPatch(p patch) []byte {
var buf bytes.Buffer
p.encode(&buf)
return packEntry(buf.Bytes())
}
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.

2 participants