Javascript引擎中的尾部调用优化实现 [英] Tail Call Optimization implementation in Javascript Engines

查看:93
本文介绍了Javascript引擎中的尾部调用优化实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

截至2019年2月在Mac
上的Chrome版本 71.0.3578.98 中,以下程序抛出 Uncaught RangeError:最大调用堆栈大小超出错误。计数为 16516

As of February 2019 in Chrome Version 71.0.3578.98 on Mac , the following program throws Uncaught RangeError: Maximum call stack size exceeded error. at a count of 16516.

const a = x => {
  console.log(x)
  a(x + 1)
}

a(1)

我已经做了很多Google搜寻工作,但找不到任何讨论Chrome或其他浏览器支持Tail Call Optimization(TCO)或任何未来的文章。计划实施。

I've done quite a bit of Googling, but wasn't able to find any articles discussing Chrome or other browser support for Tail Call Optimization (TCO) or any future plans to implement it.

我的两个问题是:


  1. Chrome或任何其他操作系统当前是否支持TCO其他浏览器或Javascript引擎

  2. 是否计划在不久的将来在任何Javascript引擎中实施TCO

我发现的帖子大多是过时的(2016年或更早)或只是令人困惑。例如 https://www.chromestatus.com/feature/5516876633341952

The posts that I've found are mostly old (2016 or earlier) or simply confusing. e.g. https://www.chromestatus.com/feature/5516876633341952

推荐答案

TCO,或更确切地说,是JavaScript中的尾部调用消除-在讨论中通常也称为正确的尾部调用(PTC)-

TCO, or rather, Tail Call Elimination in JavaScript -- also often referred to as Proper Tail Calls (PTC) in discussions -- is a long and sad story.

2011年左右,TC39(JavaScript标准委员会)决定在即将面世的ES6标准中采用强制性TCE,并得到所有主流浏览器的一致同意

Around 2011, TC39 (the JavaScript standards committee) decided to adopt mandatory TCE for the forthcoming ES6 standard, with consensus from all major browser vendors.

在2015年,新标准正式被采用,名称为EcmaScript2015。这时,实际上没有任何浏览器实现过TCE,这主要是因为太多了ES2015中被认为更重要的新功能。 (今天的JS功能提案及其采用过程(包括在生产引擎中实现两种实现的要求,对于ES6尚不存在。)

In 2015, the new standard was officially adopted, under the name EcmaScript 2015. At this point, no browser had actually implemented TCE, mostly because there were too many new features in ES2015 that were deemed more important to get out. (Today's process for JS feature proposals and their adoption, which includes the requirement of two implementations in production engines, did not yet exist for ES6.)

在2016年初,两者Safari和Chrome实施了TCE。 Safari宣布发货,而Chrome则将其保留为实验功能标志。其他浏览器(Firefox和Internet Explorer / Edge)也开始对其进行调查,并产生了新的想法。讨论最终演变为这是否是一个可行的功能。 Edge在为Windows ABI有效地实现它方面遇到问题,Firefox担心开发人员从堆栈跟踪中调用丢失的经验(这一问题已在2011年进行了详细讨论)。

In early 2016, both Safari and Chrome implemented TCE. Safari announced shipping it, while Chrome kept it behind an Experimental Feature flag. Other browsers (Firefox and Internet Explorer / Edge) started looking into it as well and had second thoughts. Discussion evolved whether this is a viable feature after all. Edge had problems implementing it efficiently for the Windows ABI, Firefox was concerned about the developer experience of calls "missing" from stack traces (an issue that was already discussed at length in 2011).

为了解决这些问题,同时挽救了电话尾部功能,包括Chrome和Edge团队在内的数名成员建议进行电话尾部显式,即要求返回语句附加关键字以加入尾注语义。这些所谓的 语法尾调用(STC)已在Chrome中实现,以证明

In an attempt to address some of these concerns while rescuing the tail call feature, several members, including the Chrome and Edge teams, proposed to make tail calls explicit, i.e., require return statements to be annotated with an additional keyword to opt into tail call semantics. These so-called "syntactic tail calls" (STC) were implemented in Chrome as a proof of concept.

在2016年5月的TC39会议上,几乎一天没有解决任何问题,就尾叫问题进行了广泛讨论。 Firefox和Edge明确表示,它们将不执行标准中指定的TCE。 Firefox成员建议将其取出。 Safari和Chrome对此并不认同,Safari团队明确表示他们无意取消TCE。语法尾部调用的提议也被拒绝,尤其是Safari。委员会陷入僵局。您可以阅读会议讨论记录

At the May 2016 TC39 meeting the issue of tail calls was discussed extensively for almost an entire day with no resolution. Firefox and Edge made clear that they would not implement TCE as specified in the standard. Firefox members proposed to take it out. Safari and Chrome did not agree with that, and the Safari team made clear that they have no intention of unshipping TCE. The proposal for syntactic tail calls was rejected as well, especially by Safari. The committee was in an impasse. You can read the meeting notes of this discussion.

从技术上讲,据我所知,这种僵局今天仍然存在。不过,实际上,对JavaScript的尾调用几乎已经死了,目前尚不清楚它们是否会回来。至少这是灾难性会议后Chrome小组的结论,该决定导致决定从Chrome删除尾部调用的实现,以简化引擎并防止钻头腐烂。仍然可以在Safari中使用。

Technically, this impasse still exists today, as far as I am aware. Practically speaking, though, tail calls for JavaScript are pretty much dead, and it's unclear whether they will ever come back. At least that was the conclusion of the Chrome team after the disastrous meeting, which led to the decision to remove the implementation of tail calls from Chrome, in order to simplify the engine and prevent bit rot. They are still available in Safari.

披露:直到2017年,我还是TC39和Chrome / V8团队的成员,所以我的看法可能会有所偏颇。

Disclosure: I was a member of TC39 and of the Chrome/V8 team until 2017, so my views may be biased.

这篇关于Javascript引擎中的尾部调用优化实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆