Skip to content

perf: delegate Attrs.__contains__ to the backing dict - #4347

Merged
ianna merged 10 commits into
scikit-hep:mainfrom
ikrommyd:perf-improve-attrs-contains
Oct 19, 2026
Merged

ianna merged 10 commits into
scikit-hep:mainfrom
ikrommyd:perf-improve-attrs-contains

Conversation

@ikrommyd

Copy link
Copy Markdown
Member

Attrs inherits __contains__ from the Mapping mixin, which implements it with a try/except around __getitem__. The named axis key is looked up on every __getitem__ of a highlevel array.

@github-actions github-actions Bot added the type/perf PR title type: perf (set automatically) label Sep 12, 2026
@codecov

codecov Bot commented Sep 13, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 84.30%. Comparing base (c3589eb) to head (90b73a0).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
Files with missing lines Coverage Δ
src/awkward/_attrs.py 94.23% <100.00%> (+0.48%) ⬆️

@ikrommyd
ikrommyd marked this pull request as ready for review September 13, 2026 10:37
github-actions Bot added a commit that referenced this pull request Sep 15, 2026
@github-actions

Copy link
Copy Markdown
Contributor

The documentation preview is ready to be viewed at https://awkward-array.org/doc/pr/4347/

ikrommyd added a commit to ikrommyd/awkward that referenced this pull request Oct 9, 2026

@ianna ianna left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ikrommyd -- Thanks! This is an excellent, low-risk performance improvement.

Even though Attrs.__getitem__ directly delegates to self._data[key] without adding any heavy custom logic, explicitly overriding __contains__ is still highly valuable. It eliminates Python's control-flow exception overhead (try/except KeyError) when querying optional or non-existent metadata keys. By steering the lookup entirely into the native C-implemented dictionary hash table via key in self._data, we achieve a pure, optimal O(1) check.

I have just a minor suggestion and since we are here -- Attrs implements a __setitem__ method, which means this class is intended to be mutable?

Comment thread src/awkward/_attrs.py Resolved
Comment thread src/awkward/_attrs.py Resolved
ikrommyd and others added 4 commits October 18, 2026 14:21
Co-authored-by: Ianna Osborne <ianna.osborne@cern.ch>
Co-authored-by: Ianna Osborne <ianna.osborne@cern.ch>
@ikrommyd

Copy link
Copy Markdown
Member Author

Making it MutableMapping also required adding __delitem__ but yeah MutableMapping is better since it already implemented __setitem__

@ianna ianna left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Excellent work refactoring this.
Shifting Attrs to inherit from MutableMapping while adding the required __delitem__ method fully fixes the structural type hierarchy problem. Combined with the optimized __contains__ override bypassing the try/except KeyError stack, this is a clean win for both performance and correctness. Just add a quick test to delete an attribute to hit that final 1 line of missing Codecov coverage on __delitem__, and this is good to merge!

@ikrommyd

Copy link
Copy Markdown
Member Author

Added a test for __delitem__ as well. Codecov should become happy in this ci run and it should be good to merge.

@ianna ianna left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ikrommyd — thanks for addressing all comments! Merging this now.

@ianna
ianna enabled auto-merge (squash) October 19, 2026 03:40
@ianna
ianna merged commit f78205f into scikit-hep:main Oct 19, 2026
36 checks passed
@ikrommyd
ikrommyd deleted the perf-improve-attrs-contains branch October 19, 2026 21:52

This branch was previously deployed

1 inactive deployment
docs — 90b73a01 Deployed Oct 19, 2026 by ianna via Remove Branch Preview #80
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type/perf PR title type: perf (set automatically)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants