Fix O(n²) performance issue with single quote smart quote matching#172
Merged
dereuromark merged 1 commit intomasterfrom Apr 1, 2026
Merged
Fix O(n²) performance issue with single quote smart quote matching#172dereuromark merged 1 commit intomasterfrom
dereuromark merged 1 commit intomasterfrom
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #172 +/- ##
============================================
- Coverage 93.98% 93.62% -0.37%
- Complexity 3139 3175 +36
============================================
Files 93 93
Lines 8017 8076 +59
============================================
+ Hits 7535 7561 +26
- Misses 482 515 +33 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Pre-compute all single quote opener/closer matches once per text block instead of re-scanning the text for each potential opener. This reduces complexity from O(n²) to O(n) for documents with many single quotes. Performance improvement for 1000 single quotes: - Before: 2545 ms - After: 14 ms (~180x faster)
1d3a2b3 to
7d04ac0
Compare
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
Performance Improvement
The issue was that
findMatchingSingleQuoteCloser()was called for each potential opener, and each call scanned the entire remaining text. With many single quotes, this became quadratic.The fix adds a
buildSingleQuoteMatchCache()method that pre-computes all matches in a single pass, thenparseSmartQuote()simply looks up the pre-computed result in O(1) time.