Skip to content

Setting __dict__ breaks PEP 412 #1758

@psippola

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

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions