[Haskell-cafe] more sharing in generated code

Joachim Breitner mail at joachim-breitner.de
Tue Nov 6 09:25:23 CET 2012


Hi,

Am Montag, den 05.11.2012, 23:34 +0100 schrieb Peter Divianszky:
> Good remark, this can be solved with weak pointers, although I don't 
> know how efficient they are.
> 
> ----------------------
> The next version of the proposal
> 
> Replace every record update
> 
>      r' = r { x = y }
> 
> with
> 
>      r' = r { x = update (weak r) r' (x r) y) }
> 
> in generated code if some sharing aware optimization flag is enabled.
> 
> Auxilary functions:
> 
> weak :: a -> Weak a
> -- make a weak pointer which is not followed by GC
> 
> update :: Weak r -> r -> x -> x
> update r_old r_new x_old x_new = unsafePerformIO $ do
>      eval x_new
>      b <- x_old === x_new
>      when b (replace r_new r_old)
>      return x_new
> 
> (===) :: a -> a -> IO Bool
> -- pointer-equality for boxed values, equality for unboxed values
> 
> replace :: a -> Weak a -> IO ()
> -- replace thunks in the heap
> -- do nothing if the weak pointer was gone

better, but still keeping too much stuff in memory: Namely x_old cannot
be freed. For most uses it will be retained by the thunk x_new anyways,
as you intend them to be updates of some kind, but it would probably
still make sense to use a weak pointer for (x r).

You can probably implement this using System.Mem.Weak,
reallyUnsafePtrEquality and unsafePerformIO. Not sure how well it would
perform, given the overhead of managing the Weak pointers.

Greetings,
Joachim

-- 
Joachim "nomeata" Breitner
  mail at joachim-breitner.de  |  nomeata at debian.org  |  GPG: 0x4743206C
  xmpp: nomeata at joachim-breitner.de | http://www.joachim-breitner.de/

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: This is a digitally signed message part
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20121106/c6cd1a74/attachment.pgp>


More information about the Haskell-Cafe mailing list