Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why did making Haskell lazy have an impact on performance?

In this video(Escape from the Ivory Tower - The Haskell Journey), Simon Peyton Jones says that making Haskell Lazy helped them with resource-constraints on the machines they had at the time. It also led to a whole lot of other benefits with laziness.

Then he said that the challenge they have now is that laziness impacts on performance.

My question is: Why did making Haskell lazy have an impact on performance?

like image 373
hawkeye Avatar asked Jun 20 '16 03:06

hawkeye


1 Answers

If you're not going to use the result of something, then storing it lazily and then never executing it is more efficient than uselessly executing it. That much is immediately obvious.

However, if you are going to execute it, then storing it lazily and then executing it later is less efficient than just executing it now. There's more indirection involved. It takes time to note down all the details you need for the execution, and it takes time to load them all back when you realise you actually need to execute.

This is particularly the case with something like adding two machine-width integers. If your operands are already in CPU registers, then adding them immediately is a single machine instruction. Instead, we laboriously put all that stuff onto the heap, and then fetch it back later (quite possibly with a bunch of cache misses and pipeline stalls).

On top of that, sometimes a computation isn't all that expensive, and produces a small result, but the details we need to store to run the computation later are quite large. The canonical example is totaling up a list. The result might be a single 32-bit integer, but the list to be totaled could be huge! All that extra work for the garbage collector to manage this data that might otherwise be dead objects that could be deallocated.

In general, laziness used right can result in massive performance gains, but laziness used wrong results in appalling performance disasters. And laziness can be very tricky to reason about; this stuff isn't easy. With experience, you do gradually get used to it though.

like image 138
MathematicalOrchid Avatar answered Oct 15 '22 03:10

MathematicalOrchid