Add a native OutOfOrderTableProxy.__contains__#515
Open
tfoutrein wants to merge 1 commit into
Open
Conversation
OutOfOrderTableProxy was the last mapping type still inheriting the MutableMapping.__contains__ mixin, which implements `key in proxy` as `try: proxy[key]` -- routing through __getitem__, which resolves the value (and builds a NonExistentKey on every absent key) only to discard it. Add a native __contains__ that returns `key in self._internal_container`, exactly the predicate __getitem__ already uses as its guard and itself a native Container.__contains__ (which still rebuilds the proxy for an out-of-order key so its validation runs as before). This completes python-poetry#483, which gave Container, Table and InlineTable native __contains__. No behaviour change: the membership result, the raised exception type and document (non-)mutation are all identical to the inherited path. Roughly 2.1x faster on a present key and 1.3x on an absent one.
1a930a2 to
545d2e5
Compare
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.
What
OutOfOrderTableProxywas the last mapping type still inheriting theMutableMapping.__contains__mixin. That mixin implementskey in proxyastry: proxy[key]— routing through__getitem__, which resolves the value (and builds aNonExistentKeyon every absent key) only to throw it away.Add a native
__contains__:This is exactly the predicate
__getitem__already uses as its guard (if key not in self._internal_container: raise NonExistentKey), and it is itself a nativeContainer.__contains__— which still rebuilds the proxy for an out-of-order key so its validation runs exactly as before.This completes #483, which added native
__contains__toContainer,TableandInlineTable.Iso-functional
Both the old (mixin) and new path bottom out on the same
Container.__contains__, so the membership result, the raised exception type and document (non-)mutation are identical. Verified by:OutOfOrderTableProxy(present / absent /Key-object / non-str→TypeError/ no document mutation);as_string()all byte-identical tomaster.Benchmark
Membership on an out-of-order proxy (median, interleaved A/B vs
master):The parse path is untouched (no measurable change there).