improvement for copy.deepcopy : no memo for immutable types

Stefan Behnel stefan_ml at behnel.de
Fri Jul 16 09:02:50 EDT 2010


Inquisitive Scientist, 16.07.2010 14:45:
> I am having problems with running copy.deepcopy on very large data
> structures containing lots of numeric data:
>
>    1. copy.deepcopy can be very slow
>    2. copy.deepcopy can cause memory errors even when I have plenty of
> memory
>
> I think the problem is that the current implementation keeps a memo
> for everything it copies even immutable types. In addition to being
> slow, this makes the memo dict grow very large when there is lots of
> simple numeric data to be copied. For long running programs, large
> memo dicts seem to cause memory fragmentation and result in memory
> errors.
>
> It seems like this could be easily fixed by adding the following lines
> at the very start of the deepcopy function:
>
>      if isinstance(x, (type(None), int, long, float, bool, str)):
>          return x
>
> This seems perfectly safe, should speed things up, keep the memo dict
> smaller, and be easy to add.

and - have you tried it?

Stefan




More information about the Python-list mailing list