[Haskell-cafe] Efficiency of bang patterns
chad.scherrer at gmail.com
Tue Nov 7 13:15:12 EST 2006
I'm curious about the implementation of bang patterns, and the
implications for performance. Previously on this list, Lemmih has
pointed out that throwing in an extra `seq` here and there to force
strictness is a bad idea, unless you do it very carefully. He points
out that the strictness analyzer will catch a lot of cases without a
need for seq.
But the approach of compiling without any `seq`s, looking for leaks,
and then adding them in one at a time seems tedious. There should be a
more predictable, more uniform way of achieving strictness.
Is it reasonable to promote a programming style where strictness is
achieved using strictness annotations and bang patterns? I find it
very appealing that the "!" syntax translates so nicely from type
declarations to patterns. I had originally thought that every bang
pattern was translated into a seq call, as a sort of preprocessing
step, but from this page...
... is is clear that in many cases a bang pattern can be rewritten as
a case statement, which seems to me an opportunity to avoid some code
ugliness. In cases semantically distinct from a case statement, maybe
something like the following could be done, or maybe is already...
1. Pass the expression to the strictness analyzer, without the bang pattern.
2. If it's already strict, great! We're done.
3. If not, add an extra seq call.
Now, I'm no compiler expert (obviously, I suspect), and maybe I've
misunderstood the role of the strictness analysis step. But being able
to easily make things really strict seems pretty important, and there
seem to be a lot of subtleties to using seq that make it difficult to
tune for performance.
"Time flies like an arrow; fruit flies like a banana" -- Groucho Marx
More information about the Haskell-Cafe