[GHC] #8345: A more efficient atomicModifyIORef'

GHC ghc-devs at haskell.org
Mon Sep 23 14:55:51 CEST 2013


#8345: A more efficient atomicModifyIORef'
------------------------------------+-------------------------------------
       Reporter:  parcs             |             Owner:
           Type:  task              |            Status:  new
       Priority:  normal            |         Milestone:
      Component:  libraries/base    |           Version:  7.6.3
       Keywords:                    |  Operating System:  Unknown/Multiple
   Architecture:  Unknown/Multiple  |   Type of failure:  None/Unknown
     Difficulty:  Unknown           |         Test Case:
     Blocked By:                    |          Blocking:
Related Tickets:                    |
------------------------------------+-------------------------------------
 `atomicModifyIORef'` is currently defined as:

 {{{
 #!haskell
 atomicModifyIORef' ref f = do
     b <- atomicModifyIORef ref
             (\x -> let (a, b) = f x
                     in (a, a `seq` b))
     b `seq` return b
 }}}

 This doesn't seem to be the best we can do, though.

 The following implementation exhibits a speedup of 1.7x (vanilla RTS) /
 1.4x (threaded RTS) over the current implementation:

 {{{
 #!haskell
 atomicModifyIORef' ref f = do
     b <- atomicModifyIORef ref $ \a ->
             case f a of
                 v@(a',_) -> a' `seq` v
     b `seq` return b
 }}}

 The differences between this version and the current version are:

 1. the lambda is now strict in `f a`, and
 2. a new result tuple is no longer being allocated.

 Is there a good reason why `atomicModifyIORef'` is not already defined
 this way?

-- 
Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/8345>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler



More information about the ghc-tickets mailing list