Skip to content

Commit 647f6af

Browse files
authored
#254, avoid space leak with collisions
1 parent f1a53e2 commit 647f6af

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

Data/HashMap/Base.hs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -757,7 +757,7 @@ insert' h0 k0 v0 m0 = go h0 k0 v0 0 m0
757757
else Full (update16 ary i st')
758758
where i = index h s
759759
go h k x s t@(Collision hy v)
760-
| h == hy = Collision h (updateOrSnocWith const k x v)
760+
| h == hy = Collision h (updateOrSnocWith (\a _ -> (# a #)) k x v)
761761
| otherwise = go h k x s $ BitmapIndexed (mask hy s) (A.singleton t)
762762
{-# INLINABLE insert' #-}
763763

@@ -880,7 +880,7 @@ unsafeInsert k0 v0 m0 = runST (go h0 k0 v0 0 m0)
880880
return t
881881
where i = index h s
882882
go h k x s t@(Collision hy v)
883-
| h == hy = return $! Collision h (updateOrSnocWith const k x v)
883+
| h == hy = return $! Collision h (updateOrSnocWith (\a _ -> (# a #)) k x v)
884884
| otherwise = go h k x s $ BitmapIndexed (mask hy s) (A.singleton t)
885885
{-# INLINABLE unsafeInsert #-}
886886

@@ -1026,7 +1026,7 @@ unsafeInsertWith f k0 v0 m0 = runST (go h0 k0 v0 0 m0)
10261026
return t
10271027
where i = index h s
10281028
go h k x s t@(Collision hy v)
1029-
| h == hy = return $! Collision h (updateOrSnocWith f k x v)
1029+
| h == hy = return $! Collision h (updateOrSnocWith (\a b -> (# f a b #)) k x v)
10301030
| otherwise = go h k x s $ BitmapIndexed (mask hy s) (A.singleton t)
10311031
{-# INLINABLE unsafeInsertWith #-}
10321032

@@ -1394,10 +1394,10 @@ unionWithKey f = go 0
13941394
else collision h1 l1 l2
13951395
| otherwise = goDifferentHash s h1 h2 t1 t2
13961396
go s t1@(Leaf h1 (L k1 v1)) t2@(Collision h2 ls2)
1397-
| h1 == h2 = Collision h1 (updateOrSnocWithKey f k1 v1 ls2)
1397+
| h1 == h2 = Collision h1 (updateOrSnocWithKey (\k a b -> (# f k a b #)) k1 v1 ls2)
13981398
| otherwise = goDifferentHash s h1 h2 t1 t2
13991399
go s t1@(Collision h1 ls1) t2@(Leaf h2 (L k2 v2))
1400-
| h1 == h2 = Collision h1 (updateOrSnocWithKey (flip . f) k2 v2 ls1)
1400+
| h1 == h2 = Collision h1 (updateOrSnocWithKey (\k a b -> (# f k b a #)) k2 v2 ls1)
14011401
| otherwise = goDifferentHash s h1 h2 t1 t2
14021402
go s t1@(Collision h1 ls1) t2@(Collision h2 ls2)
14031403
| h1 == h2 = Collision h1 (updateOrConcatWithKey f ls1 ls2)
@@ -1932,12 +1932,12 @@ updateWith# f k0 ary0 = go k0 ary0 0 (A.length ary0)
19321932
| otherwise -> go k ary (i+1) n
19331933
{-# INLINABLE updateWith# #-}
19341934

1935-
updateOrSnocWith :: Eq k => (v -> v -> v) -> k -> v -> A.Array (Leaf k v)
1935+
updateOrSnocWith :: Eq k => (v -> v -> (# v #)) -> k -> v -> A.Array (Leaf k v)
19361936
-> A.Array (Leaf k v)
19371937
updateOrSnocWith f = updateOrSnocWithKey (const f)
19381938
{-# INLINABLE updateOrSnocWith #-}
19391939

1940-
updateOrSnocWithKey :: Eq k => (k -> v -> v -> v) -> k -> v -> A.Array (Leaf k v)
1940+
updateOrSnocWithKey :: Eq k => (k -> v -> v -> (# v #)) -> k -> v -> A.Array (Leaf k v)
19411941
-> A.Array (Leaf k v)
19421942
updateOrSnocWithKey f k0 v0 ary0 = go k0 v0 ary0 0 (A.length ary0)
19431943
where
@@ -1949,7 +1949,7 @@ updateOrSnocWithKey f k0 v0 ary0 = go k0 v0 ary0 0 (A.length ary0)
19491949
A.write mary n (L k v)
19501950
return mary
19511951
| otherwise = case A.index ary i of
1952-
(L kx y) | k == kx -> A.update ary i (L k (f k v y))
1952+
(L kx y) | k == kx, (# v2 #) <- f k v y -> A.update ary i (L k v2)
19531953
| otherwise -> go k v ary (i+1) n
19541954
{-# INLINABLE updateOrSnocWithKey #-}
19551955

0 commit comments

Comments
 (0)