Skip to content

Commit 0c341bb

Browse files
committed
FIXUP: document why we use prefetchMutableByteArray0#
and what 0 means.
1 parent b0fc408 commit 0c341bb

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

bloomfilter/src/Data/BloomFilter/Blocked/BitArray.hs

+10-2
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ prefetchIndex (BitArray (PrimArray ba#)) (BlockIx blockIx) =
8383

8484
assert (i >= 0 && i < sizeofByteArray (ByteArray ba#) - 63) $
8585

86+
-- In prefetchByteArray0, the 0 refers to a "non temporal" load, which is
87+
-- a hint that the value will be used soon, and then not used again (soon).
88+
-- So the caches can evict the value as soon as they like.
8689
ST (\s -> case prefetchByteArray0# ba# i# s of
8790
s' -> (# s', () #))
8891

@@ -133,8 +136,10 @@ unsafeSet (MBitArray arr) blockIx blockBitIx = do
133136
{-# INLINE prefetchSet #-}
134137
prefetchSet :: MBitArray s -> BlockIx -> ST s ()
135138
prefetchSet (MBitArray (MutablePrimArray mba#)) (BlockIx blockIx) = do
136-
-- For setting, we will do several writes to the same cache line, so
137-
-- read it into all 3 levels of cache.
139+
-- For setting, we will do several writes to the same cache line, but all
140+
-- immediately after each other, after which we will not need the value in
141+
-- the cache again (for a long time). So as with prefetchIndex we want to
142+
-- disturbe the caches the least, and so we use prefetchMutableByteArray0.
138143
let !(I# i#) = fromIntegral blockIx `shiftL` 6
139144
-- blockIx * 64 to go from block index to the byte offset of the beginning
140145
-- of the block. This offset is in bytes, not words.
@@ -144,6 +149,9 @@ prefetchSet (MBitArray (MutablePrimArray mba#)) (BlockIx blockIx) = do
144149
assert (let i = I# i# in i >= 0 && i < sz-63) $ return ()
145150
#endif
146151

152+
-- In prefetchMutableByteArray0, the 0 refers to a "non temporal" load,
153+
-- which is a hint that the value will be used soon, and then not used
154+
-- again (soon). So the caches can evict the value as soon as they like.
147155
ST (\s -> case prefetchMutableByteArray0# mba# i# s of
148156
s' -> (# s', () #))
149157

0 commit comments

Comments
 (0)