👌 Short-circuit simple filter expressions to avoid eval() overhead#1677
Merged
chrisjsewell merged 10 commits intomasterfrom Mar 31, 2026
Merged
👌 Short-circuit simple filter expressions to avoid eval() overhead#1677chrisjsewell merged 10 commits intomasterfrom
eval() overhead#1677chrisjsewell merged 10 commits intomasterfrom
Conversation
Co-authored-by: chrisjsewell <2997570+chrisjsewell@users.noreply.github.com>
Co-authored-by: chrisjsewell <2997570+chrisjsewell@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Add benchmark tests for issue 1676
Short-circuit simple expressions in Mar 17, 2026
filter_single_need to avoid eval() overhead
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #1677 +/- ##
==========================================
+ Coverage 86.87% 89.30% +2.42%
==========================================
Files 56 72 +16
Lines 6532 10324 +3792
==========================================
+ Hits 5675 9220 +3545
- Misses 857 1104 +247
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
chrisjsewell
approved these changes
Mar 31, 2026
filter_single_need to avoid eval() overheadeval() overhead
ubmarco
approved these changes
Mar 31, 2026
Member
ubmarco
left a comment
There was a problem hiding this comment.
LGTM! Performance is always important.
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.
Short-circuit simple filter expressions to avoid
eval()overheadCloses #1676
Summary
This PR introduces an AST-based fast path (
sphinx_needs.ubquery) that compiles simple filter expressions into native Python callables, bypassingeval()entirely for common patterns. The filter string is parsed once viaast.parse, and if the expression is a supported pattern (comparisons, membership tests, booleanand/or/not,search()), a lightweight predicate closure is returned and cached (LRU, 256 entries). Complex expressions that can't be short-circuited (those referencingneeds,current_need, orc) fall through to the existingeval()path unchanged.The fast path is applied at two levels:
filter_needs_and_parts) — used byneedtable,needlist,needflow, and all fallback paths fromfilter_needs_view/filter_needs_parts. One predicate build, N native lambda applications.filter_single_need) — used by link condition evaluation, constraints, gantt, graphviz, plantuml, sequence diagrams, and dynamic functions. LRU-cached predicates avoid per-call overhead.Both layers compose with the existing index-based pre-filtering in
_analyze_and_apply_expr, which narrows the candidate set viaNeedsViewindexes before the predicate loop runs.Benchmark results
Using
tox -e py314-benchmark -- tests/benchmarks/test_querying.py --benchmark-columns=mean --benchmark-time-unit=ms -k 1000:Before (AST fast path disabled):
test_filter_single_need_simple[1000]test_filter_needs_and_parts_simple[1000]test_filter_needs_and_parts_compound[1000]test_filter_needs_and_parts_complex_eval[1000]test_resolve_links_constrained[1000]test_resolve_links_unique_conditions[1000]After, without LRU caching (AST fast path enabled, but predicate rebuilt each call):
test_filter_single_need_simple[1000]test_filter_needs_and_parts_simple[1000]test_filter_needs_and_parts_compound[1000]test_filter_needs_and_parts_complex_eval[1000]test_resolve_links_constrained[1000]test_resolve_links_unique_conditions[1000]After (AST fast path enabled):
test_filter_single_need_simple[1000]test_filter_needs_and_parts_simple[1000]test_filter_needs_and_parts_compound[1000]test_filter_needs_and_parts_complex_eval[1000]test_resolve_links_constrained[1000]test_resolve_links_unique_conditions[1000]Batch filtering with the fast path is ~37x faster for simple filters and ~9x faster for compound
andexpressions. Per-needfilter_single_needis ~34x faster. End-to-endresolve_linkswith constrained links is ~5.6x faster. Thecomplex_evalbenchmark (which forces theeval()fallback viacurrent_need) is unaffected, confirming the slow path is unchanged.The "without LRU caching" column isolates the benefit of native closures vs
eval()from the caching benefit. Batch functions (filter_needs_and_parts) build the predicate once regardless, so caching has no effect (0.18ms vs 0.17ms). Per-need functions (filter_single_need) pay theast.parsecost on every call without caching (3.13ms vs 0.34ms), but are still ~3.7x faster thaneval()(11.45ms).resolve_linkswith repeated conditions benefits most from caching (5.90ms vs 13.48ms).Pathway to per-type fields
Beyond the speed win, this architecture opens a pathway to per-type fields (discussion #1646). Because field names are resolved lazily — only when the predicate actually reaches them at runtime — short-circuit evaluation in
and/orexpressions means a filter like:will never access
spec_fieldfor needs whosetypeis not"spec". This is not possible witheval(), which eagerly populates the entire namespace with all field names before evaluation begins, requiring every field to exist on every need type.What changed
sphinx_needs/ubquery.py— AST-based filter compiler withtry_build_simple_predicate()as the public entry point. Supports==,!=,<,<=,>,>=,in,not in,search(), bare names,not,and,or. Bails out (returnsNone) for expressions referencing context-only names (needs,current_need,c) or unsupported AST patterns.sphinx_needs/filter_common.py:filter_needs_and_parts()— new fast path: triestry_build_simple_predicate()before the per-itemcompile()+eval()loop. Falls through toeval()only for complex expressions.filter_single_need()— always attempts the AST-based fast path before falling back toeval(). All callers (constraints, gantt, graphviz, plantuml, sequence, dynamic functions, link conditions) benefit automatically.sphinx_needs/directives/need.py—resolve_linkslink condition evaluation now benefits from the always-on fast path (removed the previoussimple_filter=Trueparameter which is no longer needed).tests/test_ubquery.py— Comprehensive tests for predicate building, field resolution, context-only name blocklist,filter_datafallback, andfilter_single_needintegration.tests/benchmarks/test_constrained_links_benchmark.py— Benchmarks forfilter_single_need,filter_needs_and_parts(simple, compound, and eval-fallback), andresolve_linkswith constrained links.