Skip to content

Add a native OutOfOrderTableProxy.__contains__#515

Open
tfoutrein wants to merge 1 commit into
python-poetry:masterfrom
AstekGroup:perf/ooo-contains
Open

Add a native OutOfOrderTableProxy.__contains__#515
tfoutrein wants to merge 1 commit into
python-poetry:masterfrom
AstekGroup:perf/ooo-contains

Conversation

@tfoutrein

Copy link
Copy Markdown
Contributor

What

OutOfOrderTableProxy was the last mapping type still inheriting the MutableMapping.__contains__ mixin. That mixin 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 throw it away.

Add a native __contains__:

def __contains__(self, key: object) -> bool:
    return key in self._internal_container

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 native Container.__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__ to Container, Table and InlineTable.

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:

  • the full test suite incl. the toml-test conformance submodule;
  • a new focused test exercising a real OutOfOrderTableProxy (present / absent / Key-object / non-str→TypeError / no document mutation);
  • a differential over many out-of-order / nested-proxy / dotted-key / AoT documents and post-mutation states — membership result, exception type and as_string() all byte-identical to master.

Benchmark

Membership on an out-of-order proxy (median, interleaved A/B vs master):

speedup
present key ~2.1×
absent key ~1.3×

The parse path is untouched (no measurable change there).

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.
@tfoutrein tfoutrein force-pushed the perf/ooo-contains branch from 1a930a2 to 545d2e5 Compare June 13, 2026 12:31
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.

1 participant