python / python/cpython

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

Open
#150,815 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

performance stdlib type-feature
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

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.

Linked PRs
  • gh-150822

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the copy.deepcopy() entry point and inspect linked PR gh-150822, which indicates that work is already underway. Verify the reported optimization preserves shared references, recursive structures, and int/tuple subclass handling, while matching the stated benchmark improvement.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
performance
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.