Repository navigation
🐛 fix(select): raise MemoryError on selector OOM - #1233
Merged
Merged
Conversation
gaborbernat
force-pushed
the
fix/alloc-selector-compile
branch
from
October 8, 2026 12:29
c94a587 to
29f7ef8
Compare
Merging this PR will degrade performance by 24.51%
|
| Benchmark | BASE |
HEAD |
Efficiency | |
|---|---|---|---|---|
| ❌ | test_feature[shadow-slot-comments] |
83.3 µs | 110.4 µs | -24.51% |
Tip
Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.
Comparing gaborbernat:fix/alloc-selector-compile (29f7ef8) with main (74c1b54)
Footnotes
-
32 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩
A failed buffer growth in the selector parser set the same flag as invalid syntax. select(), select_one(), css_to_xpath() and the rewriter then reported the allocation failure as SelectorSyntaxError, and a forgiving :is() or :where() list dropped the arm and matched with the rest. A failed build of the document's element index fell back to a full walk and returned an answer as if nothing had failed. The parser now records an allocation failure with no reason, which sel_raise turns into MemoryError and a forgiving list stops on. select(), select_one() and find_all() build the index before their walk and raise MemoryError when the build fails. The check sits outside the walking functions because a failure branch inside them stopped GCC's LTO from inlining the matcher into the loop, which cost select 3,000 instructions a call under callgrind. The regression tests sit in tests/fuzz_build next to the operation-limit checks, because only the fuzz build defines the allocation-failure hook; fuzz-smoke runs them under ASan and the normal matrix skips them.
gaborbernat
force-pushed
the
fix/alloc-selector-compile
branch
from
October 8, 2026 15:43
29f7ef8 to
8a0ce6d
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.
An allocation failure while compiling a selector set the same flag as a syntax error. Failing the third PyMem allocation of
document.select("main > p[zq] a, :is(b, i, [zq]) :has(> a)")raisedSelectorSyntaxError("unexpected token") for a valid selector. The same failure inside a forgiving:is()or:where()list dropped the arm as if it were invalid and returned a result. A failed build of the document's element index fell back to a full walk and hid the failure.The parser now records an allocation failure as an error with no reason, which every caller,
css_to_xpath()and the rewriter included, raises asMemoryError, and a forgiving list stops on it, since Selectors 4 §16.1 ignores only selectors that fail to parse.select(),select_one()andfind_all()build the index before they walk and raiseMemoryErrorwhen the build fails; the same check inside the walk costselect3,000 instructions a call.libxml2 2.16.0 gives a failed XPath compile its own
XPATH_MEMORY_ERROR, and lexbor 3.1.0's CSS parser stops withLXB_STATUS_ERROR_MEMORY_ALLOCATIONinstead of reporting an unexpected token. Their Python bindings lose that: lxml 7.0.0b1 wraps every failed compile inXPathSyntaxError, and selectolax 1.0.0 raisesSelectolaxError("Can't parse CSS selector."). Servo'sselectors0.41.0 uses Rust's infallible allocation, which aborts the process.