Record of STRefs better than STRef to a Record?

Jorge Adriano jadrian@mat.uc.pt
Wed, 13 Nov 2002 23:29:18 +0000


> > If I use an STRef to a record, will a new record be created
> > each time I want
> > to update a single field? Or can I expect GHC to optimize it
> > 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.
One of them has 4, the other 3, but they might grow bigger...


> Here's another trick if you use this route:
<added some parenthesis>
>  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.

Nice, thanks :)


One more question.=20
I'm passing that 'record' around as an implicit value. The record as STRe=
fs=20
that I use to collect info, but it also has some pure fields with 'read-o=
nly'=20
info. Something like,

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

In some functions I might need only some pure fields, and none of the STR=
efs,=20
but since I pass something of type 'E s' around, everything ends up beein=
g=20
monadic. Is using two records (on with STRefs and one with pure fields) t=
he=20
only/best way to avoid this?=20
I would like to use two records, doesn't seem natural, but I also don't l=
ike=20
to end up using monadic functions when they are, in fact, pure...

Thanks,
J.A.