Skip to content

Commit e66de6a

Browse files
committed
Introduce recursive worker to ‘splitMember’ to increase inlining chances
1 parent 2c1d08c commit e66de6a

File tree

5 files changed

+35
-12
lines changed

5 files changed

+35
-12
lines changed

containers-tests/containers-tests.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ library
128128
Utils.Containers.Internal.State
129129
Utils.Containers.Internal.StrictMaybe
130130
Utils.Containers.Internal.EqOrdUtil
131+
Utils.Containers.Internal.StrictTriple
131132

132133
if impl(ghc)
133134
other-modules:

containers/containers.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ Library
8383
Utils.Containers.Internal.PtrEquality
8484
Utils.Containers.Internal.Coercions
8585
Utils.Containers.Internal.EqOrdUtil
86+
Utils.Containers.Internal.StrictTriple
8687
if impl(ghc)
8788
other-modules:
8889
Utils.Containers.Internal.TypeError

containers/src/Data/Map/Internal.hs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,7 @@ import qualified Data.Set.Internal as Set
400400
import Data.Set.Internal (Set)
401401
import Utils.Containers.Internal.PtrEquality (ptrEq)
402402
import Utils.Containers.Internal.StrictPair
403+
import Utils.Containers.Internal.StrictTriple
403404
import Utils.Containers.Internal.StrictMaybe
404405
import Utils.Containers.Internal.BitQueue
405406
import Utils.Containers.Internal.EqOrdUtil (EqM(..), OrdM(..))
@@ -4048,8 +4049,6 @@ splitMember k0 m = case go k0 m of
40484049
{-# INLINABLE splitMember #-}
40494050
#endif
40504051

4051-
data StrictTriple a b c = StrictTriple !a !b !c
4052-
40534052
{--------------------------------------------------------------------
40544053
Utility functions that maintain the balance properties of the tree.
40554054
All constructors assume that all values in [l] < [k] and all values

containers/src/Data/Set/Internal.hs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ import Data.List.NonEmpty (NonEmpty(..))
256256
#endif
257257

258258
import Utils.Containers.Internal.StrictPair
259+
import Utils.Containers.Internal.StrictTriple
259260
import Utils.Containers.Internal.PtrEquality
260261
import Utils.Containers.Internal.EqOrdUtil (EqM(..), OrdM(..))
261262

@@ -1433,16 +1434,20 @@ splitS x (Bin _ y l r)
14331434
-- | \(O(\log n)\). Performs a 'split' but also returns whether the pivot
14341435
-- element was found in the original set.
14351436
splitMember :: Ord a => a -> Set a -> (Set a,Bool,Set a)
1436-
splitMember _ Tip = (Tip, False, Tip)
1437-
splitMember x (Bin _ y l r)
1438-
= case compare x y of
1439-
LT -> let (lt, found, gt) = splitMember x l
1440-
!gt' = link y gt r
1441-
in (lt, found, gt')
1442-
GT -> let (lt, found, gt) = splitMember x r
1443-
!lt' = link y l lt
1444-
in (lt', found, gt)
1445-
EQ -> (l, True, r)
1437+
splitMember k0 s = case go k0 s of
1438+
StrictTriple l b r -> (l, b, r)
1439+
where
1440+
go :: Ord a => a -> Set a -> StrictTriple (Set a) Bool (Set a)
1441+
go _ Tip = StrictTriple Tip False Tip
1442+
go x (Bin _ y l r)
1443+
= case compare x y of
1444+
LT -> let StrictTriple lt found gt = go x l
1445+
!gt' = link y gt r
1446+
in StrictTriple lt found gt'
1447+
GT -> let StrictTriple lt found gt = go x r
1448+
!lt' = link y l lt
1449+
in StrictTriple lt' found gt
1450+
EQ -> StrictTriple l True r
14461451
#if __GLASGOW_HASKELL__
14471452
{-# INLINABLE splitMember #-}
14481453
#endif
Lines changed: 17 additions & 0 deletions
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)