简体   繁体   English

Google Apps 脚本 (GAS) 是否支持适当的尾调用或尾调用优化?

[英]Does Google Apps Script (GAS) support proper tail calls or tail call optimization?

I'm wondering whether investing time into doing tail call optimization for a Google Apps Script is worth it.我想知道是否值得花时间为 Google Apps 脚本进行尾调用优化

So, I've understood that Google Apps Script uses the ES2015 (ES6) version of the JavaScript specification ( ref1 , ref2 ), running it on the V8 runtime implementation .因此,我了解到 Google Apps 脚本使用 JavaScript 规范( ref1ref2 )的 ES2015 (ES6) 版本,在 V8 运行时实现上运行它。

Supposedly, ES2015 supports (proper) tail call optimization from the spec.据推测, ES2015 支持规范中的(适当的)尾调用优化。 But there are some indications that V8 actually doesn't implement it:但是有一些迹象表明 V8 实际上并没有实现它:

Furthermore, I've learned there is an important nuance here:此外,我了解到这里有一个重要的细微差别:

The terminology of proper tail calls (PTC) and tail call optimization (TCO) is often conflated.适当的尾调用 (PTC) 和尾调用优化 (TCO) 这两个术语经常混为一谈。 Here's the difference between the two:这是两者之间的区别:

  • proper tail calls: functions called in the tail position reuse the current stack frame, preventing the creation of additional stack frames that cause space inefficiency.适当的尾部调用:尾部调用的函数 position 重用当前堆栈帧,防止创建导致空间效率低下的额外堆栈帧。
  • tail call optimization: rewrites a recursive function into an iterative one, usually by calling goto.尾调用优化:将递归 function 重写为迭代,通常通过调用 goto。

PTC only deals with stack manipulation, while TCO rewrites a recursive function as an iterative function. PTC 只处理堆栈操作,而 TCO 将递归 function 重写为迭代 function。

So, given this...所以,鉴于此...

Does Google Apps Script (GAS): Google Apps 脚本 (GAS) 是否:

  • support proper tail calls?支持适当的尾调用? My guess from the above is "No.", but it'd be nice to have an authoritative answer, as far as possible.我从上面的猜测是“否”,但尽可能有一个权威的答案会很好。
  • support tail call optimization to the point where it is worth doing it, for performance, in any way?以任何方式支持尾调用优化到值得这样做的程度,以提高性能? My guess is "Yes, you can do it, but it doesn't improve performance."我的猜测是“是的,你可以做到,但它不会提高性能。” But it'd be nice if someone knew it definitively, or could point to a GAS performance comparison that demonstrates it.但是,如果有人确切地知道它,或者可以指出证明它的 GAS 性能比较,那就太好了。

(V8 developer here.) (这里是 V8 开发人员。)

Does Google Apps Script (GAS) support proper tail calls? Google Apps 脚本 (GAS) 是否支持正确的尾调用?

No. V8 does not support PTCs (nor STCs); No. V8 不支持 PTC(也不支持 STC); you've already linked to the background story yourself.你自己已经链接到背景故事了。 Since GAS is built on V8 for JS execution, it can't support them either.由于 GAS 是为 JS 执行而构建在 V8 上的,因此它也不支持它们。

wondering whether investing time into doing tail call optimization for a script is worth it.想知道花时间为脚本做尾调用优化是否值得。 / Does GAS support tail call optimization to the point where it is worth doing it, for performance, in any way? / GAS 是否以任何方式支持尾调用优化到值得为性能而做的程度?

I'm not sure what you mean: TCO is something that a compiler potentially does, not something you do.我不确定你的意思:TCO 是编译器可能做的事情,而不是你做的事情。

If you mean "moving calls into tail positions, so the compiler can turn them into iterations": No, V8 doesn't do that, so that's not worth your time.如果您的意思是“将调用移动到尾部位置,以便编译器可以将它们转换为迭代”:不,V8 不会那样做,因此不值得您花时间。 Whether a call occurs in a tail position or not makes no difference for performance.调用是否发生在尾部 position 中对性能没有影响。 (And I wouldn't bet on that changing any time soon.) (而且我不会打赌这种情况很快就会改变。)

If you mean "manually converting recursion into iteration": that may be worth doing for performance-critical code;如果您的意思是“手动将递归转换为迭代”:对于性能关键型代码可能值得这样做; whether it's worth doing in your particular case can only be answered by trying it and measuring the effect.在您的特定情况下是否值得做,只能通过尝试并衡量效果来回答。 (As always, a small artificial test aka "microbenchmark" is very likely to produce misleading results that don't apply to other situations.) (与往常一样,小型人工测试也称为“微基准测试”很可能会产生不适用于其他情况的误导性结果。)
This doesn't need any "support" from GAS or V8.这不需要 GAS 或 V8 的任何“支持”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM