Skip to content

Commit 7e6d75f

Browse files
committed
Specialize splitTraverse; strictify pair splitting
Explicitly specialize `splitTraverse` functions to the necessary types. This has no immediate performance impact, but makes it clearer what the functions are about. Make splitting pairs a bit stricter; we don't need that much laziness.
1 parent cdf173f commit 7e6d75f

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

Data/Sequence.hs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1726,14 +1726,18 @@ instance Splittable (Seq a) where
17261726
splitState = splitAt
17271727

17281728
instance (Splittable a, Splittable b) => Splittable (a, b) where
1729-
splitState i (a, b) = ((al, bl), (ar, br))
1729+
splitState i (a, b) = (al `seq` bl `seq` (al, bl), ar `seq` br `seq` (ar, br))
17301730
where
17311731
(al, ar) = splitState i a
17321732
(bl, br) = splitState i b
17331733

1734+
{-# SPECIALIZE splitTraverseSeq :: (Seq x -> a -> b) -> Seq x -> Seq a -> Seq b #-}
1735+
{-# SPECIALIZE splitTraverseSeq :: ((Seq x, Seq y) -> a -> b) -> (Seq x, Seq y) -> Seq a -> Seq b #-}
17341736
splitTraverseSeq :: (Splittable s) => (s -> a -> b) -> s -> Seq a -> Seq b
17351737
splitTraverseSeq f s (Seq xs) = Seq $ splitTraverseTree (\s' (Elem a) -> Elem (f s' a)) s xs
17361738

1739+
{-# SPECIALIZE splitTraverseTree :: (Seq x -> Elem y -> b) -> Seq x -> FingerTree (Elem y) -> FingerTree b #-}
1740+
{-# SPECIALIZE splitTraverseTree :: (Seq x -> Node y -> b) -> Seq x -> FingerTree (Node y) -> FingerTree b #-}
17371741
splitTraverseTree :: (Sized a, Splittable s) => (s -> a -> b) -> s -> FingerTree a -> FingerTree b
17381742
splitTraverseTree _f _s Empty = Empty
17391743
splitTraverseTree f s (Single xs) = Single $ f s xs
@@ -1742,6 +1746,8 @@ splitTraverseTree f s (Deep n pr m sf) = Deep n (splitTraverseDigit f prs pr) (s
17421746
(prs, r) = splitState (size pr) s
17431747
(ms, sfs) = splitState (n - size pr - size sf) r
17441748

1749+
{-# SPECIALIZE splitTraverseDigit :: (Seq x -> Elem y -> b) -> Seq x -> Digit (Elem y) -> Digit b #-}
1750+
{-# SPECIALIZE splitTraverseDigit :: (Seq x -> Node y -> b) -> Seq x -> Digit (Node y) -> Digit b #-}
17451751
splitTraverseDigit :: (Sized a, Splittable s) => (s -> a -> b) -> s -> Digit a -> Digit b
17461752
splitTraverseDigit f s (One a) = One (f s a)
17471753
splitTraverseDigit f s (Two a b) = Two (f first a) (f second b)
@@ -1757,6 +1763,8 @@ splitTraverseDigit f s (Four a b c d) = Four (f first a) (f second b) (f third c
17571763
(middle, fourth) = splitState (size b + size c) s'
17581764
(second, third) = splitState (size b) middle
17591765

1766+
{-# SPECIALIZE splitTraverseNode :: (Seq x -> Elem y -> b) -> Seq x -> Node (Elem y) -> Node b #-}
1767+
{-# SPECIALIZE splitTraverseNode :: (Seq x -> Node y -> b) -> Seq x -> Node (Node y) -> Node b #-}
17601768
splitTraverseNode :: (Sized a, Splittable s) => (s -> a -> b) -> s -> Node a -> Node b
17611769
splitTraverseNode f s (Node2 ns a b) = Node2 ns (f first a) (f second b)
17621770
where

0 commit comments

Comments
 (0)