-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathSymTable.hs
394 lines (332 loc) · 12.2 KB
/
SymTable.hs
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
{-# language OverloadedStrings #-}
{-|
A `SymTable` is a custom mutable hash table which is keyed by spans pointing to
a particular `ByteString`. Every `SymTable` is initialized from a `ByteString`.
The components of the `ByteString` are stored losslessly besides the table (but
in a different layout).
It is important that we get rid of unnecessary copying on common table
operations. A table is defined as a nesting of mutable references. Writing the
table size or modifying the underlying array of buckets requires no heap
allocation.
-}
module SymTable (
SymTable(..)
, new
, SymTable.lookup
, lookupByteString
, deleteWithHash
, insertWithHash
, updateWithHash
, insert
, delete
, size
, src
, eob
, hash
, hashByteString
, assocs
, buckets
, Entry(..)
, loadFactor
, loadFactor'
) where
import qualified Data.Array.LI as ALI
import qualified Data.Array.LM as ALM
import qualified Data.ByteString as B
import qualified Data.ByteString.Internal as B
import qualified Data.ByteString.Unsafe as B
import qualified Data.Ref.FFF as RFFF
import qualified Data.Ref.L as RL
import qualified Data.Ref.UUU as RUUU
import Data.Bits
import Data.Word
import GHC.Exts
import GHC.ForeignPtr
import Common
import CoreTypes
--------------------------------------------------------------------------------
-- TODO: factor out all the shared reads from insert/delete for Cxt.Extension
-- Symtable entries
--------------------------------------------------------------------------------
data Entry
-- type, type val, def, cached (TopVar x v)
= Top Ty {-# unpack #-} GTy Tm Tm
-- level, type val
| Local Lvl {-# unpack #-} GTy
instance Show Entry where
showsPrec d (Local x _) = showParen (d > 10) (("Loc " ++ show x)++)
showsPrec d (Top x _ _ _) = showParen (d > 10) (("Top " ++ show x)++)
-- Span hashing
--------------------------------------------------------------------------------
newtype Hash = Hash {unHash :: Word}
deriving (Eq, Show, Ord, Num, Bits) via Word
hashToInt :: Hash -> Int
hashToInt (Hash w) = fromIntegral w
{-# inline hashToInt #-}
unW# :: Word -> Word#
unW# (W# x) = x
{-# inline unW# #-}
foldedMultiply# :: Word# -> Word# -> Word#
foldedMultiply# x y = case timesWord2# x y of
(# hi, lo #) -> xor# hi lo
{-# inline foldedMultiply# #-}
multiple :: Word
multiple = 11400714819323198549
{-# inline multiple #-}
salt :: Word
salt = 3032525626373534813
{-# inline salt #-}
combine# :: Word# -> Word# -> Word#
combine# x y = foldedMultiply# (xor# x y) (unW# multiple)
{-# inline combine# #-}
goHash :: Word# -> Addr# -> Int# -> Word#
goHash hash ptr len = case len <# 8# of
1# -> case len of
0# -> hash
_ -> combine# hash (indexPartialWord# len ptr)
_ -> goHash (combine# hash (indexWordOffAddr# ptr 0#)) (plusAddr# ptr 8#) (len -# 8#)
goHash' :: Word# -> Addr# -> Int# -> Word#
goHash' hash ptr len = case len <# 8# of
1# -> case len of
0# -> hash
_ -> combine# hash (indexPartialWord'# len ptr)
_ -> goHash' (combine# hash (indexWordOffAddr# ptr 0#)) (plusAddr# ptr 8#) (len -# 8#)
hash# :: Addr# -> Span -> Hash
hash# eob (Span (Pos (I# x)) (Pos (I# y))) = let
start = plusAddr# eob (negateInt# x)
len = x -# y
in case y <# 8# of
1# -> Hash (W# (goHash' (unW# salt) start len))
_ -> Hash (W# (goHash (unW# salt) start len))
{-# inline hash# #-}
hash :: SymTable -> Span -> IO Hash
hash (SymTable tbl) k = do
Ptr src <- RFFF.readSnd =<< RUUU.readFst tbl
pure $! hash# src k
{-# inline hash #-}
hashByteString :: Src -> IO Hash
hashByteString str = B.unsafeUseAsCString str \(Ptr addr) -> do
let !(I# l) = B.length str
pure $! hash# (plusAddr# addr l) (Span (Pos (I# l)) (Pos 0))
-- Buckets
--------------------------------------------------------------------------------
data Bucket = Empty | Cons Hash {-# unpack #-} Span Entry Bucket
foldlBucket :: (b -> Hash -> Span -> Entry -> b) -> b -> Bucket -> b
foldlBucket f acc b = go acc b where
go acc Empty = acc
go acc (Cons h k v b) = let acc' = f acc h k v in go acc' b
{-# inline foldlBucket #-}
deleteFromBucket :: Addr# -> Span -> Bucket -> (# Bucket, (# Entry | (# #) #) #)
deleteFromBucket = go where
go src k topB = case topB of
Empty -> (# Empty, (# | (# #) #) #)
Cons h' k' v' b
| 1# <- eqSpan# src k k' -> (# b, (# v' | #) #)
| otherwise ->
let !(# !b', del #) = go src k b
in if ptrEq b b' then (# topB, del #) else (# Cons h' k' v' b', del #)
lookupBucket :: Addr# -> Span -> Bucket -> (# Entry | (# #) #)
lookupBucket src k = \case
Empty -> (# | (# #) #)
Cons h' k' v' b
| 1# <- eqSpan# src k k' -> (# v' | #)
| otherwise -> lookupBucket src k b
lookupBSBucket :: Addr# -> Addr# -> Span -> Bucket -> (# Entry | (# #) #)
lookupBSBucket src' src k = \case
Empty -> (# | (# #) #)
Cons h' k' v' b
| 1# <- eqBasedSpan# src k src' k' -> (# v' | #)
| otherwise -> lookupBSBucket src' src k b
writeBucketAtIx :: Int -> Hash -> Span -> Entry -> Bucket -> Bucket
writeBucketAtIx i h k v e = case (,) $$! i $$! e of
(0, Cons _ _ _ b ) -> Cons h k v b
(i, Cons h' k' v' b) -> Cons h' k' v' (writeBucketAtIx (i - 1) h k v b)
(_, _ ) -> undefined
insertToBucket
:: Addr# -> Hash -> Span -> Entry -> Bucket -> (# Bucket, (# Entry | (# #) #) #)
insertToBucket src h k ~v ~b = go src 0 h k v b b where
go :: Addr# -> Int -> Hash -> Span -> Entry -> Bucket -> Bucket -> (# Bucket, (# Entry | (# #) #) #)
go src i h k ~v ~topB b = case b of
Empty -> let c = Cons h k v topB in (# c, (# | (# #) #) #)
Cons h' k' v' b
| 1# <- eqSpan# src k k' -> let b = writeBucketAtIx i h k v topB in (# b, (# v' | #) #)
| otherwise -> go src (i + 1) h k v topB b
-- SymTable
--------------------------------------------------------------------------------
newtype SymTable = SymTable
(RUUU.Ref (RFFF.Ref Int (Ptr Word8) Int)
(ALM.Array Bucket)
(RL.Ref ForeignPtrContents))
--------------------------------------------------------------------------------
initSlotsBits :: Int
initSlotsBits = 5
{-# inline initSlotsBits #-}
initSlots :: Int
initSlots = unsafeShiftL 1 initSlotsBits
{-# inline initSlots #-}
eob :: SymTable -> IO (Ptr Word8)
eob (SymTable tbl) = do
ref <- RUUU.readFst tbl
RFFF.readSnd ref
{-# inline eob #-}
new'# :: Int -> Ptr Word8 -> Int -> ForeignPtrContents -> IO SymTable
new'# slots eob len fpc = do
ref <- RFFF.new 0 eob len
fpcr <- RL.new fpc
buckets <- ALM.new slots Empty
table <- RUUU.new ref buckets fpcr
pure $! SymTable table
new# :: Ptr Word8 -> Int -> ForeignPtrContents -> IO SymTable
new# = new'# initSlots
{-# inline new# #-}
new :: Src -> IO SymTable
new (B.BS (ForeignPtr base ftc) (I# len)) =
new# (Ptr (plusAddr# base len)) (I# len) ftc
lookupByteString :: Src -> SymTable -> IO (UMaybe Entry)
lookupByteString k (SymTable tbl) = B.unsafeUseAsCString k \(Ptr base) -> do
let !(I# len) = B.length k
buckets <- RUUU.readSnd tbl
h <- hashByteString k
Ptr src <- RFFF.readSnd =<< RUUU.readFst tbl
let bucketsSize = ALM.size buckets
shift = 64 - ctzInt bucketsSize
ix = hashToInt (unsafeShiftR h shift)
b <- ALM.read buckets ix
let end = plusAddr# base len
let span = Span (Pos (I# len)) (Pos 0)
let !bckt = lookupBSBucket src end span b
pure $! UMaybe# bckt
lookup :: Span -> SymTable -> IO (UMaybe Entry)
lookup k (SymTable tbl) = do
buckets <- RUUU.readSnd tbl
Ptr src <- RFFF.readSnd =<< RUUU.readFst tbl
let bucketsSize = ALM.size buckets
shift = 64 - ctzInt bucketsSize
h = hash# src k
ix = hashToInt (unsafeShiftR h shift)
b <- ALM.read buckets ix
pure $! UMaybe# (lookupBucket src k b)
resize# :: Int -> SymTable -> IO ()
resize# bucketsSize' (SymTable tbl) = do
buckets <- RUUU.readSnd tbl
buckets' <- ALM.new bucketsSize' Empty
let shift = 64 - ctzInt bucketsSize'
ALM.for buckets \b ->
let go Empty =
pure ()
go (Cons h k v b) = do
ALM.modify' buckets' (hashToInt (unsafeShiftR h shift)) (Cons h k v)
go b
in go b
RUUU.writeSnd tbl buckets'
{-# noinline resize# #-}
deleteWithHash :: Span -> Hash -> SymTable -> IO (UMaybe Entry)
deleteWithHash k h (SymTable tbl) = do
ref <- RUUU.readFst tbl
I# size <- RFFF.readFst ref
Ptr src <- RFFF.readSnd ref
buckets <- RUUU.readSnd tbl
let bucketsSize = ALM.size buckets
shift = 64 - ctzInt bucketsSize
ix = hashToInt (unsafeShiftR h shift)
b <- ALM.read buckets ix
let !(# !b', old #) = deleteFromBucket src k b
let size' = I# size - (2 - tag (UMaybe# old))
ALM.write buckets ix b'
RFFF.writeFst ref size'
let downsize = unsafeShiftR bucketsSize 3
when (size' <= downsize && downsize >= initSlots) $
resize# (unsafeShiftR bucketsSize 1) (SymTable tbl)
pure $! UMaybe# old
insertWithHash :: Span -> Hash -> Entry -> SymTable -> IO (UMaybe Entry)
insertWithHash k h v (SymTable tbl) = do
ref <- RUUU.readFst tbl
I# size <- RFFF.readFst ref
Ptr src <- RFFF.readSnd ref
buckets <- RUUU.readSnd tbl
let bucketsSize = ALM.size buckets
shift = 64 - ctzInt bucketsSize
ix = hashToInt (unsafeShiftR h shift)
b <- ALM.read buckets ix
let !(# b', old #) = insertToBucket src h k v b
let size' = I# size + tag (UMaybe# old) - 1
ALM.write buckets ix b'
RFFF.writeFst ref size'
when (size' >= unsafeShiftR bucketsSize 1) $
resize# (unsafeShiftL bucketsSize 1) (SymTable tbl)
pure $! UMaybe# old
updateWithHash :: Span -> Hash -> UMaybe Entry -> SymTable -> IO (UMaybe Entry)
updateWithHash k h mv tbl = case mv of
UNothing -> do
-- debug ["deletewithhash", showSpan (src tbl) k]
deleteWithHash k h tbl
UJust v -> do
-- debug ["insertwithhash", showSpan (src tbl) k]
insertWithHash k h v tbl
{-# inline updateWithHash #-}
insert :: Span -> Entry -> SymTable -> IO (UMaybe Entry)
insert k v tbl = do
h <- hash tbl k
insertWithHash k h v tbl
delete :: Span -> SymTable -> IO (UMaybe Entry)
delete k tbl = do
h <- hash tbl k
deleteWithHash k h tbl
size :: SymTable -> IO Int
size (SymTable tbl) = RFFF.readFst =<< RUUU.readFst tbl
{-# inline size #-}
src :: SymTable -> Src
src (SymTable tbl) = runIO do
ref <- RUUU.readFst tbl
Ptr end <- RFFF.readSnd ref
I# len <- RFFF.readThd ref
fptr <- RL.read =<< RUUU.readThd tbl
let start = plusAddr# end (negateInt# len)
pure $! B.BS (ForeignPtr start fptr) (I# len)
-- testing
--------------------------------------------------------------------------------
loadFactor :: SymTable -> IO Double
loadFactor (SymTable tbl) = do
ref <- RUUU.readFst tbl
size <- RFFF.readFst ref
buckets <- RUUU.readSnd tbl
let bucketsSize = ALM.size buckets
pure $! fromIntegral size / fromIntegral bucketsSize
loadFactor' :: SymTable -> IO Double
loadFactor' tbl = do
bs <- buckets tbl
let blen = length bs
let size = length $ concat bs
pure $! fromIntegral size / fromIntegral blen
assocs :: SymTable -> IO [(String, Entry)]
assocs stbl@(SymTable tbl) = do
buckets <- ALM.freeze =<< RUUU.readSnd tbl
pure $! ALI.foldl'
(\acc b -> foldlBucket
(\acc h k v -> (showSpan (src stbl) k, v):acc) acc b)
[] buckets
buckets :: SymTable -> IO [[(Hash, String, Entry)]]
buckets stbl@(SymTable tbl) = do
buckets <- ALM.freeze =<< RUUU.readSnd tbl
pure $! ALI.foldl'
(\acc b -> foldlBucket
(\acc h k v -> (h, showSpan (src stbl) k, v):acc) [] b : acc)
[] buckets
testHash :: Src -> Span -> Hash
testHash str s = runIO $ B.unsafeUseAsCString str \(Ptr addr) -> do
let !(I# l) = B.length str
eob = plusAddr# addr l
pure $! hash# eob s
testEqSpan :: Src -> Span -> Span -> Bool
testEqSpan str s s' = runIO $ B.unsafeUseAsCString str \(Ptr addr) -> do
let !(I# l) = B.length str
eob = plusAddr# addr l
pure $! isTrue# (eqSpan# eob s s')
-- test = do
-- tbl <- new "EvalCon0EvalCon0 "
-- insert (Span (Pos 24) (Pos 16)) (Local 10 (gjoin VU)) tbl
-- insert (Span (Pos 16) (Pos 8)) (Local 10 (gjoin VU)) tbl
-- -- insert (Span (Pos 8) (Pos 0)) (Local 10 (gjoin VU)) tbl
-- -- lk <- SymTable.lookup (Span (Pos 16) (Pos 8)) tbl
-- -- print lk
-- mapM_ print =<< buckets tbl