Skip to content

⚡ Fix quadratic Library.add during parsing and slow quote-enclosing check - #612

Merged
MiWeiss merged 2 commits into
mainfrom
perf/fix-quadratic-add-and-enclosing
Sep 3, 2026
Merged

⚡ Fix quadratic Library.add during parsing and slow quote-enclosing check#612
MiWeiss merged 2 commits into
mainfrom
perf/fix-quadratic-add-and-enclosing

Conversation

@MiWeiss

@MiWeiss MiWeiss commented Sep 3, 2026

Copy link
Copy Markdown
Collaborator

Two speed regressions on main vs v2.0.0b9, found by benchmarking on cryptobib, the ACL anthology and generated files.

1. Parsing is quadratic in the number of comment blocks (since #527). Library.add now defaults to fail_on_duplicate_key=True, and _find_duplicate_keys copied both by-key dicts into sets on every call. The splitter passed False only for entries and strings, so each comment, preamble or failed block cost O(entries so far).
Fix: look keys up in the dicts directly (cost depends only on the blocks being added) and pass False at all splitter call sites. Regression test added.

2. RemoveEnclosingMiddleware slow on quote-enclosed values (since #604). _is_enclosed_in_quotes ran a regex over every value; the brace variant had a fast path, the quote one didn't.
Fix: return early when the value has no inner " (the scan can only return False on one).

Min of 3, seconds:

b9 main this PR
60k entries, % line before each — split() 3.05 29.78 3.18
gen_stress (66 MB, 5.7k comments) — split() 3.40 7.09 3.43
anthology.bib (91 MB, all quoted) — parse_string 9.72 10.99 10.02
crypto.bib (41 MB) — parse_string 4.52 5.40 4.77
40k single Library.add() calls 0.017 8.50 0.024

Tests: 2720 passed.

🤖 Generated with Claude Code

MiWeiss and others added 2 commits September 3, 2026 21:28
`_find_duplicate_keys` copied all existing entry and string keys into
sets on every call, making `add()` O(library size) per call and
parsing quadratic in the number of comment/preamble/failed blocks
(the splitter added those with the default `fail_on_duplicate_key=True`).
Look keys up in the by-key dicts directly and pass
`fail_on_duplicate_key=False` for all blocks added by the splitter.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
`_is_enclosed_in_quotes` (added in #604) scanned every quoted value with
a regex, making the default parse stack ~4x slower on files with quoted
values. Skip the scan when the value contains no inner quote, and avoid
the slice copy in the corresponding brace fast path.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@MiWeiss
MiWeiss force-pushed the perf/fix-quadratic-add-and-enclosing branch from 8a612c1 to 80792c1 Compare September 3, 2026 19:39
@MiWeiss
MiWeiss merged commit d024db3 into main Sep 3, 2026
16 checks passed
@MiWeiss
MiWeiss deleted the perf/fix-quadratic-add-and-enclosing branch September 3, 2026 19:59
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