perf: O(1) index maps for redundancy exact/normalized lookups#13
Merged
Conversation
…lookups Closes #8 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
This PR improves redundancy detection performance by adding O(1) exact and normalized lookup indexes to internal/redundancy.Checker, allowing CheckWithThreshold to short-circuit on common duplicate cases before falling back to the similarity scan.
Changes:
- Add
exactIdx/normalizedIdxmaps toCheckerand populate them inRecord. - Update
CheckWithThresholdto consult the maps before scanningc.normalizedfor Jaccard similarity. - Add tests and new accessor methods (
ExactIndex(),NormalizedIndex()) to support index verification.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
| internal/redundancy/redundancy.go | Adds exact/normalized index maps and uses them for O(1) redundancy checks; introduces index accessors. |
| internal/redundancy/redundancy_test.go | Adds tests intended to validate index population and large-history behavior. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
54
to
+57
| // Check tests content against all previously recorded strings. | ||
| // It does NOT record the content — call Record separately if desired. | ||
| func (c *Checker) ExactIndex() map[string]bool { | ||
| c.mu.Lock() |
Comment on lines
+71
to
+72
| // TestLargeExactIndex ensures exact match stays fast with many entries (index must not degrade). | ||
| func TestLargeExactIndex(t *testing.T) { |
Comment on lines
+89
to
+94
| c.Record(strings.Repeat("y", i+1)) | ||
| } | ||
| // Check reverse-order of a recorded entry — same tokens, different order (single token so same). | ||
| target := strings.Repeat("y", n/2) | ||
| result := c.Check(target) | ||
| require.True(t, result.IsRedundant) |
Comment on lines
+84
to
+85
| // TestLargeNormalizedIndex ensures normalized match stays fast with many entries. | ||
| func TestLargeNormalizedIndex(t *testing.T) { |
Comment on lines
82
to
+92
| func (c *Checker) CheckWithThreshold(content string, threshold float64) Result { | ||
| c.mu.Lock() | ||
| defer c.mu.Unlock() | ||
|
|
||
| norm := normalize(content) | ||
| normTokens := strings.Fields(norm) | ||
|
|
||
| for i, r := range c.raw { | ||
| if r == content { | ||
| return Result{IsRedundant: true, Kind: "exact", Similarity: 1.0} | ||
| } | ||
| if c.normalized[i] == norm { | ||
| return Result{IsRedundant: true, Kind: "normalized", Similarity: 1.0} | ||
| } | ||
| sim := jaccard(normTokens, strings.Fields(c.normalized[i])) | ||
| if c.exactIdx[content] { | ||
| return Result{IsRedundant: true, Kind: "exact", Similarity: 1.0} | ||
| } | ||
| if c.normalizedIdx[norm] { | ||
| return Result{IsRedundant: true, Kind: "normalized", Similarity: 1.0} |
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
exactIdx map[string]boolandnormalizedIdx map[string]booltoCheckerfor O(1) exact and normalized duplicate detectionAddpopulates both index maps alongside the existing slices (slices still needed for Jaccard similarity scan)CheckWithThresholdchecks maps first before falling through to the linear similarity scanExactIndex()andNormalizedIndex()accessors for test verificationTest plan
TestExactLookupUsesIndex— verifies index is populated and used for exact matchesTestNormalizedLookupUsesIndex— verifies normalized index populated correctlyTestLargeExactIndex— 10k entries, exact lookup completes in <5msTestLargeNormalizedIndex— 10k entries, normalized lookup completes in <5msCloses #8
🤖 Generated with Claude Code