Skip to content

Commit e75e359

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

File tree

2 files changed

+5
-0
lines changed

2 files changed

+5
-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

+3
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

0 commit comments

Comments
 (0)