unsafePerformIO and NOINLINE Pragma

David Sabel sabel at ki.informatik.uni-frankfurt.de
Tue Sep 13 10:13:05 EDT 2005


> Hi!
> I want to analyse the laziness of a data structure. To check how many nodes 
> are constructed I use a global counter.
> counter :: IORef Int
> counter = unsafePerformIO (newIORef 0)
> This counter is increased every time the constructor is called by redefining 
> the constructor OBDD as follows.
> oBDD low var high =
>   seq (unsafePerformIO (modifyIORef counter (+1))) (OBDD low var high)
> This works fine. 
> When I compile with optimisations the counter is always set to one no matter 
> how many nodes are constructed. I thought this would be caused by inlining. 
> Therefore I have added two NOINLINE pragmata.
> {-# NOINLINE counter #-}
> {-# NOINLINE oBDD #-}
> Although the counter doesn't work. Is there another optimisation that can 
> cause harm? Is there something wrong with the pragmata?

Two comments:
1. There are other optimisations than inlining that can break sharing, e.g. 
   common subexpression elimination, full-laziness-transformation.

2. I tested the following:

  {-# NOLINE counter #-}
  {-# INLINE oBDD #-}

  then the counter "seems" to work correct.  In my opinion 
  oBDD is an abstraction and can always be inlined.


More information about the Glasgow-haskell-users mailing list