[SciPy-dev] copy on write?

Nathan Bell wnbell@gmail....
Wed Jul 25 19:31:29 CDT 2007

Is there any support for copy on write behavior in numpy?  I haven't
seen anything to suggest that it does or doesn't exist.

The particular application I have in mind is as follows:

Sparse matrices, such as csr_matrix, are represented by three dense 1D
arrays.  Two of these arrays encode the structure of the matrix, while
one records the nonzero values.  Many operations like __abs__ and
__neg__ require only a change to the data array and leave the
structure arrays unchanged.  Therefore, when performing  B = abs(A),
the two sparse matrices could share the same structure arrays.  This
sort of optimization is nearly always desirable, especially when B is
short-lived (e.g. abs(A).sum(0)).

Unfortunately, the user may occasionally decide to change A or B's
sparsity structure directly (e.g. an inplace sort on the column
indices).  Hence, in general one must copy for complete safety.

If the CSR structure data was COW, then one could apply such
optimizations frequently without fear of atypical usage.

Short of COW, can one easily get the refcount of an ndarray (in Python)?

Nathan Bell wnbell@gmail.com

More information about the Scipy-dev mailing list