realloc for I/MArrays

Simon Marlow
Mon, 22 Apr 2002 10:27:29 +0100

> Any chance IArrays (or MArrays) will some day support something like:
> resize :: (Ix ix, IArray a e) =3D>
>           a ix e -> (ix,ix) -> a ix e
> that is faster than allocating a new array and then copying
> value-by-value?  perhaps make the result a Maybe (a ix e) in=20
> the same way
> that realloc can return null if it can't get you more memory=20
> at the end=20

I suspect that this would be unlikely to be much use, due to GHC's
discontiguous block scheme for managing memory.  The best you could hope
for in most cases would be to fill up the slop in the block of memory at
the end  of the array.

> or even just a simple memcpy like function so you could allocate a new
> array with the same index and element type and then copy=20
> directly from one
> to the other, like:
> arrcopy :: (Ix ix, IArray a e) =3D>
>            (ix -> ix) -> a ix e -> a ix e -> a ix e
> which can possibly fail if the indices for the destination=20
> don't line up
> with the source properly?

Doing it by hand *should* result in efficient code, eg.

	let copyElt i =3D do x <- readArray a1 i witeArray a2 i x
	sequence_ [ copyElt i | i <- indices a1 ]
should result in a memcpy (or at least a tight loop) for UArrays with
Int indices.  (it might not though - I recall there are some problems
with optimisation of array ops at the moment).