简体   繁体   English

为什么让Haskell懒惰对性能产生影响?

[英]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. 这段视频中 (逃离象牙塔 - 哈斯克尔之旅​​), Simon Peyton Jones说,让Haskell Lazy帮助他们对当时的机器进行资源限制。 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? 我的问题是: 为什么让Haskell懒惰对性能产生影响?

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. 如果操作数已经在CPU寄存器中,那么立即添加它们就是一条机器指令。 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 ! 结果可能是一个32位整数,但总计的列表可能很大 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. 有了经验,你会逐渐习惯它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM