Skip to content

Fix O(n²) performance issue with single quote smart quote matching#172

Merged
dereuromark merged 1 commit intomasterfrom
feature/smart-quote-performance
Apr 1, 2026
Merged

Fix O(n²) performance issue with single quote smart quote matching#172
dereuromark merged 1 commit intomasterfrom
feature/smart-quote-performance

Conversation

@dereuromark
Copy link
Copy Markdown
Contributor

Summary

  • Pre-compute all single quote opener/closer matches once per text block instead of re-scanning for each potential opener
  • Reduces algorithmic complexity from O(n²) to O(n) for documents with many single quotes

Performance Improvement

Test Case Before After Speedup
500 single quotes 368 ms 4.8 ms ~77x
1000 single quotes 2545 ms 14 ms ~180x
2000 single quotes N/A 39 ms linear

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, then parseSmartQuote() simply looks up the pre-computed result in O(1) time.

@codecov
Copy link
Copy Markdown

codecov bot commented Apr 1, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.62%. Comparing base (6b165b4) to head (7d04ac0).
⚠️ Report is 2 commits behind head on master.

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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)
@dereuromark dereuromark force-pushed the feature/smart-quote-performance branch from 1d3a2b3 to 7d04ac0 Compare April 1, 2026 21:36
@dereuromark dereuromark merged commit 16db29a into master Apr 1, 2026
5 of 6 checks passed
@dereuromark dereuromark deleted the feature/smart-quote-performance branch April 1, 2026 21:49
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.

1 participant