尾調(diào)用
某個(gè)函數(shù)的最后一步是調(diào)用另一個(gè)函數(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)部變量等信息都不會(huì)再用到了宣肚,只要直接用內(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
筆記
- 函數(shù)內(nèi)聯(lián)是指把被調(diào)用的函數(shù)體插入調(diào)用的函數(shù)當(dāng)中错洁,就好像被調(diào)用的函數(shù)直接寫在調(diào)用的函數(shù)代碼中一樣,因此戒突,能節(jié)省函數(shù)調(diào)用和返回的開銷(比如屯碴,寄存器的保存與恢復(fù))。
- 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 不再是一個(gè)單純的內(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ù)可能要依賴于特定的上下文。
- 解決禁用arguments.callee的辦法:
[1,2,3,4,5].map(function factorial(n) {
return (!(n>1))? 1 : factorial(n-1)*n;
});
- arguments.callee.caller對(duì)解析器優(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)
- 跨文件腳本內(nèi)聯(lián):函數(shù)內(nèi)聯(lián)使得性能提升
- Why was the arguments.callee.caller property deprecated in JavaScript?
- ECMAscript5 的 Strict Mode 中為什么禁止使用 arguments.callee仍稀?
- https://stackoverflow.com/documentation/javascript/2355/tail-call-optimization#t=201708230040512939622
- http://www.ruanyifeng.com/blog/2015/04/tail-call.html
- 深入理解遞歸中的尾調(diào)用