[Haskell-cafe] more sharing in generated code

Peter Divianszky divipp at gmail.com
Mon Nov 5 23:34:15 CET 2012

Hi Joachim,

 > > ...
 > I think one problem with this approach is that now, until "x r'" is
 > evaluated, you keep both r and r' alive. If r was _not_ retained by
 > anything else, then you are now preventing the GC from freeing it and
 > hence increasing the memory footprint.

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 }


     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


More information about the Haskell-Cafe mailing list