Skip to content

Commit aa19655

Browse files
committed
Use two with Collisions too
This results in a ~10% reduction in Code size for each of the insert variants. Closes #447.
1 parent 89c7baa commit aa19655

File tree

2 files changed

+6
-7
lines changed

2 files changed

+6
-7
lines changed

Data/HashMap/Internal.hs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -832,7 +832,7 @@ insert' h0 k0 v0 m0 = go h0 k0 v0 0 m0
832832
where i = index h s
833833
go h k x s t@(Collision hy v)
834834
| h == hy = Collision h (updateOrSnocWith (\a _ -> (# a #)) k x v)
835-
| otherwise = go h k x s $ BitmapIndexed (mask hy s) (A.singleton t)
835+
| otherwise = runST (two s h k x hy t)
836836
{-# INLINABLE insert' #-}
837837

838838
-- | Insert optimized for the case when we know the key is not in the map.
@@ -866,8 +866,7 @@ insertNewKey !h0 !k0 x0 !m0 = go h0 k0 x0 0 m0
866866
where i = index h s
867867
go h k x s t@(Collision hy v)
868868
| h == hy = Collision h (A.snoc v (L k x))
869-
| otherwise =
870-
go h k x s $ BitmapIndexed (mask hy s) (A.singleton t)
869+
| otherwise = runST (two s h k x hy t)
871870
{-# NOINLINE insertNewKey #-}
872871

873872

@@ -953,7 +952,7 @@ unsafeInsert k0 v0 m0 = runST (go h0 k0 v0 0 m0)
953952
where i = index h s
954953
go h k x s t@(Collision hy v)
955954
| h == hy = return $! Collision h (updateOrSnocWith (\a _ -> (# a #)) k x v)
956-
| otherwise = go h k x s $ BitmapIndexed (mask hy s) (A.singleton t)
955+
| otherwise = two s h k x hy t
957956
{-# INLINABLE unsafeInsert #-}
958957

959958
-- | Create a map from two key-value pairs which hashes don't collide. To
@@ -1107,7 +1106,7 @@ unsafeInsertWithKey f k0 v0 m0 = runST (go h0 k0 v0 0 m0)
11071106
where i = index h s
11081107
go h k x s t@(Collision hy v)
11091108
| h == hy = return $! Collision h (updateOrSnocWithKey f k x v)
1110-
| otherwise = go h k x s $ BitmapIndexed (mask hy s) (A.singleton t)
1109+
| otherwise = two s h k x hy t
11111110
{-# INLINABLE unsafeInsertWithKey #-}
11121111

11131112
-- | \(O(\log n)\) Remove the mapping for the specified key from this map

Data/HashMap/Internal/Strict.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ insertWith f k0 v0 m0 = go h0 k0 v0 0 m0
216216
where i = index h s
217217
go h k x s t@(Collision hy v)
218218
| h == hy = Collision h (updateOrSnocWith f k x v)
219-
| otherwise = go h k x s $ BitmapIndexed (mask hy s) (A.singleton t)
219+
| otherwise = x `seq` runST (HM.two s h k x hy t)
220220
{-# INLINABLE insertWith #-}
221221

222222
-- | In-place update version of insertWith
@@ -257,7 +257,7 @@ unsafeInsertWithKey f k0 v0 m0 = runST (go h0 k0 v0 0 m0)
257257
where i = index h s
258258
go h k x s t@(Collision hy v)
259259
| h == hy = return $! Collision h (updateOrSnocWithKey f k x v)
260-
| otherwise = go h k x s $ BitmapIndexed (mask hy s) (A.singleton t)
260+
| otherwise = x `seq` HM.two s h k x hy t
261261
{-# INLINABLE unsafeInsertWithKey #-}
262262

263263
-- | \(O(\log n)\) Adjust the value tied to a given key in this map only

0 commit comments

Comments
 (0)