Haskell优化器对范围中的重复函数调用使用memoization吗? [英] Does Haskell optimizer utilize memoization for repeated function calls in a scope?

查看:140
本文介绍了Haskell优化器对范围中的重复函数调用使用memoization吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑这个函数:

f as = if length as > 100 then length as else 100

由于函数是纯的,所以长度显然是相同的两个调用。我的问题是Haskell优化器将上面的代码转换为下面的代码?

Since the function is pure it's obvious that the length will be the same in both calls. My question is does Haskell optimizer turn the code above into equivalent of the following?

f as = 
  let l = length as
  in if l > 100 then l else 100

如果是,那么哪个级别设置启用它?如果没有,那为什么?在这种情况下,内存浪费不能是此答案中解释的原因,因为引入的变量很快就会释放因为函数执行完成。

If it does, then which level setting enables it? If it doesn't, then why? In this scenario a memory waste can't be the reason as explained in this answer, because the introduced variable gets released as soon as the function execution is finished.

请注意,这不是

Please note that this is not a duplicate of this question because of the local scope, and thus it may get a radically different answer.

推荐答案

GHC现在一些默认情况下,CSE为,因为 -fcse 标志已启用。

GHC now does some CSE by default, as the -fcse flag is on.


默认情况下打开..启用公共子表达式消除
优化。如果你有一些
unsafePerformIO表达式,你不想让它们通用,那么关闭这个函数很有用。

On by default.. Enables the common-sub-expression elimination optimisation. Switching this off can be useful if you have some unsafePerformIO expressions that you don't want commoned-up.

,因为引入共享(因此存在空间泄漏)的问题,它是保守的
CSE通行证获得位更好虽然(和这个)。

However, it is conservative, due to the problems with introducing sharing (and thus space leaks). The CSE pass is getting a bit better though (and this).

最后,请注意有一个插件用于完整的CSE。

Finally, note there is a plugin for full CSE.

  • http://hackage.haskell.org/package/cse-ghc-plugin

如果您有可以从中受益的代码。

If you have code that could benefit from that.

这篇关于Haskell优化器对范围中的重复函数调用使用memoization吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆