[commit: ghc] wip/oneShot: Use oneShot in the definition of foldl etc. (12530cb)
git at git.haskell.org
git at git.haskell.org
Tue Oct 28 22:44:53 UTC 2014
Repository : ssh://git@git.haskell.org/ghc
On branch : wip/oneShot
Link : http://ghc.haskell.org/trac/ghc/changeset/12530cb05dc75bbdf79b9ac51442eb41f9d8b0fa/ghc
>---------------------------------------------------------------
commit 12530cb05dc75bbdf79b9ac51442eb41f9d8b0fa
Author: Joachim Breitner <mail at joachim-breitner.de>
Date: Sat Oct 25 12:27:06 2014 +0200
Use oneShot in the definition of foldl etc.
This increases the chance of good code after fusing a left fold. See
ticket #7994 and the new Note [Left folds via right fold]
>---------------------------------------------------------------
12530cb05dc75bbdf79b9ac51442eb41f9d8b0fa
libraries/base/Data/OldList.hs | 4 +++-
libraries/base/GHC/List.lhs | 33 ++++++++++++++++++++++-----------
2 files changed, 25 insertions(+), 12 deletions(-)
diff --git a/libraries/base/Data/OldList.hs b/libraries/base/Data/OldList.hs
index 00bc660..78b5fe1 100644
--- a/libraries/base/Data/OldList.hs
+++ b/libraries/base/Data/OldList.hs
@@ -522,9 +522,11 @@ pairWithNil x = (x, [])
mapAccumLF :: (acc -> x -> (acc, y)) -> x -> (acc -> (acc, [y])) -> acc -> (acc, [y])
{-# INLINE [0] mapAccumLF #-}
-mapAccumLF f = \x r s -> let (s', y) = f s x
+mapAccumLF f = \x r -> oneShot $ \s ->
+ let (s', y) = f s x
(s'', ys) = r s'
in (s'', y:ys)
+ -- See Note [Left folds via right fold]
-- | The 'mapAccumR' function behaves like a combination of 'map' and
diff --git a/libraries/base/GHC/List.lhs b/libraries/base/GHC/List.lhs
index 52fab6f..5632d5c 100644
--- a/libraries/base/GHC/List.lhs
+++ b/libraries/base/GHC/List.lhs
@@ -186,10 +186,22 @@ filterFB c p x r | p x = x `c` r
foldl :: forall a b. (b -> a -> b) -> b -> [a] -> b
{-# INLINE foldl #-}
foldl k z0 xs =
- foldr (\(v::a) (fn::b->b) (z::b) -> fn (k z v)) (id :: b -> b) xs z0
--- Implementing foldl via foldr is only a good idea if the compiler can optimize
--- the resulting code (eta-expand the recursive "go"), so this needs
--- -fcall-arity! Also see #7994.
+ foldr (\(v::a) (fn::b->b) -> oneShot (\(z::b) -> fn (k z v))) (id :: b -> b) xs z0
+ -- See Note [Left folds via right fold]
+
+{-
+Note [Left folds via right fold]
+
+Implementing foldl et. al. via foldr is only a good idea if the compiler can
+optimize the resulting code (eta-expand the recursive "go"). See #7994.
+We hope that one of the two measure kick in:
+
+ * Call Arity (-call-ary, enabled by default) eta-expands it if it can see
+ all calls and determine that the arity is large.
+ * The oneShot annotation gives a hint to the regular arity analysis that
+ it may assume that the lambda is called at most once.
+ See [One-shot lambdas] and especially [Eta expanding thunks]
+-}
-- ----------------------------------------------------------------------------
@@ -197,11 +209,8 @@ foldl k z0 xs =
foldl' :: forall a b . (b -> a -> b) -> b -> [a] -> b
{-# INLINE foldl' #-}
foldl' k z0 xs =
- foldr (\(v::a) (fn::b->b) (z::b) -> z `seq` fn (k z v)) (id :: b -> b) xs z0
-
--- Implementing foldl' via foldr is only a good idea if the compiler can
--- optimize the resulting code (eta-expand the recursive "go"), so this needs
--- -fcall-arity! Also see #7994
+ foldr (\(v::a) (fn::b->b) -> oneShot (\(z::b) -> z `seq` fn (k z v))) (id :: b -> b) xs z0
+ -- See Note [Left folds via right fold]
-- | 'foldl1' is a variant of 'foldl' that has no starting value argument,
-- and thus must be applied to non-empty lists.
@@ -257,7 +266,8 @@ scanl = scanlGo
{-# INLINE [0] scanlFB #-}
scanlFB :: (b -> a -> b) -> (b -> c -> c) -> a -> (b -> c) -> b -> c
-scanlFB f c = \b g x -> let b' = f x b in b' `c` g b'
+scanlFB f c = \b g -> oneShot (\x -> let b' = f x b in b' `c` g b')
+ -- See Note [Left folds via right fold]
{-# INLINE [0] constScanl #-}
constScanl :: a -> b -> a
@@ -294,7 +304,8 @@ scanl' = scanlGo'
{-# INLINE [0] scanlFB' #-}
scanlFB' :: (b -> a -> b) -> (b -> c -> c) -> a -> (b -> c) -> b -> c
-scanlFB' f c = \b g x -> let b' = f x b in b' `seq` b' `c` g b'
+scanlFB' f c = \b g -> oneShot (\x -> let b' = f x b in b' `seq` b' `c` g b')
+ -- See Note [Left folds via right fold]
{-# INLINE [0] flipSeqScanl' #-}
flipSeqScanl' :: a -> b -> a
More information about the ghc-commits
mailing list