⚡️ Add NeedItem.filter_context() to avoid costly {**need} unpacking#1706
Merged
Conversation
Addresses #1699: the `{**need}` pattern triggers __getitem__ for every key (including link fields which allocate fresh list[str] each call), resulting in O(N_keys × M_links) allocations per need per filter/render invocation. The new `filter_context()` method on both NeedItem and NeedPartItem builds a flat dict by directly combining internal data structures, bypassing the 7-namespace lookup chain and materializing link string lists only once. Replace all `{**need}` / `{**need_info}` usages in: - filter_common.py (eval slow-path) - needflow/_graphviz.py (match_variants context) - needflow/_plantuml.py (template rendering + match_variants) - needuml.py (template rendering) - api/need.py (jinja content + template rendering)
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1706 +/- ##
==========================================
+ Coverage 86.87% 89.31% +2.43%
==========================================
Files 56 72 +16
Lines 6532 10344 +3812
==========================================
+ Hits 5675 9239 +3564
- Misses 857 1105 +248
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:
|
ubmarco
approved these changes
May 20, 2026
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.
The
{**need}pattern triggers__getitem__for every key (including link fields which allocate freshlist[str]each call), resulting in O(N_keys × M_links) allocations per need per filter/render invocation.The new
filter_context()method on bothNeedItemandNeedPartItembuilds a flat dict by directly combining internal data structures, bypassing the 7-namespace lookup chain and materializing link string lists only once.Replace all
{**need}/{**need_info}usages in:Partially addresses #1699