Skip to content

Commit 2c8a286

Browse files
authored
32bit base (#317)
* bump to 32 bit base * bump benchmarks size bump benchmark element count * base 16 -> base 32 in docs * Internal.Strict: update16 --> update32 * Benchmarks: revert to 2^12 elements Closes #300
1 parent db9ddde commit 2c8a286

File tree

4 files changed

+34
-33
lines changed

4 files changed

+34
-33
lines changed

Data/HashMap/Internal.hs

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,9 @@ module Data.HashMap.Internal
116116
, sparseIndex
117117
, two
118118
, unionArrayBy
119-
, update16
120-
, update16M
121-
, update16With'
119+
, update32
120+
, update32M
121+
, update32With'
122122
, updateOrConcatWith
123123
, updateOrConcatWithKey
124124
, filterMapAux
@@ -809,7 +809,7 @@ insert' h0 k0 v0 m0 = go h0 k0 v0 0 m0
809809
!st' = go h k x (s+bitsPerSubkey) st
810810
in if st' `ptrEq` st
811811
then t
812-
else Full (update16 ary i st')
812+
else Full (update32 ary i st')
813813
where i = index h s
814814
go h k x s t@(Collision hy v)
815815
| h == hy = Collision h (updateOrSnocWith (\a _ -> (# a #)) k x v)
@@ -843,7 +843,7 @@ insertNewKey !h0 !k0 x0 !m0 = go h0 k0 x0 0 m0
843843
go h k x s (Full ary) =
844844
let !st = A.index ary i
845845
!st' = go h k x (s+bitsPerSubkey) st
846-
in Full (update16 ary i st')
846+
in Full (update32 ary i st')
847847
where i = index h s
848848
go h k x s t@(Collision hy v)
849849
| h == hy = Collision h (snocNewLeaf (L k x) v)
@@ -887,7 +887,7 @@ insertKeyExists !collPos0 !h0 !k0 x0 !m0 = go collPos0 h0 k0 x0 0 m0
887887
go collPos h k x s (Full ary) =
888888
let !st = A.index ary i
889889
!st' = go collPos h k x (s+bitsPerSubkey) st
890-
in Full (update16 ary i st')
890+
in Full (update32 ary i st')
891891
where i = index h s
892892
go collPos h k x _s (Collision _hy v)
893893
| collPos >= 0 = Collision h (setAtPosition collPos k x v)
@@ -1015,7 +1015,7 @@ insertModifying x f k0 m0 = go h0 k0 0 m0
10151015
go h k s t@(Full ary) =
10161016
let !st = A.index ary i
10171017
!st' = go h k (s+bitsPerSubkey) st
1018-
ary' = update16 ary i $! st'
1018+
ary' = update32 ary i $! st'
10191019
in if ptrEq st st'
10201020
then t
10211021
else Full ary'
@@ -1236,7 +1236,7 @@ adjust# f k0 m0 = go h0 k0 0 m0
12361236
let i = index h s
12371237
!st = A.index ary i
12381238
!st' = go h k (s+bitsPerSubkey) st
1239-
ary' = update16 ary i $! st'
1239+
ary' = update32 ary i $! st'
12401240
in if ptrEq st st'
12411241
then t
12421242
else Full ary'
@@ -1622,12 +1622,12 @@ unionWithKey f = go 0
16221622
go s (Full ary1) t2 =
16231623
let h2 = leafHashCode t2
16241624
i = index h2 s
1625-
ary' = update16With' ary1 i $ \st1 -> go (s+bitsPerSubkey) st1 t2
1625+
ary' = update32With' ary1 i $ \st1 -> go (s+bitsPerSubkey) st1 t2
16261626
in Full ary'
16271627
go s t1 (Full ary2) =
16281628
let h1 = leafHashCode t1
16291629
i = index h1 s
1630-
ary' = update16With' ary2 i $ \st2 -> go (s+bitsPerSubkey) t1 st2
1630+
ary' = update32With' ary2 i $ \st2 -> go (s+bitsPerSubkey) t1 st2
16311631
in Full ary'
16321632

16331633
leafHashCode (Leaf h _) = h
@@ -2252,36 +2252,36 @@ subsetArray cmpV ary1 ary2 = A.length ary1 <= A.length ary2 && A.all inAry2 ary1
22522252
-- Manually unrolled loops
22532253

22542254
-- | /O(n)/ Update the element at the given position in this array.
2255-
update16 :: A.Array e -> Int -> e -> A.Array e
2256-
update16 ary idx b = runST (update16M ary idx b)
2257-
{-# INLINE update16 #-}
2255+
update32 :: A.Array e -> Int -> e -> A.Array e
2256+
update32 ary idx b = runST (update32M ary idx b)
2257+
{-# INLINE update32 #-}
22582258

22592259
-- | /O(n)/ Update the element at the given position in this array.
2260-
update16M :: A.Array e -> Int -> e -> ST s (A.Array e)
2261-
update16M ary idx b = do
2262-
mary <- clone16 ary
2260+
update32M :: A.Array e -> Int -> e -> ST s (A.Array e)
2261+
update32M ary idx b = do
2262+
mary <- clone ary
22632263
A.write mary idx b
22642264
A.unsafeFreeze mary
2265-
{-# INLINE update16M #-}
2265+
{-# INLINE update32M #-}
22662266

22672267
-- | /O(n)/ Update the element at the given position in this array, by applying a function to it.
2268-
update16With' :: A.Array e -> Int -> (e -> e) -> A.Array e
2269-
update16With' ary idx f
2268+
update32With' :: A.Array e -> Int -> (e -> e) -> A.Array e
2269+
update32With' ary idx f
22702270
| (# x #) <- A.index# ary idx
2271-
= update16 ary idx $! f x
2272-
{-# INLINE update16With' #-}
2271+
= update32 ary idx $! f x
2272+
{-# INLINE update32With' #-}
22732273

2274-
-- | Unsafely clone an array of 16 elements. The length of the input
2274+
-- | Unsafely clone an array of (2^bitsPerSubkey) elements. The length of the input
22752275
-- array is not checked.
2276-
clone16 :: A.Array e -> ST s (A.MArray s e)
2277-
clone16 ary =
2278-
A.thaw ary 0 16
2276+
clone :: A.Array e -> ST s (A.MArray s e)
2277+
clone ary =
2278+
A.thaw ary 0 (2^bitsPerSubkey)
22792279

22802280
------------------------------------------------------------------------
22812281
-- Bit twiddling
22822282

22832283
bitsPerSubkey :: Int
2284-
bitsPerSubkey = 4
2284+
bitsPerSubkey = 5
22852285

22862286
maxChildren :: Int
22872287
maxChildren = 1 `unsafeShiftL` bitsPerSubkey
@@ -2291,6 +2291,7 @@ subkeyMask = 1 `unsafeShiftL` bitsPerSubkey - 1
22912291

22922292
sparseIndex :: Bitmap -> Bitmap -> Int
22932293
sparseIndex b m = popCount (b .&. (m - 1))
2294+
{-# INLINE sparseIndex #-}
22942295

22952296
mask :: Word -> Shift -> Bitmap
22962297
mask w s = 1 `unsafeShiftL` index w s

Data/HashMap/Internal/Strict.hs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
-- strings.
3636
--
3737
-- Many operations have a average-case complexity of /O(log n)/. The
38-
-- implementation uses a large base (i.e. 16) so in practice these
38+
-- implementation uses a large base (i.e. 32) so in practice these
3939
-- operations are constant time.
4040
module Data.HashMap.Internal.Strict
4141
(
@@ -195,7 +195,7 @@ insertWith f k0 v0 m0 = go h0 k0 v0 0 m0
195195
go h k x s (Full ary) =
196196
let st = A.index ary i
197197
st' = go h k x (s+bitsPerSubkey) st
198-
ary' = update16 ary i $! st'
198+
ary' = update32 ary i $! st'
199199
in Full ary'
200200
where i = index h s
201201
go h k x s t@(Collision hy v)
@@ -266,7 +266,7 @@ adjust f k0 m0 = go h0 k0 0 m0
266266
let i = index h s
267267
st = A.index ary i
268268
st' = go h k (s+bitsPerSubkey) st
269-
ary' = update16 ary i $! st'
269+
ary' = update32 ary i $! st'
270270
in Full ary'
271271
go h k _ t@(Collision hy v)
272272
| h == hy = Collision h (updateWith f k v)
@@ -494,12 +494,12 @@ unionWithKey f = go 0
494494
go s (Full ary1) t2 =
495495
let h2 = leafHashCode t2
496496
i = index h2 s
497-
ary' = update16With' ary1 i $ \st1 -> go (s+bitsPerSubkey) st1 t2
497+
ary' = update32With' ary1 i $ \st1 -> go (s+bitsPerSubkey) st1 t2
498498
in Full ary'
499499
go s t1 (Full ary2) =
500500
let h1 = leafHashCode t1
501501
i = index h1 s
502-
ary' = update16With' ary2 i $ \st2 -> go (s+bitsPerSubkey) t1 st2
502+
ary' = update32With' ary2 i $ \st2 -> go (s+bitsPerSubkey) t1 st2
503503
in Full ary'
504504

505505
leafHashCode (Leaf h _) = h

Data/HashMap/Lazy.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
-- strings.
2121
--
2222
-- Many operations have a average-case complexity of /O(log n)/. The
23-
-- implementation uses a large base (i.e. 16) so in practice these
23+
-- implementation uses a large base (i.e. 32) so in practice these
2424
-- operations are constant time.
2525
module Data.HashMap.Lazy
2626
(

Data/HashSet/Internal.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
-- strings.
3636
--
3737
-- Many operations have a average-case complexity of /O(log n)/. The
38-
-- implementation uses a large base (i.e. 16) so in practice these
38+
-- implementation uses a large base (i.e. 32) so in practice these
3939
-- operations are constant time.
4040

4141
module Data.HashSet.Internal

0 commit comments

Comments
 (0)