Record of STRefs better than STRef to a Record?

Simon Marlow simonmar@microsoft.com
Wed, 13 Nov 2002 10:08:55 -0000


> If I use an STRef to a record, will a new record be created=20
> each time I want=20
> to update a single field? Or can I expect GHC to optimize it=20
> and have the field of the record updated in place?

You'll get a new record for each update.  This might not be so bad
though, depending on the number of fields in your record.

> Right now I'm using a record of STRefs, like:
> data E s =3D E{
>       refi ::  STRef s Int,
>       refc ::  STRef s Char
>     }
>=20
> but it can get a little messy, since thread s propagates to=20
> every datatype that uses the record type in it's definition.

Here's another trick if you use this route:

 data E s =3D E{
       refi ::  !STRef s Int,
       refc ::  !STRef s Char
     }

and compile with -funbox-strict-fields.  This will eliminate the boxing
of the STRefs.

Cheers,
	Simon