summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoachim Breitner <mail@joachim-breitner.de>2014-07-22 10:37:56 +0200
committerJoachim Breitner <mail@joachim-breitner.de>2014-07-22 10:37:56 +0200
commit1c126cfb4a9067d4ac5987d9371f6920328f6f29 (patch)
tree5492f65fe27a1c48443263d7b945fa297c0cfa0a
parent7aabfa6292c2469cf3250e006869273fb1b356ce (diff)
downloadhaskell-wip/T9339.tar.gz
Make last a good consumerwip/T9339
simply by implementing it as foldl. This fixes #9339. Thanks to David Feuer for bringing it up.
-rw-r--r--libraries/base/GHC/List.lhs7
-rw-r--r--testsuite/tests/perf/should_run/T9339.hs4
-rw-r--r--testsuite/tests/perf/should_run/T9339.stdout1
-rw-r--r--testsuite/tests/perf/should_run/all.T9
4 files changed, 16 insertions, 5 deletions
diff --git a/libraries/base/GHC/List.lhs b/libraries/base/GHC/List.lhs
index e004ded1e8..9b6cc2eb19 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 0000000000..96f5f7201a
--- /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 0000000000..e161ae3694
--- /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 a9d7c0325d..924f7f15b8 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'])