Skip to content

Optimisation puzzle with list of list folds and mconcat

As observed in !4890 (comment 326560) optimisation of some list of list folds is inconsistent across superficially similar forms. Specifically:

print $ foldl' (+) 0 $ mconcat $ map (\i -> [0..i]) [0..n]

performs noticeably better (half the allocations and a just over half the runtime of:

print $ foldl' (+) 0 $ foldr (++) [] $ map (\i -> [0..i]) [0..n]

@simonpj notes differences in the rules that fired in the two cases: !4890 (comment 326990)

Some more comments on this subtopic in:

This topic is a tangent from the original concerns of !4890 (closed) and so is best explored and tracked separately.

Edited by vdukhovni
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information