Skip to content

Commit a4f9bc6

Browse files
committed
Fix 'unsafeInsertWithInternal' in Strict module
This commit fixes the 'unsafeInsertWithInternal' function. The value-strictness test for 'fromListWith' was failing because a comparison between two hashmaps was failing since their size-tracking field differed. This was due to the insertion function used by 'fromListWith' not updating the hashmap's size correctly when a leaf which does not collide with any other leaves in the hashmap was inserted in it. The 'unsafeInsertWithInternal' function was also updated with more '$!' operators to reduce parentheses.
1 parent 3f6c515 commit a4f9bc6

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

Data/HashMap/Strict.hs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ insertWithInternal f k0 v0 m0 = go h0 k0 v0 0 m0
163163
| hy == h = if ky == k
164164
then A.Sized 0 (leaf h k (f x y))
165165
else A.Sized 1 (x `seq` (collision h l (L k x)))
166-
| otherwise = A.Sized 0 (x `seq` runST (two s h k x hy ky y))
166+
| otherwise = A.Sized 1 (x `seq` runST (two s h k x hy ky y))
167167
go h k x s (BitmapIndexed b ary)
168168
| b .&. m == 0 =
169169
let ary' = A.insert ary i $! leaf h k x
@@ -210,37 +210,39 @@ unsafeInsertWithInternal
210210
unsafeInsertWithInternal f k0 v0 m0 = runST (go h0 k0 v0 0 m0)
211211
where
212212
h0 = hash k0
213-
go !h !k x !_ Empty = return $! A.Sized 1 (leaf h k x)
213+
go !h !k x !_ Empty = return . A.Sized 1 $! leaf h k x
214214
go h k x s (Leaf hy l@(L ky y))
215215
| hy == h = if ky == k
216-
then return $! A.Sized 0 (leaf h k (f x y))
216+
then return . A.Sized 0 $! leaf h k (f x y)
217217
else do
218218
let l' = x `seq` (L k x)
219-
return $! A.Sized 1 (collision h l l')
220-
| otherwise = (x `seq` two s h k x hy ky y) >>= return . A.Sized 1
219+
return . A.Sized 1 $! collision h l l'
220+
| otherwise = do
221+
twoHM <- x `seq` two s h k x hy ky y
222+
return . A.Sized 1 $! twoHM
221223
go h k x s t@(BitmapIndexed b ary)
222224
| b .&. m == 0 = do
223225
ary' <- A.insertM ary i $! leaf h k x
224-
return $! A.Sized 1 (bitmapIndexedOrFull (b .|. m) ary')
226+
return . A.Sized 1 $! bitmapIndexedOrFull (b .|. m) ary'
225227
| otherwise = do
226228
st <- A.indexM ary i
227229
A.Sized sz st' <- go h k x (s+bitsPerSubkey) st
228-
A.unsafeUpdateM ary i st'
229-
return (A.Sized sz t)
230+
A.unsafeUpdateM ary i $! st'
231+
return . A.Sized sz $! t
230232
where m = mask h s
231233
i = sparseIndex b m
232234
go h k x s t@(Full ary) = do
233235
st <- A.indexM ary i
234236
A.Sized sz st' <- go h k x (s+bitsPerSubkey) st
235-
A.unsafeUpdateM ary i st'
236-
return (A.Sized sz t)
237+
A.unsafeUpdateM ary i $! st'
238+
return . A.Sized sz $! t
237239
where i = index h s
238240
go h k x s t@(Collision hy v)
239241
| h == hy =
240242
let !start = A.length v
241243
!newV = updateOrSnocWith f k x v
242244
!end = A.length newV
243-
in return $! A.Sized (end - start) (Collision h newV)
245+
in return . A.Sized (end - start) $! Collision h newV
244246
| otherwise = go h k x s $ BitmapIndexed (mask hy s) (A.singleton t)
245247
{-# INLINABLE unsafeInsertWithInternal #-}
246248

0 commit comments

Comments
 (0)