Add a native Container.__contains__#487
Open
tfoutrein wants to merge 1 commit into
Open
Conversation
Container inherits __contains__ from MutableMapping, so `key in container` goes through __getitem__ -> item(), which resolves and returns the value (and constructs a NonExistentKey on every absent key) only to discard it. Resolve the key exactly as item() does (str -> SingleKey; a non-str/non-Key argument still raises TypeError) and probe _map directly. For an out-of-order table the OutOfOrderTableProxy is still built so its validation runs as before. This also speeds up __setitem__, which does `if key in self` on every assignment. Behaviour-identical: 969 tests pass (incl. toml-test); a differential `in` check over present/absent/out-of-order/dotted/non-str keys matches master exactly. ~1.17x faster on membership (drift-immune A/B). Part of python-poetry#483. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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.
Summary
First of the small, self-contained, conformance-preserving performance changes discussed in #483.
Containerinherits__contains__fromMutableMapping, sokey in containergoes throughself[key]→__getitem__→item(), which resolves and returns the value — and constructs aNonExistentKeyexception on every absent key — only to discard it.This adds a native
Container.__contains__that resolves the key exactly asitem()does (str→SingleKey; a non-str/non-Keyargument still raisesTypeError) and probesself._mapdirectly. For an out-of-order table (a tuple index) it still builds theOutOfOrderTableProxy, so its validation runs exactly as before. This also speeds upContainer.__setitem__, which doesif key in selfon every assignment.Behaviour — no change
Beyond the existing suite, I ran a differential
incheck (present / absent / out-of-order / dotted-string / non-strkeys) comparing this branch againstmaster: identical results, including theTypeErroron a non-string key and the out-of-order proxy path.Benchmark
Drift-immune A/B in a single process (the new
__contains__vs the inherited__getitem__-based path, interleaved so thermal drift cancels), CPython 3.11, on a mix of present + absent keys:The win is mostly from not constructing/raising
NonExistentKeyon absent-key membership.Verification
pytest tests/(incl. thetoml-testconformance submodule): 969 passed, unchanged frommaster.ruff check+ruff format --check: clean.mypy(1.19.1): no new errors. The single# type: ignore[arg-type]mirrors the existingSingleKey(key)call initem()(which also raisesTypeErroron a non-string argument).Context: this is harness-assisted work — every change is individually benchmarked and gated (full test suite incl. conformance, a differential guard, ruff + mypy) before it can land, and I review and stand behind each PR personally. Happy to slice the rest of #483 into further small PRs.