Skip to content

Commit 0a474c7

Browse files
authored
Use shrink in filterMapAux (#433)
This results in a ~8% speedup in the filterWithKey benchmark. Context: #362
1 parent 8f7ebc5 commit 0a474c7

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
@@ -2101,9 +2101,9 @@ filterMapAux onLeaf onColl = go
21012101
ch <- A.read mary 0
21022102
case ch of
21032103
t | isLeafOrCollision t -> return t
2104-
_ -> BitmapIndexed b <$> A.trim mary 1
2104+
_ -> BitmapIndexed b <$> (A.unsafeFreeze =<< A.shrink mary 1)
21052105
_ -> do
2106-
ary2 <- A.trim mary j
2106+
ary2 <- A.unsafeFreeze =<< A.shrink mary j
21072107
return $! if j == maxChildren
21082108
then Full ary2
21092109
else BitmapIndexed b ary2
@@ -2130,7 +2130,7 @@ filterMapAux onLeaf onColl = go
21302130
return $! Leaf h l
21312131
_ | i == j -> do ary2 <- A.unsafeFreeze mary
21322132
return $! Collision h ary2
2133-
| otherwise -> do ary2 <- A.trim mary j
2133+
| otherwise -> do ary2 <- A.unsafeFreeze =<< A.shrink mary j
21342134
return $! Collision h ary2
21352135
| Just el <- onColl $! A.index ary i
21362136
= 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)