-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf(runtime): add a padded variant of atomic counter
- Loading branch information
1 parent
46ef03b
commit 8053e20
Showing
4 changed files
with
76 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/runtime-prototype/src/StuntDouble/AtomicCounterPadded.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
{-# LANGUAGE MagicHash #-} | ||
{-# LANGUAGE UnboxedTuples #-} | ||
|
||
-- Inspired by: | ||
-- https://github.com/jberryman/unagi-chan/blob/master/src/Data/Atomics/Counter/Fat.hs | ||
-- and: | ||
-- https://hackage.haskell.org/package/atomic-primops-0.8.4/docs/src/Data.Atomics.Counter.html | ||
|
||
module StuntDouble.AtomicCounterPadded | ||
( AtomicCounter() | ||
, newCounter | ||
, incrCounter | ||
, readCounter | ||
) where | ||
|
||
import Control.Monad.Primitive (RealWorld) | ||
import Data.Primitive.MachDeps (sIZEOF_INT) | ||
import GHC.Exts | ||
import GHC.Types | ||
|
||
------------------------------------------------------------------------ | ||
|
||
data AtomicCounter = AtomicCounter (MutableByteArray# RealWorld) | ||
|
||
sIZEOF_CACHELINE :: Int | ||
sIZEOF_CACHELINE = 64 | ||
{-# INLINE sIZEOF_CACHELINE #-} | ||
|
||
-- | Create a new atomic counter padded with 64-bytes (an x86 cache line) on | ||
-- either side to try to avoid false sharing. | ||
newCounter :: Int -> IO AtomicCounter | ||
newCounter (I# n) = IO $ \realWorld -> | ||
let I# sz = sIZEOF_CACHELINE in | ||
case newAlignedPinnedByteArray# sz sz realWorld of | ||
(# realWorld', arr #) -> case writeIntArray# arr 0# n realWorld' of | ||
realWorld'' -> (# realWorld'', AtomicCounter arr #) | ||
{-# INLINE newCounter #-} | ||
|
||
incrCounter :: Int -> AtomicCounter -> IO Int | ||
incrCounter (I# incr) (AtomicCounter arr) = | ||
IO (\realWorld -> case fetchAddIntArray# arr 0# incr realWorld of | ||
(# realWorld', i #) -> (# realWorld', I# i #)) | ||
{-# INLINE incrCounter #-} | ||
|
||
readCounter :: AtomicCounter -> IO Int | ||
readCounter (AtomicCounter arr) = | ||
IO (\realWorld -> case readIntArray# arr 0# realWorld of | ||
(# realWorld', i #) -> (# realWorld', I# i #)) | ||
{-# INLINE readCounter #-} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters