尾調(diào)用優(yōu)化與函數(shù)內(nèi)聯(lián)

尾調(diào)用

某個函數(shù)的最后一步是調(diào)用另一個函數(shù)。

function f(x){
  return g(x) + 1;
}

由于調(diào)用后還有操作,即使寫在一行內(nèi),不屬于尾調(diào)用。

尾調(diào)用由于是函數(shù)的最后一步操作,所以不需要保留外層函數(shù)的調(diào)用記錄,因?yàn)檎{(diào)用位置、內(nèi)部變量等信息都不會再用到了,只要直接用內(nèi)層函數(shù)的調(diào)用記錄,取代外層函數(shù)的調(diào)用記錄就可以了。
比如:

function f() {
  let m = 1;
  let n = 2;
  return g(m + n);
}
f();

但由于調(diào)用g之后,函數(shù)f就結(jié)束了,所以執(zhí)行到最后一步,完全可以刪除 f() 的調(diào)用記錄,只保留 g(3) 的調(diào)用記錄。這就叫做"尾調(diào)用優(yōu)化"(Tail call optimization)

尾調(diào)用優(yōu)化(Tail Call Optimization)

TCO is only available in strict mode.

It provides a way to optimise recursive and deeply nested function calls by eliminating the need to push function state onto the global frame stack, and avoiding having to step down through each calling function by returning directly to the initial calling function.

TCO allows for recursive functions to have indefinite recursion as the frame stack will not grow with each recursive call. Without TCO recursive function had a limited recursive depth.

尾調(diào)用優(yōu)化舉例說明

function a(){
   return b(); // 2
} 
function b(){
   return 1;  // 3
}
a(); // 1

Without TCO the call to a() creates a new frame for that function. When that function calls b() the a()'s frame is pushed onto the frame stack and a new frame is created for function b()

When b() return to a() a()'s frame is popped from the frame stack. It immediately return to the global frame and thus does not use any of the states save on the stack.

TCO recognises that the call from a() to b() is at the tail of function a() and thus there is no need to push a()'s state onto the frame stack. When b(0) returns rather than returning to a() it returns directly to the global frame. Further optimising by eliminating the intermediate steps.

一種特殊的尾調(diào)用——尾遞歸

函數(shù)調(diào)用自身,稱為遞歸。如果尾調(diào)用自身,就稱為尾遞歸。

function factorial(n) {
  if (n === 1) return 1;
  return n * factorial(n - 1);
}

factorial(5) // 120

筆記

  1. 函數(shù)內(nèi)聯(lián)是指把被調(diào)用的函數(shù)體插入調(diào)用的函數(shù)當(dāng)中,就好像被調(diào)用的函數(shù)直接寫在調(diào)用的函數(shù)代碼中一樣,因此,能節(jié)省函數(shù)調(diào)用和返回的開銷(比如,寄存器的保存與恢復(fù))。
  2. Javascript Garden中談到arguments與函數(shù)內(nèi)聯(lián)的問題
function foo() {
    arguments.callee; // do something with this function object
    arguments.callee.caller; // and the calling function object
}
function bigLoop() {
    for(var i = 0; i < 100000; i++) {
        foo(); // Would normally be inlined...
    }
}

上面代碼中,foo 不再是一個單純的內(nèi)聯(lián)函數(shù) inlining譯者注:這里指的是解析器可以做內(nèi)聯(lián)處理), 因?yàn)樗枰浪约汉退恼{(diào)用者。 這不僅抵消了內(nèi)聯(lián)函數(shù)帶來的性能提升(內(nèi)聯(lián)函數(shù)帶來的性能提升見第一條),而且破壞了封裝,因此現(xiàn)在函數(shù)可能要依賴于特定的上下文。

  1. 解決禁用arguments.callee的辦法:
[1,2,3,4,5].map(function factorial(n) { 
    return (!(n>1))? 1 : factorial(n-1)*n; 
});
  1. arguments.callee.caller對解析器優(yōu)化有副作用

arguments.callee.caller gives you the function that called you, not the function you're in, which isn't overly useful in real code, and has the effect of making inlining, tail calls, etc unsound.

參考文獻(xiàn)

  1. 跨文件腳本內(nèi)聯(lián):函數(shù)內(nèi)聯(lián)使得性能提升
  2. Why was the arguments.callee.caller property deprecated in JavaScript?
  3. ECMAscript5 的 Strict Mode 中為什么禁止使用 arguments.callee?
  4. https://stackoverflow.com/documentation/javascript/2355/tail-call-optimization#t=201708230040512939622
  5. http://www.ruanyifeng.com/blog/2015/04/tail-call.html
  6. 深入理解遞歸中的尾調(diào)用
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容