Space leak in recursive accumulation with foldl
0 reputation · 07 May 2021, 09:42 UTC
Thunk accumulation in lazy evaluation
Haskell's default lazy evaluation defers computations by creating thunks. When using a recursive accumulation pattern, such as with foldl, these unevaluated expressions can build up in memory, leading to a space leak before the final result is demanded.
Strictness and WHNF
While foldl' and the BangPatterns extension are documented methods to force evaluation to Weak Head Normal Form (WHNF), there is uncertainty regarding the depth of this evaluation. Strictness annotations on data fields ensure the constructor is evaluated, but nested structures within those fields may still retain thunks.
Given GHC's strictness analyzer and varying optimization levels (e.g., -O2), the exact timing of evaluation and the resulting memory footprint can shift.
- Does the strictness analyzer consistently eliminate thunks in nested structures when using
BangPatternson a top-level accumulator? - How does the interaction between strictness strategies and the bottom value (
undefined) affect the point of failure in a strict fold?