Skip to content

gh-155962: Specialize LOAD_ATTR after replacing an instance's __dict__ - #155963

Open
koxudaxi wants to merge 3 commits into
python:mainfrom
koxudaxi:gh-155962-load-attr-replaced-dict
Open

gh-155962: Specialize LOAD_ATTR after replacing an instance's __dict__#155963
koxudaxi wants to merge 3 commits into
python:mainfrom
koxudaxi:gh-155962-load-attr-replaced-dict

Conversation

@koxudaxi

@koxudaxi koxudaxi commented Aug 17, 2026

Copy link
Copy Markdown
Contributor

Replacing an instance's __dict__ prevents LOAD_ATTR from specializing on Python 3.14 and main. The same case specializes to LOAD_ATTR_WITH_HINT on Python 3.13.

instance_has_key() used shared keys whenever the type supported inline values, even after replacing an instance's __dict__ had invalidated its inline values. Check the per-instance state and use the managed dictionary instead. General-key dictionaries remain unsupported by the hint opcodes.

This affects Pydantic v2 because pydantic-core installs validated fields by replacing the model's __dict__.

Benchmark

Fresh-process datamodel-code-generator CLI generation using a 500-definition JSON Schema, Pydantic v2 output, the built-in formatter, and a temporary output file:

Median
main 354.5 ms
this PR 327.7 ms

This is a 26.8 ms (7.6%) reduction. The results are medians from 11 alternating-order samples on macOS arm64.

Tests

Tests cover replacement dictionaries with stale shared keys, unsupported general-key dictionaries, and concurrent dictionary replacement.

Fixes #155962

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.

LOAD_ATTR does not specialize after replacing an instance's __dict__

1 participant