fix: BoundedBacktracker span-based CanHandle + ReplaceAllStringFunc O(n)#128
Merged
fix: BoundedBacktracker span-based CanHandle + ReplaceAllStringFunc O(n)#128
Conversation
…(n) performance SearchAtWithState checked CanHandle(len(haystack)) against full haystack, rejecting valid searches when remaining span [at, len] fit within budget. LogParser on 7MB input returned 22004 matches instead of 33089. Fix: span-based visited table sizing matching Rust regex's Input span model. Visited positions stored relative to SpanStart. Full haystack preserved for zero-width assertions (\b) needing backward context. ReplaceAllStringFunc replaced O(n^2) string concatenation with strings.Builder for O(n) performance (2m19s -> 1.3s on 150K matches). Fixes #127
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Benchmark ComparisonComparing Summary:
|
4 tasks
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
SearchAtWithState(haystack, at, state)now checksCanHandle(len(haystack) - at)instead ofCanHandle(len(haystack)). The visited table is sized for the search span only, with positions stored relative toSpanStart. Full haystack is preserved for\bassertions. This matches Rust regex'sInputspan model (backtrack.rsline 1848).result += stringO(n²) concatenation withstrings.Builder. On 150K replacements over 6MB string: 2m19s → 1.3s.TestBoundedBacktracker_SearchAtWithState_SpanBased(large haystack span test) andTestBoundedBacktracker_SearchAtWithState_WordBoundary(\bcontext preservation).Fixes #127. Reported by @kostya via #124.
Test plan
go test ./...— all 9 packages passgofmt -l .— no formatting issuesReplaceAllStringFunc150K replacements — results identical, 1.3s vs 2m19s\bfoo\bwith search starting mid-haystack preserves backward context