[commit: ghc] wip/T10830: Make Data.List.foldr1 inline (6066e1f)

git at git.haskell.org git at git.haskell.org
Wed Sep 2 23:50:05 UTC 2015


Repository : ssh://git@git.haskell.org/ghc

On branch  : wip/T10830
Link       : http://ghc.haskell.org/trac/ghc/changeset/6066e1fa6770f2cae3114ab4d4dbde04176863cb/ghc

>---------------------------------------------------------------

commit 6066e1fa6770f2cae3114ab4d4dbde04176863cb
Author: Joachim Breitner <mail at joachim-breitner.de>
Date:   Wed Sep 2 15:58:38 2015 -0700

    Make Data.List.foldr1 inline
    
    Previously, foldr1 would be defiend recursively and thus not inline.
    This is bad, for example, when maximumBy has a strict comparison
    function: Before the BBP, it was implemented via foldl1, which inlined
    and yielded good code. With BBP, it goes via foldr1, so we better inline
    this as well. Fixes #10830.


>---------------------------------------------------------------

6066e1fa6770f2cae3114ab4d4dbde04176863cb
 libraries/base/GHC/List.hs                     | 8 +++++---
 testsuite/tests/simplCore/should_run/T10830.hs | 3 +++
 testsuite/tests/simplCore/should_run/all.T     | 1 +
 3 files changed, 9 insertions(+), 3 deletions(-)

diff --git a/libraries/base/GHC/List.hs b/libraries/base/GHC/List.hs
index fcc89d3..ca3fb75 100644
--- a/libraries/base/GHC/List.hs
+++ b/libraries/base/GHC/List.hs
@@ -355,9 +355,11 @@ match on everything past the :, which is just the tail of scanl.
 -- and thus must be applied to non-empty lists.
 
 foldr1                  :: (a -> a -> a) -> [a] -> a
-foldr1 _ [x]            =  x
-foldr1 f (x:xs)         =  f x (foldr1 f xs)
-foldr1 _ []             =  errorEmptyList "foldr1"
+foldr1 f = go
+  where go [x]            =  x
+        go (x:xs)         =  f x (go xs)
+        go []             =  errorEmptyList "foldr1"
+{-# INLINE [0] foldr1 #-}
 
 -- | 'scanr' is the right-to-left dual of 'scanl'.
 -- Note that
diff --git a/testsuite/tests/simplCore/should_run/T10830.hs b/testsuite/tests/simplCore/should_run/T10830.hs
new file mode 100644
index 0000000..354f0f5
--- /dev/null
+++ b/testsuite/tests/simplCore/should_run/T10830.hs
@@ -0,0 +1,3 @@
+import GHC.OldList
+main :: IO ()
+main = maximumBy compare [1..10000] `seq` return ()
diff --git a/testsuite/tests/simplCore/should_run/all.T b/testsuite/tests/simplCore/should_run/all.T
index 364dfd6..ba775b7 100644
--- a/testsuite/tests/simplCore/should_run/all.T
+++ b/testsuite/tests/simplCore/should_run/all.T
@@ -69,3 +69,4 @@ test('T457', [ only_ways(['normal','optasm']), exit_code(1) ], compile_and_run,
 
 test('T9128', normal, compile_and_run, [''])
 test('T9390', normal, compile_and_run, [''])
+test('T10830', extra_run_opts('+RTS -K100k -RTS'), compile_and_run, [''])



More information about the ghc-commits mailing list