Should I check for changes before writing IORef?

Here, code that reads IORef and is based on some conditions and calculations creates a new value. Now it writes the new value to IORef. But there is a chance that he has not changed at all. The new value may be identical to the old.

What are some considerations about whether to check if the value is different before writing IORef, or just writing IORef independently?

Does writeIORef check to see if the value has changed before tuning?

By checking first, could you avoid recording and maintain performance a bit?

+3
source share
1 answer

Does writeIORef check to see if the value has changed before tuning?

No. writeIORefwraps writeSTRef, which is defined as

-- |Write a new value into an 'STRef'
writeSTRef :: STRef s a -> a -> ST s ()
writeSTRef (STRef var#) val = ST $ \s1# ->
    case writeMutVar# var# val s1#      of { s2# ->
    (# s2#, () #) }

By checking first, could you avoid recording and maintain performance a bit?

What are some considerations about whether to check if the value is different before writing IORef, or just writing IORef independently?

. ? / ? ? ? ?

, , : , , GHC . , .

:

, , 97% : -

, , , , ,

. , , GHC- (Core), d, ( ) .

+8

All Articles