Skip to content
This repository was archived by the owner on Apr 11, 2026. It is now read-only.
This repository was archived by the owner on Apr 11, 2026. It is now read-only.

Setting __dict__ breaks PEP 412 #1758

Description

@psippola

In pydantic v1, replacing each instantiated object's __dict__ with a dict of validated values used to broke
PEP 412 (use of shared dictionary keys), which probably explains a part of the excessive memory usage of V1 when a lot of objects of same type were created.

This may be a concern still in v2, if I read rows like this correctly:

force_setattr(py, model, intern!(py, DUNDER_DICT), validated_dict)?;

I have no adequate rust skills to test this, but it could be beneficial to study how shared keys affect memory consumption of objects validated by pydantic, and could updating __dict__ instead of replacing result in a smaller memory footprint.

More about shared keys, I hope that this is still relevant:

Internals of sets and dicts

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions