perf: optimize merge overlap check with coordinate cache#46
Merged
Conversation
The merge overlap check was O(n^2) with string parsing on every comparison: for each new merge region, all existing references were re-parsed from their string form (e.g. "A1:C3") into numeric coordinates. This caused measurable slowdown when adding 500+ merge regions. Add a `cached_coords` field (serde-skipped) to `MergeCells` that stores pre-parsed `(min_col, min_row, max_col, max_row)` tuples in parallel with the reference strings. The cache is lazily populated on first overlap check (handling worksheets deserialized from XML) and maintained incrementally on add/remove. Reference string shifting (row/col insert) invalidates the cache so it rebuilds on next use. This eliminates all string parsing from the hot path: overlap checks become pure numeric comparisons at 16 bytes per region, with negligible memory overhead for the common case (< 50 merge regions). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Owner
Author
|
리뷰 코멘트 공유드립니다.
참고로 로컬에서 PR 브랜치 기준 |
The cached_coords field is a runtime performance cache that is not serialized. With the derived PartialEq, two MergeCells with identical XML content could compare as unequal depending on whether the cache had been populated. Implement PartialEq manually to compare only count and merge_cells, making equality independent of cache state. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Code reviewNo issues found. Checked for bugs and CLAUDE.md compliance. |
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.
Summary
cached_coords: Vec<(u32, u32, u32, u32)>field toMergeCellswith#[serde(skip)]for zero serialization impactPerformance Impact
cell_name_to_coordinatesper region). For 500 sequential inserts, ~125,000 string-parsing operations.Changes
crates/sheetkit-xml/src/worksheet.rs: Addcached_coordsfield toMergeCellscrates/sheetkit-core/src/merge.rs:ensure_cache()helper + cached overlap checkcrates/sheetkit-core/src/stream.rs: Initialize cache in StreamWritercrates/sheetkit-core/src/workbook/sheet_ops.rs: Invalidate cache on row/col shiftsTest plan
cargo clippy --workspacecleancargo fmt --checkpass🤖 Generated with Claude Code