How unsafe are unsafeThawArray# and unsafeFreezeArray#

Johan Tibell johan.tibell at gmail.com
Fri Mar 9 20:51:49 CET 2012


On Fri, Mar 9, 2012 at 11:39 AM, Twan van Laarhoven <twanvl at gmail.com> wrote:
> Quick follow up question:
>
> Are unsafeThawArray# and unsafeFreezeArray# guaranteed to not make a copy?
> I.e. are these two pieces of code equivalent with respect to further usage
> of the return value:
>
>    do mary <- unsafeThawArray# ary
>       writeArray# mary 123# newValue
>       unsafeFreezeArray# mary
>
> and
>
>    do mary <- unsafeThawArray# ary
>       writeArray# mary 123# newValue
>       return ary
>
> (where do notation is actually done manually.)

I believe you

 * don't have to use the return value from unsafeFreezeArray#, you can
use the (now mutated) Array# initially passed to unsafeThawArray#, but
 * you must call unsafeFreezeArray# to make sure the header of the
heap object is updated correctly.

At least this is what I did in unordered-containers here:
https://github.com/tibbe/unordered-containers/commit/428885cb1a62123921c22bc21da9ff385e797686

Cheers,
Johan



More information about the Glasgow-haskell-users mailing list