Repository navigation
fix(text): seven regexes read a run of spaces in linear time (#1074) - #1121
Conversation
) The prefilter table delimiter, the `.bzl` assignment scan, the memory empty-link check, the front matter key check, the sed print-only script check, the Inspect AI docstring heading and the novelty version_gate tag each had `\s*` on both sides of an optional token (`\s*\|?\s*`, `\s*,?\s*=`, `\s*(?:<>)?\s*`, `[{\[]?\s*`, `)?\s*p`, `\s*:?\s*$`, `\S*\s*\d`). A run of whitespace that ended in a failure was split between the two runs at every position, so 6000 spaces took 16 times as long as 1500 (0.17 s for `" " * 6000 + "|"` against the table delimiter). Each run is now written once, as `\s*(?:\|\s*)?`, `\s*(?:,\s*)?=` and the like, so a run has one way to be read. The table delimiter is written `\s*(?:\|\s*)?:?-+:?(?:\s*\|\s*:?-+:?)*\s*(?:\|\s*)?`: the issue's form for the leading pipe alone leaves `:?-+:?\s*` followed by the final `\|?\s*` quadratic (`"-" + " " * n + "x"`). The strings matched, the groups and the match ends are unchanged: the new test keeps each old regex and compares it with the new one on hand tables and on seeded random strings. The assignment pattern moves to a module constant (`_BZL_BINDING`) so the test can read it. Signed-off-by: Anton Volkov <antonwolfy@gmail.com>
|
✅ OpenCodeReview: Review complete: 0 finding(s) across 9 selected item(s). CI at `6597fde`: 1 cancelled, 24 passing, 3 skipped
Review details
|
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files
🚀 New features to boost your workflow:
|
napetrov
left a comment
There was a problem hiding this comment.
Change class: LOCAL (head 6597fde)
- Contract: none
- Responsibility: none
- Dependency: none
- Review depth: skim: local change, check diff + tests
- Coverage gaps: none
All seven regex rewrites preserve the matches used by their callers. I checked the table delimiter, Starlark binding groups, empty links, YAML keys, sed print commands, Inspect section headings and novelty version gates. All seven differential tests passed, including the captured groups and match endpoints used by the scanners. The focused integration runs with #1118 and #1099 passed 117 and 37 tests, respectively.
xaleryb
left a comment
There was a problem hiding this comment.
Approved on 6597fde. Ran the head merged with current main: 280000 random inputs over the seven patterns give identical spans and groups for the old and the new pattern, runs of spaces at 10x and 100x are linear on the head and quadratic on main, the new test file passes (14 of its tests fail on main's code, the equivalence tests pass on both), and the full suite, ruff, mypy, bandit, growth and comment-block gates are clean. CI is green and no thread is open.
Summary
Seven regexes had whitespace matched on both sides of an optional token (
\s*\|?\s*,\s*,?\s*=,\s*(?:<>)?\s*,[{\[]?\s*,)?\s*p,\s*:?\s*$,\S*\s*\d), so a run of spaces that ended in a failure was split between the two runs at every position and the time grew with the square of the run. #1074 names the first two; the same search overdokimi/dokimi/found five more of the same shape that go through the same reproduction. Onmain, CPU time for 1500 and for 6000 spaces:prefilter._TABLE_DELIMITER(fullmatch)" " * n + "|"prefilter._TABLE_DELIMITER(fullmatch)"|--" + " " * n + "x"prefilter._TABLE_DELIMITER(fullmatch)"-" + " " * n + "x"l0/adaptersassignment scan (finditer,re.M)"a" + " " * n + "x"memory/store._EMPTY_LINK(match)"](" + " " * n + "x"frontmatter_shape.YAML_KEY_RE(match)" " * n + "x"shell_sed._PRINT_ONLY(fullmatch)" " * n + "x"inspect_ai._DOC_SECTION(match)"Args" + " " * n + "x"spansversion_gate(search)"requires" + " " * n + "!"A ratio near 16 for 4x the input is quadratic; linear work gives about 4. With this change every row above is 3.7 to 4.3. The issue's suggested form (
\s*(?:\|\s*)?for the leading pipe alone) leaves the table delimiter quadratic where:?-+:?\s*is followed by the final\|?\s*("-" + " " * n + "x", the third row), so the table pattern is written once for the leading pipe, the runs between cells and the final pipe:\s*(?:\|\s*)?:?-+:?(?:\s*\|\s*:?-+:?)*\s*(?:\|\s*)?. The issue's other repro, many short cells ("|--" + " |--" * n + " x"), is already linear onmain(ratio 4.1) and stays so.Two other regexes of the same cause need a different rewrite or sit in a file other work touches, and are tracked in #1119 (
harnesses/tbench_v1._SIZE_RE) and #1120 (the[^\n]*\)?\]?\s*=check inl0/adapters._bzl_public).Closes #1074
Changes
context/prefilter.py:_TABLE_DELIMITERwrites each whitespace run once.repos/l0/adapters.py: the assignment pattern is\s*(?:,\s*)?=(?!=)and moves from an inline literal to the module constant_BZL_BINDING, so a test can read it._bzl_publicuses it unchanged otherwise.memory/store.py_EMPTY_LINKis\]\(\s*(?:<>\s*)?\);external/frontmatter_shape.pyYAML_KEY_REtakes(?:[{\[]\s*)?;repos/shell_sed.py_PRINT_ONLYtakes the address and its\s*as one optional group beforep;task_formats/imports/inspect_ai.py_DOC_SECTIONtakes\s*(?::\s*)?$;novelty/spans.pyversion_gatetakes\s+(?:\S+\s*)?\d.tests/test_whitespace_run_regexes.py: for each regex, a copy of the old one is compared with the new one on a hand table and on 1500 seeded random strings (a 3000-string table-delimiter corpus, and the assignment pattern's group 1, group 2 and match end), and a growth test withtests/growth_timing.pyfor each repro above.Risk
None for output: each new regex accepts the same strings, with the same groups and match ends, as the one it replaces (the equivalence tests above). Only the time on a long run of whitespace changes. Open PR #1045 edits
novelty/spans.pytwo lines below theversion_gateline this change edits (theorderingrule), and #1099 edits only the docstring ofrepos/shell_sed.py, far from_PRINT_ONLY: neither touches a line this change touches, so a merge with either is clean.Testing
On Linux, at
6597fde9(onmain06341ca):make ci-local: 38 passed, 0 failed, 14 skipped (the matrix legs on other Pythons, semgrep, micromamba, the atheris fuzz run, and jobs that only run on a push). 26001 tests passed, 19 skipped; changed-line coverage 100%.main's code the new test file gives13 failed, 7 passed: the 13 growth tests fail, the first onCPU time grew 160x for 16x the whitespace; the 7 equivalence tests pass there, as they should (the old and the new regex agree). On this branch all 20 pass.fix/linear-citation-and-swebench,fix/linear-code-spansonmain06341ca:pytest -n auto26169 passed, 19 skipped,mypyclean,ruff checkclean.Open questions
None.
Revert
A plain revert restores the old regexes and their quadratic time on a long run of spaces. Nothing is stored.
Checklist
pytestpassesruff,mypy, and schema checks passAGENTS.mdraise/exit and test names the doc, contract or issue it traces to.github/.licenserc.yaml)