What is “Lambda Lifting”?

前端 未结 4 1666
时光取名叫无心
时光取名叫无心 2020-12-12 17:09

I just ran into this while going through the Erlang compiler source.

I\'m not really getting it. (go figure ;)), considering that I just realized that there is suc

4条回答
  •  长情又很酷
    2020-12-12 17:42

    Lambda lifting is used to turn a closure into a pure function. By passing extra arguments to the function, you reduce the number of its free variables. As you "lift" the lambda into higher and higher scopes, you add arguments to accommodate the local variables declared in that scope (which would be free variables otherwise). Once the lambda has no free variables it is a pure "top level" function.

    Of course you can only do this if you know all the lambda's call sites; in other words, only if the lambda does not escape.

    The benefit in a compiler optimizer is that closures (function environments) can be eliminated. This might make it possible to pass the arguments in registers rather than stack (or heap) allocating them as free variables.

提交回复
热议问题