Does Haskell optimizer utilize memoization for repeated function calls in a scope?

后端 未结 3 1833
醉梦人生
醉梦人生 2021-01-17 12:04

Consider this function:

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

Since the function is pure it\'s obvious that the length will

3条回答
  •  野的像风
    2021-01-17 12:31

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

    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.

    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).

    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.

提交回复
热议问题