Skip to content

⚡️ Add NeedItem.filter_context() to avoid costly {**need} unpacking#1706

Merged
chrisjsewell merged 1 commit into
masterfrom
need_item_filter_context
May 20, 2026
Merged

⚡️ Add NeedItem.filter_context() to avoid costly {**need} unpacking#1706
chrisjsewell merged 1 commit into
masterfrom
need_item_filter_context

Conversation

@chrisjsewell
Copy link
Copy Markdown
Member

@chrisjsewell chrisjsewell commented May 19, 2026

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)

Partially addresses #1699

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)
@chrisjsewell chrisjsewell linked an issue May 19, 2026 that may be closed by this pull request
@codecov
Copy link
Copy Markdown

codecov Bot commented May 19, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.31%. Comparing base (4e10030) to head (a045ff2).
⚠️ Report is 279 commits behind head on master.

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     
Flag Coverage Δ
pytests 89.31% <100.00%> (+2.43%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@chrisjsewell chrisjsewell requested a review from ubmarco May 19, 2026 10:50
@chrisjsewell chrisjsewell merged commit b9ce9f4 into master May 20, 2026
24 checks passed
@chrisjsewell chrisjsewell deleted the need_item_filter_context branch May 20, 2026 08:27
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.

Improvements to NeedItem.__getitem__ peformance

2 participants