Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Faster disjoint/isSubsetOf for Set via unbalanced splitting. #865

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions containers-tests/benchmarks/Set.hs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ main = do
, bench "fromDistinctAscList" $ whnf S.fromDistinctAscList elems
, bench "disjoint:false" $ whnf (S.disjoint s) s_even
, bench "disjoint:true" $ whnf (S.disjoint s_odd) s_even
, bench "isSubsetOf:true" $ whnf (s_even `S.isSubsetOf`) s
, bench "isSubsetOf:false" $ whnf (s_even `S.isSubsetOf`) s_odd
, bench "null.intersection:false" $ whnf (S.null. S.intersection s) s_even
, bench "null.intersection:true" $ whnf (S.null. S.intersection s_odd) s_even
, bench "alterF:member" $ whnf (alterF_member elems) s
Expand Down
22 changes: 20 additions & 2 deletions containers/src/Data/Set/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,7 @@ isSubsetOfX (Bin _ x l r) t
size l <= size lt && size r <= size gt &&
isSubsetOfX l lt && isSubsetOfX r gt
where
(lt,found,gt) = splitMember x t
(lt,found,gt) = splitMemberUnbalanced x t
#if __GLASGOW_HASKELL__
{-# INLINABLE isSubsetOfX #-}
#endif
Expand Down Expand Up @@ -743,7 +743,7 @@ disjoint (Bin _ x l r) t
-- Analogous implementation to `subsetOfX`
= not found && disjoint l lt && disjoint r gt
where
(lt,found,gt) = splitMember x t
(lt,found,gt) = splitMemberUnbalanced x t

{--------------------------------------------------------------------
Minimal, Maximal
Expand Down Expand Up @@ -1308,6 +1308,24 @@ splitMember x (Bin _ y l r)
{-# INLINABLE splitMember #-}
#endif

-- Same as 'splitMember' but skips re-balancing by using 'bin' instead of 'link'.
-- Attempting to build new trees out of these will error when re-balancing but
-- this can improve performance when the resulting trees are disposable.
splitMemberUnbalanced :: Ord a => a -> Set a -> (Set a,Bool,Set a)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Tried to factor out bin/link to avoid code duplication but that made the involved functions 2-3x slower.

splitMemberUnbalanced _ Tip = (Tip, False, Tip)
splitMemberUnbalanced x (Bin _ y l r)
= case compare x y of
LT -> let (lt, found, gt) = splitMemberUnbalanced x l
!gt' = bin y gt r
in (lt, found, gt')
GT -> let (lt, found, gt) = splitMemberUnbalanced x r
!lt' = bin y l lt
in (lt', found, gt)
EQ -> (l, True, r)
#if __GLASGOW_HASKELL__
{-# INLINABLE splitMemberUnbalanced #-}
#endif

{--------------------------------------------------------------------
Indexing
--------------------------------------------------------------------}
Expand Down