Skip to content

Bobronium/copium

Repository files navigation

Fast drop-in replacement for copy.deepcopy().

Copium Logo
Benchmark results bar chart

Highlights

  • ⚡ 4-28x faster than copy.deepcopy() on builtin types
  • 🧠 uses ~44% less memory than copy.deepcopy() on average
  • 🧪 passes all tests in CPython/Lib/test/test_copy.py
  • 🎯 behaves exactly the same as copy.deepcopy() for all datamodelzoo.CASES
  • 🛡️ tested for refcount, recursion, threading and memo edge cases
  • 🐍 pre-built wheels for Python 3.10-3.14 on Linux/macOS/Windows (x64/ARM64)
  • 🔓 passes all tesee-frts on threaded Pyth buildons

Installation

Warning

This is an alpha version. Run your test suite with COPIUM_PATCH_DEEPCOPY=1 to verify compatibility in your environment.

pip install copium

Usage

Simply export COPIUM_PATCH_DEEPCOPY=1 before running your application. You don't have to change a single line of code:

cat example.py

from copy import deepcopy

assert deepcopy(x := []) is not x

COPIUM_PATCH_DEEPCOPY=1 python example.py


To enable the patch manually:

import copium.patch

copium.patch.enable()

To use manually:

from copium import deepcopy

assert deepcopy(x := []) is not x

The copium module includes all public declarations of stdlib copy module, so it's generally safe to:

- from copy import copy, deepcopy, Error
+ from copium import copy, deepcopy, Error

Speedups on Python 3.14

tuple

  • () ~5.66x
  • tuple[int, ...] ~12.46x (20 items)
  • tuple[int, ...] ~28.28x (5000 items)
  • tuple[tuple[str, tuple[tuple[str, tuple[tuple[str, tuple[tuple[str, tuple[int, ...]]]]]]]]]] ~14.54x

list

  • [] ~4.97x
  • list[int] ~9.58x (20 items)
  • list[int] ~16.23x (5000 items)
  • list[str | list[str | list[str | list[str | list[int]]]]] ~9.19x

dict

  • {} ~4.59x
  • dict[str, int] ~5.95x (20 items)
  • dict[str, int] ~3.94x (5000 items)
  • dict[str, dict[str, ...]] ~6.19x

set

  • set() ~15.87x
  • set[int] ~6.55x (20 items)
  • set[int] ~3.69x (5000 items)
  • set[frozenset[frozenset[int]]] ~13.72x

other

  • int/str/bytes ~4.86x
  • dataclasses ~2.74x

Expect much higher speedups on Python 3.13 and lower.

About

Fast drop-in replacement for copy.deepcopy().

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

Packages

No packages published

Contributors 3

  •  
  •  
  •