Skip to content

Commit

Permalink
Simplify Map minViewSure and maxViewSure (#1001)
Browse files Browse the repository at this point in the history
This avoids allocations of MinView and MaxView, improving performance.
Affected benchmarks include minView (-22%) and difference (-13%).
  • Loading branch information
meooow25 authored Aug 7, 2024
1 parent ddd130a commit 04d9198
Showing 1 changed file with 8 additions and 14 deletions.
22 changes: 8 additions & 14 deletions containers/src/Data/Map/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4050,22 +4050,16 @@ data MinView k a = MinView !k a !(Map k a)
data MaxView k a = MaxView !k a !(Map k a)

minViewSure :: k -> a -> Map k a -> Map k a -> MinView k a
minViewSure = go
where
go k x Tip r = MinView k x r
go k x (Bin _ kl xl ll lr) r =
case go kl xl ll lr of
MinView km xm l' -> MinView km xm (balanceR k x l' r)
{-# NOINLINE minViewSure #-}
minViewSure !k x l !r = case l of
Tip -> MinView k x r
Bin _ lk lx ll lr -> case minViewSure lk lx ll lr of
MinView km xm l' -> MinView km xm (balanceR k x l' r)

maxViewSure :: k -> a -> Map k a -> Map k a -> MaxView k a
maxViewSure = go
where
go k x l Tip = MaxView k x l
go k x l (Bin _ kr xr rl rr) =
case go kr xr rl rr of
MaxView km xm r' -> MaxView km xm (balanceL k x l r')
{-# NOINLINE maxViewSure #-}
maxViewSure !k x !l r = case r of
Tip -> MaxView k x l
Bin _ rk rx rl rr -> case maxViewSure rk rx rl rr of
MaxView km xm r' -> MaxView km xm (balanceL k x l r')

-- | \(O(\log n)\). Delete and find the minimal element.
--
Expand Down

0 comments on commit 04d9198

Please sign in to comment.