From 04d919866731757d6a85fb7a855b3f49a051c80a Mon Sep 17 00:00:00 2001 From: Soumik Sarkar Date: Thu, 8 Aug 2024 04:24:24 +0530 Subject: [PATCH] Simplify Map minViewSure and maxViewSure (#1001) This avoids allocations of MinView and MaxView, improving performance. Affected benchmarks include minView (-22%) and difference (-13%). --- containers/src/Data/Map/Internal.hs | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/containers/src/Data/Map/Internal.hs b/containers/src/Data/Map/Internal.hs index 556149ad8..6b6a00829 100644 --- a/containers/src/Data/Map/Internal.hs +++ b/containers/src/Data/Map/Internal.hs @@ -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. --