Skip to content

Speed up copy.deepcopy() of containers holding atomic elements #150815

@gaborbernat

Description

@gaborbernat

copy.deepcopy() copies a structure by sending every element back through deepcopy(). For elements that need no copying at all — strings, ints, None, booleans, floats and the other immutable atomic types — that round trip still costs a function call each, even though the value handed back is the same object.

Real data is dominated by these atomic leaves. A parsed JSON document, a settings dict cloned before mutation, a record copied inside a framework: the keys are strings and most values are strings and numbers. Copying such a structure spends most of its time calling deepcopy() only to receive the same object straight back.

Deep-copying 105 JSON documents drawn from the top-1000 PyPI projects takes 1.20 ms today. Folding the atomic-type check into the dict, list and tuple copiers brings that to 970 µs, 23% faster, with identical handling of shared references, recursive structures and int/tuple subclasses.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels
    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