
#13656: Consider implementing atomicModifyMutVar# in Haskell -------------------------------------+------------------------------------- Reporter: dfeuer | Owner: dfeuer Type: task | Status: closed Priority: low | Milestone: 8.4.1 Component: Runtime System | Version: 8.2.1-rc2 Resolution: invalid | Keywords: Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Changes (by dfeuer): * status: new => closed * resolution: => invalid Comment: fryguybob first explained that the implementation above will allocate like mad. I countered with {{{#!hs atomicModifyMutVar# :: s ~ RealWorld => MutVar# s a -> (a -> (a,b)) -> State# s -> (# State# s, b #) atomicModifyMutVar# mv f s = case readMutVar# mv s of { (# s', old #) -> case newMutVar# old s' of { (# s'', tempy #) -> let thunk = case runRW# $ \ss -> readMutVar# tempy ss of (# _, now #) -> f now in go# tempy mv (fst thunk) (snd thunk) s''}} go# :: s ~ RealWorld => MutVar# s a -> MutVar# s a -> a -> b -> State# s -> (# State# s, b #) go# tempy mv thunka thunkb s = case readMutVar# mv s of { (# s', old #) -> case writeMutVar# tempy old s of { s'' -> case casMutVar# mv old thunka s'' of { (# s''', unchanged, new #) -> case unchanged of 0# -> (# s''', thunkb #) _ -> go# tempy mv thunka thunkb s''' }}} }}} fryguybob then explained that the cmm implementation is able to avoid write barrier inefficiencies that we can't in Haskell. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/13656#comment:2 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler