I've been running into performance bottlenecks in the wild where `copy.deepcopy()` was the bottleneck. After digging into it, I discovered that deepcopy can actually be slower than even serializing and deserializing with pickle or json in many cases!
I wrote up my findings on why this happens and some practical alternatives that can give you significant performance improvements.
*TL;DR:* deepcopy's recursive approach and safety checks create memory overhead that often isn't worth it. The post covers when to use alternatives like shallow copy + manual handling, pickle round-trips, or restructuring your code to avoid copying altogether.
Has anyone else run into this? Curious to hear about other performance gotchas you've discovered in commonly-used Python functions.
misrasaurabh1•4h ago
I wrote up my findings on why this happens and some practical alternatives that can give you significant performance improvements.
*TL;DR:* deepcopy's recursive approach and safety checks create memory overhead that often isn't worth it. The post covers when to use alternatives like shallow copy + manual handling, pickle round-trips, or restructuring your code to avoid copying altogether.
Has anyone else run into this? Curious to hear about other performance gotchas you've discovered in commonly-used Python functions.