1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
|
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Data.HashTable.ST.Swiss
( Table (..)
, new
, newSized
, insert'
, insert
, lookup'
, lookup
, delete'
, delete
, foldM
, mapM_
, analyze
, getSize
, mutateST
, mutate
) where
import Control.Monad (forM_, void, when)
import qualified Control.Monad as M
import Control.Monad.ST (RealWorld, ST)
import Data.Bits
import Data.Hashable
import Data.Primitive
import Data.Primitive.Array as A
import Data.Primitive.Ptr as PP
import Data.STRef
import Data.Word
import Foreign.C.Types
import GHC.Generics (Generic)
import GHC.IO (ioToST)
import Prelude hiding (lookup, mapM_)
-- todo: try foreign import prim
foreign import ccall unsafe "_elm_cmp_vec" cElmCmpVec :: Word8 -> Ptr Word8 -> Word32
foreign import ccall unsafe "_load_movemask" cLoadMovemask :: Ptr Word8 -> Word32
foreign import ccall unsafe "ffs" cFfs :: Word32 -> CInt
foreign import ccall unsafe "_elm_add_movemask" cElmAddMovemask :: Word8 -> Ptr Word8 -> Word32
newtype Table s k v = T (STRef s (Table_ s k v))
deriving (Generic)
-- todo: distibute STRef
data Table_ s k v = Table
{ elems :: {-# UNPACK #-} !(MutableArray s (k, v))
, ctrl :: {-# UNPACK #-} !(MutablePrimArray s Word8)
, size :: {-# UNPACK #-} !Int
, mask :: {-# UNPACK #-} !Int
, used :: {-# UNPACK #-} !(STRef s Int)
} deriving (Generic)
new :: ST s (Table s k v)
new = newSized 16
empty :: Word8
empty = 128
deleted :: Word8
deleted = 254
newSized :: Int -> ST s (Table s k v)
newSized n = do
when (n .&. (n - 1) /= 0) $ error "size should be power of 2"
es <- A.newArray n (error "impossible")
c <- newPinnedPrimArray (n + 32)
setPrimArray c 0 n empty
setPrimArray c n 32 deleted
u <- newSTRef 0
let t = Table es c (fromIntegral n) (fromIntegral n - 1) u
newRef t
newRef :: Table_ s k v -> ST s (Table s k v)
newRef = fmap T . newSTRef
{-# INLINE newRef #-}
readRef :: Table s k v -> ST s (Table_ s k v)
readRef (T ref) = readSTRef ref
{-# INLINE readRef #-}
writeRef :: Table s k v -> Table_ s k v -> ST s ()
writeRef (T ref) = writeSTRef ref
{-# INLINE writeRef #-}
insert' :: (Hashable k, Eq k) => (k -> Int) -> Table s k v -> k -> v -> ST s ()
insert' h m k v = do
mutateST' h m k (const $ pure (Just v, ()))
{-# INLINE insert' #-}
rawInsert :: (Hashable k, Eq k) => Int -> Table s k v -> k -> v -> ST s ()
rawInsert !h1' ref !k !v = do
m@Table{..} <- readRef ref
iterateCtrlIdx (f (mutablePrimArrayContents ctrl) size elems ctrl) size (mask .&. h1')
modifySTRef' used (+ 1)
checkOverflow m >>= \x -> when x $ grow ref
where
f !ptr !size !elems !ctrl !idx = do
let !pc = PP.advancePtr ptr idx
let !mask = cLoadMovemask pc
let !offset = cFfs mask - 1
let !idx' = idx + fromIntegral offset
if offset >= 0 && idx' < size then do
writeArray elems idx' (k, v)
writePrimArray ctrl idx' (h2 h1')
pure $ Just ()
else pure Nothing
{-# INLINE f #-}
{-# INLINE rawInsert #-}
lookup' :: forall k s a. (Hashable k, Eq k) => (k -> Int) -> Table s k a -> k -> ST s (Maybe a)
lookup' h !r !k = fmap fst <$> lookup'' (h1 h k) r k
{-# INLINE lookup' #-}
lookup'' :: forall k s a. (Hashable k, Eq k) => Int -> Table s k a -> k -> ST s (Maybe (a, Int))
lookup'' !h1' ref !k = do
Table{..} <- readRef ref
let !idx = mask .&. h1'
iterateCtrlIdx (lookCtrlAt (mutablePrimArrayContents ctrl) elems) size idx
where
!h2' = h2 h1'
lookBitmask es idx bidx = do
let idx' = idx + bidx - 1
(!k', v) <- readArray es idx'
pure $ if k == k' -- todo: opt(hashも保持?)
then Just (v, idx')
else Nothing
{-# INLINE lookBitmask #-}
lookCtrlAt !ptr !es !idx = do
let pc = PP.advancePtr ptr idx
let !mask = cElmCmpVec h2' pc
x <- iterateBitmaskSet (lookBitmask es idx) mask
case x of
Nothing
| cElmCmpVec 128 pc /= 0 -> pure (Just Nothing) -- found empty -- unlikely
| otherwise -> pure Nothing
_ -> pure (Just x)
{-# INLINE lookCtrlAt #-}
{-# INLINE lookup'' #-}
iterateCtrlIdx :: Monad m => (Int -> m (Maybe b)) -> Int -> Int -> m b
iterateCtrlIdx f !s !offset = go offset
where
go !idx = do
f idx >>= \case
Nothing ->
let !next = idx + 32
in if next > s then go 0 else go next
Just x -> pure x
{-# INLINE iterateCtrlIdx #-}
listBitmaskSet :: Word32 -> [CInt]
listBitmaskSet = map cFfs . iterate (\x -> x .&. (x - 1))
{-# INLINE listBitmaskSet #-}
iterateBitmaskSet :: Monad m => (Int -> m (Maybe a)) -> Word32 -> m (Maybe a)
iterateBitmaskSet !f !mask = do
let bitidxs = listBitmaskSet mask
go bitidxs
where
go (bidx:bidxs)
| bidx /= 0 = do
f (fromIntegral bidx) >>= \case
Nothing -> go bidxs
x -> pure x
| otherwise = pure Nothing
go _ = pure Nothing
{-# INLINE go #-}
{-# INLINE iterateBitmaskSet #-}
h1 :: Hashable k => (k -> Int) -> k -> Int
h1 = ($)
{-# INLINE h1 #-}
h2 :: Int -> Word8
h2 x = fromIntegral $ x .&. 127
{-# INLINE h2 #-}
-- delete :: (PrimMonad m, Hashable k, Eq k) => k -> Table (PrimState m) k v -> m ()
delete :: (Hashable k, Eq k) => Table s k v -> k -> ST s ()
delete = delete' hash
-- delete' :: (PrimMonad m, Hashable k, Eq k) => (k -> Int) -> k -> Table (PrimState m) k v -> m ()
delete' :: (Hashable k, Eq k) => (k -> Int) -> Table s k v -> k -> ST s ()
delete' hash' ref k = do
m <- readRef ref
let s = size m
let h1' = h1 hash' k
h2' = h2 h1'
let idx = (s - 1) .&. h1'
let es = elems m
let ct = ctrl m
let f'' offset = do
let pc = PP.advancePtr (mutablePrimArrayContents ct) offset
let mask = cElmCmpVec h2' pc
iterateBitmaskSet (readBM es offset) mask >>= \case
Nothing
| cElmCmpVec 128 pc /= 0 -> pure (Just Nothing)
| otherwise -> pure Nothing
x -> pure (Just x)
idx' <- iterateCtrlIdx f'' s idx
forM_ idx' $ deleteIdx m
where
readBM es offset bidx = do
let idx' = offset + bidx - 1
(k', _) <- readArray es idx'
pure $ if k == k'
then Just idx'
else Nothing
deleteIdx :: Table_ s k v
-> Int
-> ST s ()
deleteIdx m idx = do
writePrimArray (ctrl m) idx 254
modifySTRef (used m) (\x -> x - 1)
-- insert :: (PrimMonad m, Hashable k) => k -> v -> Table (PrimState m) k v -> m ()
insert :: (Hashable k, Eq k) => Table s k v -> k -> v -> ST s ()
insert = insert' hash
-- lookup :: (PrimMonad m, Hashable k, Show k, Eq k)
-- => k -> Table (PrimState m) k v -> m (Maybe v)
lookup :: (Hashable k, Eq k) => Table s k a -> k -> ST s (Maybe a)
lookup = lookup' hash
{-# INLINE lookup #-}
checkOverflow ::
(Hashable k) => Table_ s k v -> ST s Bool
checkOverflow t = do
u <- readSTRef (used t)
pure $ fromIntegral u / fromIntegral (size t) > maxLoad
{-# INLINE checkOverflow #-}
maxLoad :: Double
maxLoad = 0.8
grow :: (Hashable k, Eq k) => Table s k v -> ST s ()
grow ref = do
t <- readRef ref
let size' = size t * 2
t' <- newSized size'
mapM_ (f t') ref
writeRef ref =<< readRef t'
pure ()
where
f t (k, v) = insert t k v
mapM_ :: ((k, v) -> ST s a) -> Table s k v -> ST s ()
mapM_ f ref = do
t <- readRef ref
let idx = 0
void $ iterateCtrlIdx (h t) (size t) idx
where
g t idx bidx = do
let idx' = idx + bidx - 1
e <- readArray (elems t) idx'
void $ f e
pure Nothing
h t idx = do
let pc = PP.advancePtr (mutablePrimArrayContents (ctrl t)) idx
let mask = cElmAddMovemask 128 pc
r <- iterateBitmaskSet (g t idx) mask
if idx + 32 > size t then pure (Just Nothing) else pure r
foldM :: (a -> (k,v) -> ST s a) -> a -> Table s k v -> ST s a
foldM f seed0 ref = do
t <- readRef ref
foldCtrlM g seed0 t 0
where
g acc t idx (bidx:xs)
| bidx == 0 = pure acc
| otherwise = do
let idx' = idx + bidx - 1
e <- readArray (elems t) idx'
acc' <- f acc e
g acc' t idx xs
g _ _ _ _ = error "impossible"
foldCtrlM :: (a -> Table_ s k v -> Int -> [Int] -> ST s a) -> a -> Table_ s k v -> Int -> ST s a
foldCtrlM g acc t idx = do
let pc = PP.advancePtr (mutablePrimArrayContents (ctrl t)) idx
let mask = cElmAddMovemask 128 pc
acc' <- g acc t idx (map fromIntegral $ listBitmaskSet mask)
if idx + 32 > size t then pure acc' else foldCtrlM g acc' t (idx + 32)
_foldM :: (a -> (k,v) -> Int -> ST s a) -> a -> Table s k v -> ST s a
_foldM f seed0 ref = do
t <- readRef ref
foldCtrlM g seed0 t 0
where
g acc t idx (bidx:xs)
| bidx == 0 = pure acc
| otherwise = do
let idx' = idx + bidx - 1
e <- readArray (elems t) idx'
acc' <- f acc e idx'
g acc' t idx xs
g _ _ _ _ = error "impossible"
analyze :: (Hashable k, Show k) => (Table RealWorld k v -> ST RealWorld ())
analyze ref = do
t <- readRef ref
cs <- _foldM (f t) [] ref
u <- readSTRef (used t)
ioToST $ do
putStrLn $ "size: " <> show (size t)
putStrLn $ "used: " <> show u
putStrLn $ " " <> show (fromIntegral u / fromIntegral (size t) :: Double)
print $ "max diff: " <> show (maximum (fmap snd cs))
print $ "sum diff: " <> show (sum (fmap snd cs))
M.mapM_ print cs
where
f t acc (k, _) idx = do
let nidx = (size t - 1) .&. hash k
let d = if idx - nidx < 0 then idx - nidx + size t else idx - nidx
pure $ ((k, nidx, idx), d):acc
mutateST' :: (Eq k, Hashable k)
=> (k -> Int) -> Table s k v -> k -> (Maybe v -> ST s (Maybe v, a)) -> ST s a
mutateST' h ref k f = do
t <- readRef ref
let !h1' = h1 h k
lookup'' h1' ref k >>= \case
Just (v, idx) ->
f (Just v) >>= \case
(Just v', a) -> -- update
writeArray (elems t) idx (k, v') >> pure a
(Nothing, a) -> --delete
deleteIdx t idx >> pure a
Nothing ->
f Nothing >>= \case
(Just v', a) -> -- insert
rawInsert h1' ref k v' >> pure a
(Nothing, a) -> pure a
{-# INLINE mutateST' #-}
mutateST :: (Eq k, Hashable k)
=> Table s k v -> k -> (Maybe v -> ST s (Maybe v, a)) -> ST s a
mutateST = mutateST' hash
{-# INLINE mutateST #-}
mutate :: (Eq k, Hashable k) =>
Table s k v -> k -> (Maybe v -> (Maybe v, a)) -> ST s a
mutate ref !k !f = mutateST ref k (pure . f)
{-# INLINE mutate #-}
{-
試したい
右端で競合が発生した際に0に戻るのではなく、
予備領域を使い、予備領域が埋まったら拡張する。
-> unlikelyすぎて効果うすそう
-- make Data.HashTable.Class instance?
-> 両方に依存したインターフェス揃えるようlibrary作れば良い
-}
getSize :: Table s k v -> ST s Int
getSize = fmap size . readRef
|