[commit: ghc] master: Make last a good consumer (b709f0a)

git at git.haskell.org git at git.haskell.org
Tue Jul 22 09:21:09 UTC 2014


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

On branch  : master
Link       : http://ghc.haskell.org/trac/ghc/changeset/b709f0a047dc036de15dc43d3b0ab88d3e32c5e6/ghc

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

commit b709f0a047dc036de15dc43d3b0ab88d3e32c5e6
Author: Joachim Breitner <mail at joachim-breitner.de>
Date:   Tue Jul 22 11:21:01 2014 +0200

    Make last a good consumer
    
    Summary:
    Make last a good consumer simply by implementing it as foldl. This fixes Trac: #9339.
    Thanks to David Feuer for bringing it up.
    
    Test Plan: perf/should_run/T9339 + general validation
    
    Reviewers: austin
    
    Reviewed By: austin
    
    Subscribers: phaskell, simonmar, relrod, carter
    
    Differential Revision: https://phabricator.haskell.org/D86
    
    Trac Issues: #9339


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

b709f0a047dc036de15dc43d3b0ab88d3e32c5e6
 libraries/base/GHC/List.lhs                  | 7 ++-----
 testsuite/tests/perf/should_run/T9339.hs     | 4 ++++
 testsuite/tests/perf/should_run/T9339.stdout | 1 +
 testsuite/tests/perf/should_run/all.T        | 9 +++++++++
 4 files changed, 16 insertions(+), 5 deletions(-)

diff --git a/libraries/base/GHC/List.lhs b/libraries/base/GHC/List.lhs
index e004ded..9b6cc2e 100644
--- a/libraries/base/GHC/List.lhs
+++ b/libraries/base/GHC/List.lhs
@@ -83,11 +83,8 @@ last [x]                =  x
 last (_:xs)             =  last xs
 last []                 =  errorEmptyList "last"
 #else
--- eliminate repeated cases
-last []                 =  errorEmptyList "last"
-last (x:xs)             =  last' x xs
-  where last' y []     = y
-        last' _ (y:ys) = last' y ys
+-- use foldl to allow fusion
+last = foldl (\_ x -> x) (errorEmptyList "last")
 #endif
 
 -- | Return all the elements of a list except the last one.
diff --git a/testsuite/tests/perf/should_run/T9339.hs b/testsuite/tests/perf/should_run/T9339.hs
new file mode 100644
index 0000000..96f5f72
--- /dev/null
+++ b/testsuite/tests/perf/should_run/T9339.hs
@@ -0,0 +1,4 @@
+-- Tests that `last` successfully fuses.
+
+main :: IO ()
+main = print $ last $ filter odd $ [1::Int ..10000000]
diff --git a/testsuite/tests/perf/should_run/T9339.stdout b/testsuite/tests/perf/should_run/T9339.stdout
new file mode 100644
index 0000000..e161ae3
--- /dev/null
+++ b/testsuite/tests/perf/should_run/T9339.stdout
@@ -0,0 +1 @@
+9999999
diff --git a/testsuite/tests/perf/should_run/all.T b/testsuite/tests/perf/should_run/all.T
index a9d7c03..8b8547e 100644
--- a/testsuite/tests/perf/should_run/all.T
+++ b/testsuite/tests/perf/should_run/all.T
@@ -379,3 +379,12 @@ test('T9203',
       only_ways(['normal'])],
      compile_and_run,
      ['-O2'])
+
+test('T9339',
+     [stats_num_field('bytes allocated',
+                      [ (wordsize(64),    80050760, 5) ]),
+                      # w/o fusing last: 320005080
+                      # 2014-07-22:       80050760
+      only_ways(['normal'])],
+     compile_and_run,
+     ['-O2'])



More information about the ghc-commits mailing list