turn off let floating

Bernard James POPE bjpop at cs.mu.OZ.AU
Thu Apr 15 14:52:38 EDT 2004

On Tue, Apr 13, 2004 at 02:03:21PM +0100, Simon Marlow wrote:
> > On Fri, Apr 09, 2004 at 03:27:01PM +0200, David Sabel wrote:
> > 
> > > you can turn off let-floating by compiling without optimizations,
> > > i.e. without using a -O flag or using -O0 explicitly. 
> > > The disadvantage is that most of all other optimizations 
> > > are turned off too.
> > 
> > That is exactly what I'm doing at the moment. The module that has the
> > nasty impure bits in it is not compiled with optimisations. 
> > I will improve this when GHC regains the non-let floating flag.
> If you need -ffull-laziness to force a certain behaviour when using
> unsafePerformIO, I say that what you're doing is at the very least
> unsupported ;-)  However, there are occasoinally good uses for this:
> HOOD is one; I imagine your case is similar?

Hi Simon,

What I am trying to do is implement a global (mutable) integer counter.
I'm using a combination of IORefs and unsafePerformIO.

The reason I want to do this is that I'm experimenting with a new 
design of buddha. Each function call in a program being debugged gets a 
new number by reading and incrementing the global counter. 

Thus the counter is read and incremented from within pure code 
(no IO monad).

Of course this is not what you are supposed to do in a pure language :)
Nonetheless, a global mutable counter is exactly what I want for this
job - I don't want to thread anything through the code.

So I have code like:

   {-# NOINLINE count #-}
   count :: IORef Int
   count = unsafePerformIO $ newIORef 0

   {-# NOINLINE getCount #-}
   getCount :: (Int -> a) -> a
   getCount f
      = let nextCount
             = (unsafePerformIO $
                  do oldCount <- readIORef count
                     let newCount = oldCount + 1
                     writeIORef count newCount
                     return oldCount)
        in seq nextCount (f nextCount)

It seems to work okay.

However, if you have any suggestions about how to make a FAST global counter
I would be very glad to hear it. From profiling it seems like this code
is a little expensive (also it is called quite frequently).


More information about the Glasgow-haskell-users mailing list