Visitar URL original
Avoid thunks in IntSet minView and maxView by oheliav · Pull Request #1277 · haskell/containers · GitHub
Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 26 additions & 12 deletions containers/src/Data/IntSet/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1164,26 +1164,40 @@ splitMember x t =
-- | \(O(\min(n,W))\). Retrieves the maximal key of the set, and the set
-- stripped of that element, or 'Nothing' if passed an empty set.
maxView :: IntSet -> Maybe (Key, IntSet)
maxView t =
case t of Nil -> Nothing
Bin p l r | signBranch p -> case go l of (result, l') -> Just (result, binCheckL p l' r)
_ -> Just (go t)
maxView t = case t of
Nil -> Nothing
_ -> Just $ case maxViewSure t of result :*: t' -> (result, t')
{-# INLINE maxView #-}

maxViewSure :: IntSet -> StrictPair Key IntSet

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good overall, but instead of StrictPair we should have View type like for IntMap:

data View a = View {-# UNPACK #-} !Key a !(IntMap a)

This makes a difference for GHC <9.4, documented here:

-- This type allows GHC to return unboxed ints from leftmostTipSure, as
-- $wleftmostTipSure :: IntSet -> (# Int#, Word# #)
-- On a modern enough GHC (>=9.4) this is unnecessary, we could use StrictPair
-- instead and get the same Core.
data Tip' = Tip' {-# UNPACK #-} !Int {-# UNPACK #-} !BitMap

maxViewSure t =
case t of Nil -> error "maxViewSure Nil"
Bin p l r | signBranch p -> case go l of (result :*: l') -> result :*: binCheckL p l' r
_ -> go t
where
go (Bin p l r) = case go r of (result, r') -> (result, binCheckR p l r')
go (Tip kx bm) = case highestBitSet bm of bi -> (kx + bi, tip kx (bm .&. complement (bitmapOfSuffix bi)))
go (Bin p l r) = case go r of (result :*: r') -> result :*: binCheckR p l r'
go (Tip kx bm) = case highestBitSet bm of bi -> (kx + bi) :*: tip kx (bm .&. complement (bitmapOfSuffix bi))
go Nil = error "maxView Nil"
{-# NOINLINE maxViewSure #-}

-- | \(O(\min(n,W))\). Retrieves the minimal key of the set, and the set
-- stripped of that element, or 'Nothing' if passed an empty set.
minView :: IntSet -> Maybe (Key, IntSet)
minView t =
case t of Nil -> Nothing
Bin p l r | signBranch p -> case go r of (result, r') -> Just (result, binCheckR p l r')
_ -> Just (go t)
minView t = case t of
Nil -> Nothing
_ -> Just $ case minViewSure t of result :*: t' -> (result, t')
{-# INLINE minView #-}

minViewSure :: IntSet -> StrictPair Key IntSet
minViewSure t =
case t of Nil -> error "minViewSure Nil"
Bin p l r | signBranch p -> case go r of (result :*: r') -> result :*: binCheckR p l r'
_ -> go t
where
go (Bin p l r) = case go l of (result, l') -> (result, binCheckL p l' r)
go (Tip kx bm) = case lowestBitSet bm of bi -> (kx + bi, tip kx (bm .&. complement (bitmapOfSuffix bi)))
go (Bin p l r) = case go l of (result :*: l') -> result :*: binCheckL p l' r
go (Tip kx bm) = case lowestBitSet bm of bi -> (kx + bi) :*: tip kx (bm .&. complement (bitmapOfSuffix bi))
go Nil = error "minView Nil"
{-# NOINLINE minViewSure #-}

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