Skip to content

Commit

Permalink
Also apply Bin 1 check to filter. It makes it 20% faster
Browse files Browse the repository at this point in the history
  • Loading branch information
HuwCampbell committed Nov 15, 2024
1 parent 2f1eebc commit e75e359
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 0 deletions.
2 changes: 2 additions & 0 deletions containers-tests/benchmarks/Map.hs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ main = do
, bench "lookup present" $ whnf (lookup evens) m_even
, bench "map" $ whnf (M.map (+ 1)) m
, bench "map really" $ nf (M.map (+ 2)) m
, bench "filter" $ whnf (M.filter even) m
, bench "filter really" $ nf (M.filter even) m
, bench "<$" $ whnf ((1 :: Int) <$) m
, bench "<$ really" $ nf ((2 :: Int) <$) m
, bench "alterF lookup absent" $ whnf (atLookup evens) m_odd
Expand Down
3 changes: 3 additions & 0 deletions containers/src/Data/Map/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2966,6 +2966,9 @@ filter p m

filterWithKey :: (k -> a -> Bool) -> Map k a -> Map k a
filterWithKey _ Tip = Tip
filterWithKey p t@(Bin 1 kx x _ _)
| p kx x = t
| otherwise = Tip
filterWithKey p t@(Bin _ kx x l r)
| p kx x = if pl `ptrEq` l && pr `ptrEq` r
then t
Expand Down

0 comments on commit e75e359

Please sign in to comment.