Skip to content

perf: replace causal scan in find_last_delete_op with peer-by-peer range scan#978

Merged
zxch3n merged 1 commit into
loro-dev:mainfrom
camdenslade:perf/find-last-delete-op
May 7, 2026
Merged

perf: replace causal scan in find_last_delete_op with peer-by-peer range scan#978
zxch3n merged 1 commit into
loro-dev:mainfrom
camdenslade:perf/find-last-delete-op

Conversation

@camdenslade
Copy link
Copy Markdown
Contributor

@camdenslade camdenslade commented May 4, 2026

find_last_delete_op was flagged with FIXME: PERF for iterating all changes in the oplog using iter_changes_causally_rev, which performs a DAG-ordered merge across all peers - O(total changes).

Furthermore, any delete op that covers id must have causally observed it, meaning the deleting peer's counter at delete time was strictly greater than id.counter. The version vector at id (start_vv) is therefore a valid lower bound per peer; thus changes at or before start_vv[peer] predate id and cannot have deleted it.

Switching to iter_changes_peer_by_peer(&start_vv, oplog.vv()) skips those changes without needing causal ordering. Since a given ID can only be deleted once, there is typically exactly one match; the lamport comparison is a safety net for any edge case.

The existing test suite passes without changes.

@camdenslade camdenslade force-pushed the perf/find-last-delete-op branch from c14396e to 13e6ede Compare May 4, 2026 20:09
…nge scan

iter_changes_causally_rev walks every change in the oplog in DAG order,
which is O(total changes). Any delete op covering `id` must have observed
it, so start_vv (the vv at `id`) is a valid lower bound per peer. Switching
to iter_changes_peer_by_peer with that bound skips all changes that predate
`id` without needing causal ordering. The match with the highest lamport
timestamp is the latest delete.
@camdenslade camdenslade force-pushed the perf/find-last-delete-op branch from b19fbcd to 3c49ceb Compare May 6, 2026 16:20
@zxch3n
Copy link
Copy Markdown
Member

zxch3n commented May 7, 2026

The original impl actually has similar time complexity as the new one, it only scans the changes inside the start_vv -> latest_vv. But the new impl should be more accurate

@zxch3n zxch3n merged commit 095ad56 into loro-dev:main May 7, 2026
1 check passed
@camdenslade camdenslade deleted the perf/find-last-delete-op branch May 7, 2026 12:50
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