Skip to content

Commit ec94da2

Browse files
committed
Also apply Bin 1 check to filter. It makes it 20% faster
1 parent 2f1eebc commit ec94da2

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

containers-tests/benchmarks/Map.hs

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ main = do
2727
, bench "lookup present" $ whnf (lookup evens) m_even
2828
, bench "map" $ whnf (M.map (+ 1)) m
2929
, bench "map really" $ nf (M.map (+ 2)) m
30+
, bench "filter" $ whnf (M.filter even) m
31+
, bench "filter really" $ nf (M.filter even) m
3032
, bench "<$" $ whnf ((1 :: Int) <$) m
3133
, bench "<$ really" $ nf ((2 :: Int) <$) m
3234
, bench "alterF lookup absent" $ whnf (atLookup evens) m_odd

containers/src/Data/Map/Internal.hs

+5
Original file line numberDiff line numberDiff line change
@@ -2966,6 +2966,9 @@ filter p m
29662966

29672967
filterWithKey :: (k -> a -> Bool) -> Map k a -> Map k a
29682968
filterWithKey _ Tip = Tip
2969+
filterWithKey p t@(Bin 1 kx x _ _)
2970+
| p kx x = t
2971+
| otherwise = Tip
29692972
filterWithKey p t@(Bin _ kx x l r)
29702973
| p kx x = if pl `ptrEq` l && pr `ptrEq` r
29712974
then t
@@ -2978,6 +2981,8 @@ filterWithKey p t@(Bin _ kx x l r)
29782981
-- predicate.
29792982
filterWithKeyA :: Applicative f => (k -> a -> f Bool) -> Map k a -> f (Map k a)
29802983
filterWithKeyA _ Tip = pure Tip
2984+
filterWithKeyA p t@(Bin 1 kx x _ _) =
2985+
fmap (bool Tip t) (p kx x)
29812986
filterWithKeyA p t@(Bin _ kx x l r) =
29822987
liftA3 combine (filterWithKeyA p l) (p kx x) (filterWithKeyA p r)
29832988
where

0 commit comments

Comments
 (0)