Skip to content

Commit 8d09e9a

Browse files
committed
Define unsafeInsertWith using unsafeInsertWithKey
See #246 (comment) for performance numbers.
1 parent 2c190bb commit 8d09e9a

File tree

2 files changed

+2
-63
lines changed

2 files changed

+2
-63
lines changed

Data/HashMap/Base.hs

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -938,39 +938,9 @@ insertModifyingArr x f k0 ary0 = go k0 ary0 0 (A.length ary0)
938938
unsafeInsertWith :: forall k v. (Eq k, Hashable k)
939939
=> (v -> v -> v) -> k -> v -> HashMap k v
940940
-> HashMap k v
941-
unsafeInsertWith f k0 v0 m0 = runST (go h0 k0 v0 0 m0)
942-
where
943-
h0 = hash k0
944-
go :: Hash -> k -> v -> Shift -> HashMap k v -> ST s (HashMap k v)
945-
go !h !k x !_ Empty = return $! Leaf h (L k x)
946-
go h k x s t@(Leaf hy l@(L ky y))
947-
| hy == h = if ky == k
948-
then return $! Leaf h (L k (f x y))
949-
else return $! collision h l (L k x)
950-
| otherwise = two s h k x hy t
951-
go h k x s t@(BitmapIndexed b ary)
952-
| b .&. m == 0 = do
953-
ary' <- A.insertM ary i $! Leaf h (L k x)
954-
return $! bitmapIndexedOrFull (b .|. m) ary'
955-
| otherwise = do
956-
st <- A.indexM ary i
957-
st' <- go h k x (s+bitsPerSubkey) st
958-
A.unsafeUpdateM ary i st'
959-
return t
960-
where m = mask h s
961-
i = sparseIndex b m
962-
go h k x s t@(Full ary) = do
963-
st <- A.indexM ary i
964-
st' <- go h k x (s+bitsPerSubkey) st
965-
A.unsafeUpdateM ary i st'
966-
return t
967-
where i = index h s
968-
go h k x s t@(Collision hy v)
969-
| h == hy = return $! Collision h (updateOrSnocWith f k x v)
970-
| otherwise = go h k x s $ BitmapIndexed (mask hy s) (A.singleton t)
941+
unsafeInsertWith f k0 v0 m0 = unsafeInsertWithKey (const f) k0 v0 m0
971942
{-# INLINABLE unsafeInsertWith #-}
972943

973-
-- | In-place update version of insertWith
974944
unsafeInsertWithKey :: forall k v. (Eq k, Hashable k)
975945
=> (k -> v -> v -> v) -> k -> v -> HashMap k v
976946
-> HashMap k v

Data/HashMap/Strict/Base.hs

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -184,40 +184,9 @@ insertWith f k0 v0 m0 = go h0 k0 v0 0 m0
184184
-- | In-place update version of insertWith
185185
unsafeInsertWith :: (Eq k, Hashable k) => (v -> v -> v) -> k -> v -> HashMap k v
186186
-> HashMap k v
187-
unsafeInsertWith f k0 v0 m0 = runST (go h0 k0 v0 0 m0)
188-
where
189-
h0 = hash k0
190-
go !h !k x !_ Empty = return $! leaf h k x
191-
go h k x s t@(Leaf hy l@(L ky y))
192-
| hy == h = if ky == k
193-
then return $! leaf h k (f x y)
194-
else do
195-
let l' = x `seq` (L k x)
196-
return $! collision h l l'
197-
| otherwise = x `seq` two s h k x hy t
198-
go h k x s t@(BitmapIndexed b ary)
199-
| b .&. m == 0 = do
200-
ary' <- A.insertM ary i $! leaf h k x
201-
return $! bitmapIndexedOrFull (b .|. m) ary'
202-
| otherwise = do
203-
st <- A.indexM ary i
204-
st' <- go h k x (s+bitsPerSubkey) st
205-
A.unsafeUpdateM ary i st'
206-
return t
207-
where m = mask h s
208-
i = sparseIndex b m
209-
go h k x s t@(Full ary) = do
210-
st <- A.indexM ary i
211-
st' <- go h k x (s+bitsPerSubkey) st
212-
A.unsafeUpdateM ary i st'
213-
return t
214-
where i = index h s
215-
go h k x s t@(Collision hy v)
216-
| h == hy = return $! Collision h (updateOrSnocWith f k x v)
217-
| otherwise = go h k x s $ BitmapIndexed (mask hy s) (A.singleton t)
187+
unsafeInsertWith f k0 v0 m0 = unsafeInsertWithKey (const f) k0 v0 m0
218188
{-# INLINABLE unsafeInsertWith #-}
219189

220-
-- | In-place update version of insertWith
221190
unsafeInsertWithKey :: (Eq k, Hashable k) => (k -> v -> v -> v) -> k -> v -> HashMap k v
222191
-> HashMap k v
223192
unsafeInsertWithKey f k0 v0 m0 = runST (go h0 k0 v0 0 m0)

0 commit comments

Comments
 (0)