Skip to content

Commit 203b7f8

Browse files
committed
Use shrink in filterMapAux
This results in a ~8% speedup in the filterWithKey benchmark. Context: #362
1 parent 19674b5 commit 203b7f8

File tree

2 files changed

+4
-9
lines changed

2 files changed

+4
-9
lines changed

Data/HashMap/Internal.hs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2100,9 +2100,9 @@ filterMapAux onLeaf onColl = go
21002100
ch <- A.read mary 0
21012101
case ch of
21022102
t | isLeafOrCollision t -> return t
2103-
_ -> BitmapIndexed b <$> A.trim mary 1
2103+
_ -> BitmapIndexed b <$> (A.unsafeFreeze =<< A.shrink mary 1)
21042104
_ -> do
2105-
ary2 <- A.trim mary j
2105+
ary2 <- A.unsafeFreeze =<< A.shrink mary j
21062106
return $! if j == maxChildren
21072107
then Full ary2
21082108
else BitmapIndexed b ary2
@@ -2129,7 +2129,7 @@ filterMapAux onLeaf onColl = go
21292129
return $! Leaf h l
21302130
_ | i == j -> do ary2 <- A.unsafeFreeze mary
21312131
return $! Collision h ary2
2132-
| otherwise -> do ary2 <- A.trim mary j
2132+
| otherwise -> do ary2 <- A.unsafeFreeze =<< A.shrink mary j
21332133
return $! Collision h ary2
21342134
| Just el <- onColl $! A.index ary i
21352135
= A.write mary j el >> step ary mary (i+1) (j+1) n

Data/HashMap/Internal/Array.hs

+1-6
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,14 @@ module Data.HashMap.Internal.Array
5252
, insertM
5353
, delete
5454
, sameArray1
55-
, trim
5655

5756
, unsafeFreeze
5857
, unsafeThaw
5958
, unsafeSameArray
6059
, run
6160
, copy
6261
, copyM
62+
, cloneM
6363

6464
-- * Folds
6565
, foldl
@@ -318,11 +318,6 @@ cloneM _mary@(MArray mary#) _off@(I# off#) _len@(I# len#) =
318318
case cloneSmallMutableArray# mary# off# len# s of
319319
(# s', mary'# #) -> (# s', MArray mary'# #)
320320

321-
-- | Create a new array of the @n@ first elements of @mary@.
322-
trim :: MArray s a -> Int -> ST s (Array a)
323-
trim mary n = cloneM mary 0 n >>= unsafeFreeze
324-
{-# INLINE trim #-}
325-
326321
-- | \(O(n)\) Insert an element at the given position in this array,
327322
-- increasing its size by one.
328323
insert :: Array e -> Int -> e -> Array e

0 commit comments

Comments
 (0)