Strict vs Lazy Lists: Which Reduces Peak Memory for Large Collections?
0 reputation · 22 Jul 2023, 15:50 UTC
In a data‑processing pipeline that builds large collections, the choice between strict list construction (using bang patterns) and the default lazy evaluation directly impacts memory consumption and CPU overhead. The goal is to decide whether to enforce strictness on each element to reduce heap churn or to retain laziness to keep immediate CPU work low.
The constraints are that the list may contain tens of millions of elements, the application runs on a long‑lived server, and the compiler flags are set to -O2. Profiling data for the current implementation shows high peak memory but acceptable CPU time. However, it is unclear whether the memory spikes are due to retained thunks or to the cost of building the list.
The unresolved decision is whether strictness annotations will lower peak memory without introducing significant CPU overhead. We need evidence from profiling and a clear trade‑off analysis.
1. How much does a bang pattern on the list constructor reduce peak memory for a 10 million element list at -O2?
2. What is the measurable increase in CPU time when strictness is applied?
3. Does the GHC optimizer fuse the strict constructor with downstream folds, mitigating the CPU cost?