Repository navigation
perf: delegate Attrs.__contains__ to the backing dict - #4347
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files
|
|
The documentation preview is ready to be viewed at https://awkward-array.org/doc/pr/4347/ |
ianna
left a comment
There was a problem hiding this comment.
@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?
Co-authored-by: Ianna Osborne <ianna.osborne@cern.ch>
Co-authored-by: Ianna Osborne <ianna.osborne@cern.ch>
|
Making it |
ianna
left a comment
There was a problem hiding this comment.
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!
|
Added a test for |
Attrsinherits__contains__from theMappingmixin, which implements it with a try/except around__getitem__. The named axis key is looked up on every__getitem__of a highlevel array.