Skip to content

Commit 93d6b7b

Browse files
committed
Introduce recursive worker to ‘splitMember’ to increase inlining chances
1 parent 43c15f0 commit 93d6b7b

File tree

5 files changed

+35
-12
lines changed

5 files changed

+35
-12
lines changed

containers-tests/containers-tests.cabal

+1
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ library
115115
Utils.Containers.Internal.PtrEquality
116116
Utils.Containers.Internal.State
117117
Utils.Containers.Internal.StrictMaybe
118+
Utils.Containers.Internal.StrictTriple
118119

119120
if impl(ghc)
120121
other-modules:

containers/containers.cabal

+1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ Library
7979
Utils.Containers.Internal.StrictMaybe
8080
Utils.Containers.Internal.PtrEquality
8181
Utils.Containers.Internal.Coercions
82+
Utils.Containers.Internal.StrictTriple
8283
if impl(ghc)
8384
other-modules:
8485
Utils.Containers.Internal.TypeError

containers/src/Data/Map/Internal.hs

+1-2
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,7 @@ import qualified Data.Set.Internal as Set
398398
import Data.Set.Internal (Set)
399399
import Utils.Containers.Internal.PtrEquality (ptrEq)
400400
import Utils.Containers.Internal.StrictPair
401+
import Utils.Containers.Internal.StrictTriple
401402
import Utils.Containers.Internal.StrictMaybe
402403
import Utils.Containers.Internal.BitQueue
403404
#ifdef DEFINE_ALTERF_FALLBACK
@@ -3983,8 +3984,6 @@ splitMember k0 m = case go k0 m of
39833984
{-# INLINABLE splitMember #-}
39843985
#endif
39853986

3986-
data StrictTriple a b c = StrictTriple !a !b !c
3987-
39883987
{--------------------------------------------------------------------
39893988
Utility functions that maintain the balance properties of the tree.
39903989
All constructors assume that all values in [l] < [k] and all values

containers/src/Data/Set/Internal.hs

+15-10
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ import qualified Data.Foldable as Foldable
250250
import Control.DeepSeq (NFData(rnf))
251251

252252
import Utils.Containers.Internal.StrictPair
253+
import Utils.Containers.Internal.StrictTriple
253254
import Utils.Containers.Internal.PtrEquality
254255

255256
#if __GLASGOW_HASKELL__
@@ -1318,16 +1319,20 @@ splitS x (Bin _ y l r)
13181319
-- | \(O(\log n)\). Performs a 'split' but also returns whether the pivot
13191320
-- element was found in the original set.
13201321
splitMember :: Ord a => a -> Set a -> (Set a,Bool,Set a)
1321-
splitMember _ Tip = (Tip, False, Tip)
1322-
splitMember x (Bin _ y l r)
1323-
= case compare x y of
1324-
LT -> let (lt, found, gt) = splitMember x l
1325-
!gt' = link y gt r
1326-
in (lt, found, gt')
1327-
GT -> let (lt, found, gt) = splitMember x r
1328-
!lt' = link y l lt
1329-
in (lt', found, gt)
1330-
EQ -> (l, True, r)
1322+
splitMember k0 s = case go k0 s of
1323+
StrictTriple l b r -> (l, b, r)
1324+
where
1325+
go :: Ord a => a -> Set a -> StrictTriple (Set a) Bool (Set a)
1326+
go _ Tip = StrictTriple Tip False Tip
1327+
go x (Bin _ y l r)
1328+
= case compare x y of
1329+
LT -> let StrictTriple lt found gt = go x l
1330+
!gt' = link y gt r
1331+
in StrictTriple lt found gt'
1332+
GT -> let StrictTriple lt found gt = go x r
1333+
!lt' = link y l lt
1334+
in StrictTriple lt' found gt
1335+
EQ -> StrictTriple l True r
13311336
#if __GLASGOW_HASKELL__
13321337
{-# INLINABLE splitMember #-}
13331338
#endif
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{-# LANGUAGE CPP #-}
2+
#if !defined(TESTING) && defined(__GLASGOW_HASKELL__)
3+
{-# LANGUAGE Safe #-}
4+
#endif
5+
6+
#include "containers.h"
7+
8+
-- | A strict triple
9+
10+
module Utils.Containers.Internal.StrictTriple (StrictTriple(..)) where
11+
12+
-- | The same as a regular Haskell tuple, but
13+
--
14+
-- @
15+
-- StrictTriple x y _|_ = StrictTriple x _|_ z = StrictTriple _|_ y z = _|_
16+
-- @
17+
data StrictTriple a b c = StrictTriple !a !b !c

0 commit comments

Comments
 (0)