sameMutableArray equivalent for Array
Sometime it is useful to compare reference equality for immutable arrays, for example when comparing vectors which are slices of arrays, we may want to know if two vectors are slices of the same array. But currently we only have
A workaround may look like this:
sameArray :: Array a -> Array a -> Bool sameArray arr1 arr2 = runST (do marr1 <- unsafeThawArray arr1 marr2 <- unsafeThawArray arr2 return (sameMutableArray marr1 marr2) )
But the problem is for boxed arrays, this code will push an unchanged array to mutable list if it's already on older generations. Next GC will have to scan it(at least will scan its card table).
I can see two solutions here, first one is to add
sameXXXArray for all array types. The second one is somehow more complex:
Currently we simply rewrite array's header to
MUT_ARR_PTRS_FROZEN0/SMALL_MUT_ARR_PTRS_FROZEN0 when we do
unsafeFreeze. But for mutable array with no write happend, e.g. the ones with
MUT_ARR_PTRS_CLEAN/SMALL_MUT_ARR_PTRS_CLEAN header. we can safely rewrite the header to
MUT_ARR_PTRS_FROZEN/SMALL_MUT_ARR_PTRS_FROZEN, so that we can keep it from next GC.
Then to fix previous code, we can add a freeze before returning.
Of course we can do these two solutions all together ; )