Skip to content

Commit

Permalink
[#32] Use foldl' (+) 0 instead of sum
Browse files Browse the repository at this point in the history
`sum` is strict starting in GHC 9.0, for earlier GHCs it uses foldl
under the hood, which accumulates thunks. So to run well GHC 8.10, we
use `foldl' (+) 0)` instead of sum.
  • Loading branch information
pgujjula committed Dec 28, 2024
1 parent b1525be commit b62623a
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions bench/Bench/ApplyMerge.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import ApplyMerge.IntMap qualified
import ApplyMerge.IntSet qualified
import ApplyMerge.MergeAll qualified
import Data.Function ((&))
import Data.List (foldl')
import Data.List.Ordered (minus)
import Test.Tasty.Bench (Benchmark, bench, bgroup, nf)

Expand Down Expand Up @@ -55,7 +56,7 @@ benchmarks =

composites :: [Int]
composites = applyMerge (\p j -> p * (p + j)) primes [zero ..]
in sum (takeWhile (<= n) primes)
in foldl' (+) 0 (takeWhile (<= n) primes)
]

type ApplyMerge = forall a b c. (Ord c) => (a -> b -> c) -> [a] -> [b] -> [c]
Expand All @@ -68,7 +69,7 @@ funcToCollapse f applyMerge n =
let one = (n `quot` maxBound) + 1
in applyMerge f [one ..] [one ..]
& take n
& sum
& foldl' (+) 0

collapseToBenchmark :: String -> (ApplyMerge -> Int -> Int) -> Benchmark
collapseToBenchmark name collapse = bgroup name (map mkBench [1 .. 6])
Expand Down

0 comments on commit b62623a

Please sign in to comment.