I have a class that returns large NumPy arrays. These arrays are cached within the class. I would like the returned arrays to be copy-on-write arrays. If the caller ends up just reading from the array, no copy is ever made. This will case no extra memory will be used. However, the array is "modifiable", but does not modify the internal cached arrays.
My solution at the moment is to make any cached arrays readonly (a.flags.writeable = False)
. This means that if the caller of the function may have to make their own copy of the array if they want to modify it. Of course, if the source was not from cache and the array was already writable, then they would duplicate the data unnecessarily.
So, optimally I would love something like a.view(flag=copy_on_write)
. There seems to be a flag for the reverse of this UPDATEIFCOPY
which causes a copy to update the original once deallocated.
Thanks!