Faster async functions and promises
上一篇排序偷懒比较厉害,这篇相对来说比较重要,就重点全文翻译了。原文我仍旧留着,以作对照用。
Asynchronous processing in JavaScript traditionally had a reputation for not being particularly fast. To make matters worse, debugging live JavaScript applications — in particular Node.js servers — is no easy task, especially when it comes to async programming. Luckily the times, they are a-changin’. This article explores how we optimized async functions and promises in V8 (and to some extent in other JavaScript engines as well), and describes how we improved the debugging experience for async code.
JavaScript的异步处理不够快已经是名声在外了。更糟糕的是在JavaScript中对应用程序进行debug - 特别是Node.js服务器 - 很困难,特别是异步编程的情况。幸运的是,事情正在转变。这篇文章会探索我们在V8中是如何优化异步函数和promise的,并描述我们是如何提升异步代码的debugging体验的。
Before promises were part of the JavaScript language, callback-based APIs were commonly used for asynchronous code, especially in Node.js. Here’s an example:
在promise出现之前,基于callback的APIs是异步代码的实现选择,特别是在Node.js中。这里有一个例子:
function handler(done) {
validateParams((error) => {
if (error) return done(error);
dbQuery((error, dbResults) => {
if (error) return done(error);
serviceCall(dbResults, (error, serviceResults) => {
console.log(result);
done(error, serviceResults);
});
});
});
}
The specific pattern of using deeply-nested callbacks in this manner is commonly referred to as “callback hell”, because it makes the code less readable and hard to maintain.
这种深度嵌套的callback写法被称为”回调地狱”,因为这样的代码使得可读性和维护性都非常差。
Luckily, now that promises are part of the JavaScript language, the same code could be written in a more elegant and maintainable manner:
幸运的是,现在promise已经来了,同样的逻辑,代码可以写得更优雅更可维护:
function handler() {
return validateParams()
.then(dbQuery)
.then(serviceCall)
.then(result => {
console.log(result);
return result;
});
}
Even more recently, JavaScript gained support for async functions. The above asynchronous code can now be written in a way that looks very similar to synchronous code:
最近,JavaScript得到了async函数的支持。上述的异步操作代码现在可以写成类似同步的代码:
async function handler() {
await validateParams();
const dbResults = await dbQuery();
const results = await serviceCall(dbResults);
console.log(results);
return results;
}
With async functions, the code becomes more succinct, and the control and data flow are a lot easier to follow, despite the fact that the execution is still asynchronous. (Note that the JavaScript execution still happens in a single thread, meaning async functions don’t end up creating physical threads themselves.)
有了async函数的支持,代码变得更简洁,代码控制以及数据流更容易追踪,尽管事实上这些代码运行时仍旧是异步执行的。(请注意,JavaScript代码的执行仍旧是在单线程中,意味着async函数并未给它们自己创建物理上的线程。)
Another asynchronous paradigm that’s especially common in Node.js is that of ReadableStreams. Here’s an example:
另一个在Node.ReadableStreams。这里有个例子:
const http = require('http');
http.createServer((req, res) => {
let body = '';
req.setEncoding('utf8');
req.on('data', (chunk) => {
body += chunk;
});
req.on('end', () => {
res.write(body);
res.end();
});
}).listen(1337);
This code can be a little hard to follow: the incoming data is processed in chunks that are only accessible within callbacks, and the end-of-stream signaling happens inside a callback too. It’s easy to introduce bugs here when you don’t realize that the function terminates immediately and that the actual processing has to happen in the callbacks.
这段代码有点难以追踪:输入的数据仅仅只能在callback中访问到,并且end-of-stream信号也只能在另一个回调函数中得到通知。如果你并没有理解函数在运行时立马结束了,而真正的处理是放生在回调函数中的话,就很容易产生bug。
Fortunately, a cool new ES2018 feature called async iteration can simplify this code:
幸运的是,ES2018中引入了一个很cool的功能,称为 async iteration,能简化这段代码:
const http = require('http');
http.createServer(async (req, res) => {
try {
let body = '';
req.setEncoding('utf8');
for await (const chunk of req) {
body += chunk;
}
res.write(body);
res.end();
} catch {
res.statusCode = 500;
res.end();
}
}).listen(1337);
Instead of putting the logic that deals with the actual request processing into two different callbacks — the 'data'
and the 'end'
callback — we can now put everything into a single async function instead, and use the new for await…of
loop to iterate over the chunks asynchronously. We also added a try-catch block to avoid the unhandledRejection
problem.
不再将请求处理的逻辑放在两个不同的回调函数里 - 'data'
以及 'end'
回调 - 我们现在可以将所有逻辑放在一个async函数里处理即可,并且使用新的for await…of
循环来异步迭代chunks数据。我们还添加了一个try-catch代码块来防止unhandledRejection
问题。
You can already use these new features in production today! Async functions are fully supported starting with Node.js 8 (V8 v6.2 / Chrome 62), and async iterators and generators are fully supported starting with Node.js 10 (V8 v6.8 / Chrome 68)!
你已经可以在生产环境中使用这些新功能了!async函数从Node.js 8(V8 v6.2 / Chrome 62)就得到全面支持了,而async迭代以及generators则从Node.js 10(V8 v6.8 / Chrome 68)开始得到全面支持!
We’ve managed to improve the performance of asynchronous code significantly between V8 v5.5 (Chrome 55 & Node.js 7) and V8 v6.8 (Chrome 68 & Node.js 10). We reached a level of performance where developers can safely use these new programming paradigms without having to worry about speed.
我们已经在V8 v5.5(Chrome 55 & Node.js 7)以及V8 v6.8(Chrome 68 & Node.js 10)版本之间显著提升了异步代码的性能。现在的性能已经可以让开发者放心安全使用这些新的语法功能,而不用担心性能问题。
The above chart shows the doxbee benchmark, which measures performance of promise-heavy code. Note that the charts visualize execution time, meaning lower is better.
上图显示了doxbee benchmark,这个测试是用来衡量promise重度使用代码的性能。请注意,图表可视化了执行时长,这意味着越低越好。
The results on the parallel benchmark, which specifically stresses the performance of Promise.all(), are even more exciting:
下图显示了parallel benchmark,这个测试是用来压测Promise.all()的性能,很有趣:
We’ve managed to improve Promise.all
performance by a factor of 8×.
我们已经将Promise.all
的性能提升了8x。
However, the above benchmarks are synthetic micro-benchmarks. The V8 team is more interested in how our optimizations affect real-world performance of actual user code.
然而,上述benchmarks是人为设定的微型benchmarks。V8团队对我们的优化是如何影响真实场景用户代码性能更感兴趣。
The above chart visualizes the performance of some popular HTTP middleware frameworks that make heavy use of promises and async
functions. Note that this graph shows the number of requests/second, so unlike the previous charts, higher is better. The performance of these frameworks improved significantly between Node.js 7 (V8 v5.5) and Node.js 10 (V8 v6.8).
上图显示了一些重度使用promise和async
函数的流行HTTP中间件框架的性能表现。请注意这幅图表显示了 requests / second 的数量,所以不像之前的图表,现在是越高越好。这些框架的性能在Node.js 7(V8 v5.5)和Node.js 10(V8 v6.8)之间提升明显。
These performance improvements are the result of three key achievements:
这些性能提升来自于三项关键成就的结果:
TurboFan, the new optimizing compiler 🎉
Orinoco, the new garbage collector 🚛
a Node.js 8 bug causing await
to skip microticks 🐛
TurboFan,最新的优化编译器
Orinoco,新的GC垃圾回收器
一个导致await
跳过microticks的Node.js 8 bug
When we launched TurboFan in Node.js 8, that gave a huge performance boost across the board.
当我们在Node.js 8中上线TurboFan的时候,得到了一个超级巨大的全面性能提升。
We’ve also been working on a new garbage collector, called Orinoco, which moves garbage collection work off the main thread, and thus improves request processing significantly as well.
我们也正在制作一个新的GC垃圾回收器,被称为Orinoco,它将垃圾回收工作从主线程中剥离出来,也显著提升了请求处理的量。
And last but not least, there was a handy bug in Node.js 8 that caused await
to skip microticks in some cases, resulting in better performance. The bug started out as an unintended spec violation, but it later gave us the idea for an optimization. Let’s start by explaining the buggy behavior:
放在最后说,但不代表最不重要,在Node.js 8中有一个bug,导致了在某些情况下await
会跳过microticks,导致了更好的性能表现。这个bug来源于一个非自觉的spec违反,但它也给了我们一些优化的灵感。让我们从解释这个bug行为开始:
const p = Promise.resolve();
(async () => {
await p; console.log('after:await');
})();
p.then(() => console.log('tick:a'))
.then(() => console.log('tick:b'));
The above program creates a fulfilled promise p
, and await
s its result, but also chains two handlers onto it. In which order would you expect the console.log
calls to execute?
上述程序创建了一个被满足的promisep
,然后await
它的结果,但仍旧链式附加了两个处理函数到这个promise上。你觉得最后console.log
的调用顺序会如何?
Since p is fulfilled, you might expect it to print 'after:await'
first and then the 'tick'
s. In fact, that’s the behavior you’d get in Node.js 8:
因为 p 已经被满足了,你可能觉得应该会先打印'after:await'
,然后才是'tick'
。事实上,在Node.js 8中,确实是这个顺序:
Although this behavior seems intuitive, it’s not correct according to the specification. Node.js 10 implements the correct behavior, which is to first execute the chained handlers, and only afterwards continue with the async function.
虽然这个行为结果符合直觉,但其实按照spec来说,它是不正确的。Node.js 10实现了正确的行为,先执行被链式附加的两个处理函数,然后才会继续这个async函数。
This “correct behavior” is arguably not immediately obvious, and was actually surprising to JavaScript developers, so it deserves some explanation. Before we dive into the magical world of promises and async functions, let’s start with some of the foundations.
这个”正确行为”确实第一眼非常不直观,对于JavaScript程序员众来说可能有点让人吃惊,因此这里解释一下。在我们深入promise和async函数之前,让我们先来看下基础。
On a high level there are tasks and microtasks in JavaScript. Tasks handle events like I/O and timers, and execute one at a time. Microtasks implement deferred execution for async
/await
and promises, and execute at the end of each task. The microtask queue is always emptied before execution returns to the event loop.
就设计来说,JavaScript中存在task
和microtask
。Task负责处理类似I/O以及计时器等事件,且一次只执行一个。Microtask负责处理延后的promise以及async
/await
执行,且在每个task之后运行。Microtask队列会在返回事件循环之前被清空。
For more details, check out Jake Archibald’s explanation of tasks, microtasks, queues, and schedules in the browser. The task model in Node.js is very similar.
如果想要了解更多细节,请查看Jake Archibald的解释帖 tasks, microtasks, queues, and schedules in the browser。在Node.js中的Task模型也是类似的。
According to MDN, an async function is a function which operates asynchronously using an implicit promise to return its result. Async functions are intended to make asynchronous code look like synchronous code, hiding some of the complexity of the asynchronous processing from the developer.
根据MDN,一个async函数就是一个函数会被异步处理,且会使用一个隐式(implicit)Promise来返回结果。Async函数的初衷是使得异步代码看上去像同步代码一样,从开发者这里隐藏掉一部分异步处理的复杂性。
The simplest possible async function looks like this:
最简单的async函数看起来像这样:
async function computeAnswer() {
return 42;
}
When called it returns a promise, and you can get to its value like with any other promise.
当这个函数被调用的时候,它会返回一个promise,你就可以像处理其他promise一样得到它的返回值。
const p = computeAnswer();
// → Promise
p.then(console.log);
// prints 42 on the next turn
You only get to the value of this promise p
the next time microtasks are run. In other words, the above program is semantically equivalent to using Promise.resolve
with the value:
你会在下次microtask运行的时候得到这个promisep
的返回值。换句话说,上面的程序其实相当于使用Promise.resolve
来处理返回值:
function computeAnswer() {
return Promise.resolve(42);
}
The real power of async functions comes from await
expressions, which cause the function execution to pause until a promise is resolved, and resume after fulfillment. The value of await
is that of the fulfilled promise. Here’s an example showing what that means:
async函数的真正强大之处体现在await
表达式,它会将函数执行暂停在那里,直到promise得到resolve,并会在promise得到fulfillment之后继续执行。await
的结果是promise被满足之后的正确结果。下面有一个例子:
async function fetchStatus(url) {
const response = await fetch(url);
return response.status;
}
The execution of fetchStatus
gets suspended on the await
, and is later resumed when the fetch
promise fulfills. This is more or less equivalent to chaining a handler onto the promise returned from fetch
.
fetchStatus
函数的执行会在await
处暂停,并会在fetch
promise得到满足之后继续执行。这多多少少有点类似于将一个handler附加到fetch
返回的promise之上。
function fetchStatus(url) {
return fetch(url).then(response => response.status);
}
That handler contains the code following the await
in the async function.
这个handler含有在async函数中await
之后的代码功能。
Normally you’d pass a Promise
to await
, but you can actually wait on any arbitrary JavaScript value. If the value of the expression following the await
is not a promise, it’s converted to a promise. That means you can await 42
if you feel like doing that:
一般来说你需要将Promise
提供给await
,但实际上你可以在任何JavaScript值上进行等待。如果await
所接的表达式不是一个promise,它就会被转换成一个promise。这意味着你可以编写类似于await 42
这样的代码,只要你想:
async function foo() {
const v = await 42;
return v;
}
const p = foo();
// → Promise
p.then(console.log);
// prints `42` eventually
More interestingly, await
works with any “thenable”, i.e. any object with a then
method, even if it’s not a real promise. So you can implement funny things like an asynchronous sleep that measures the actual time spent sleeping:
更有趣的是,await
能和任何“thenable”协同工作,举例来说,任何带有then
方法的对象,即便它不是一个真正的promise。所以你可以实现很有趣的东西,比如说一个异步睡眠逻辑,并在这个睡眠中记录睡眠时长:
class Sleep {
constructor(timeout) {
this.timeout = timeout;
}
then(resolve, reject) {
const startTime = Date.now();
setTimeout(() => resolve(Date.now() - startTime),
this.timeout);
}
}
(async () => {
const actualTime = await new Sleep(1000);
console.log(actualTime);
})();
Let’s see what V8 does for await
under the hood, following the specification. Here’s a simple async function foo
:
让我们看看V8在台面下究竟针对await
做了什么,参照specification。这里有一个简单的async函数foo
:
async function foo(v) {
const w = await v;
return w;
}
When called, it wraps the parameter v into a promise and suspends execution of the async function until that promise is resolved. Once that happens, execution of the function resumes and w
gets assigned the value of the fulfilled promise. This value is then returned from the async function.
当被调用的时候,函数将参数v包装成一个promise,并将执行暂停在那里直到promise得到resolve。此时,函数的执行会恢复,且w
得到完成的promise提供的值作为赋值。这个值接下来会从async函数中得到返回。
await
under the hoodFirst of all, V8 marks this function as resumable, which means that execution can be suspended and later resumed (at await
points). Then it creates the so-called implicit_promise
, which is the promise that is returned when you invoke the async function, and that eventually resolves to the value produced by the async function.
首先,V8将这个函数标记成可恢复,这意味着这个函数的执行能被暂停且能后续被恢复(在await
代码点)。然后它会创建被称为隐式 promise
(implicit_promise
),就是调用async函数时被返回的promise,并最终被resolve成async函数处理完成之后的值。
Then comes the interesting bit: the actual await
. First the value passed to await
is wrapped into a promise. Then, handlers are attached to this wrapped promise to resume the function once the promise is fulfilled, and execution of the async function is suspended, returning the implicit_promise
to the caller. Once the promise
is fulfilled, execution of the async function is resumed with the value w
from the promise
, and the implicit_promise
is resolved with w.
然后就是比较有趣的部分了:真正的await
。首先被传递给await
的值被包装成一个promise。然后,handlers被附加到这个被包装的promise上用以在promise被满足之后恢复函数的执行,之后async函数的执行就被暂停下来,将隐式 promise
返回给调用者。一旦当promise
被满足,async函数的执行就会被恢复,带有值w
的promise
被返回,然后隐式 promise
将会被值w
resolve。
In a nutshell, the initial steps for await v
are:
概括来说,await v
的初始步骤如下:
v
— the value passed to await
— into a promise.implicit_promise
to the caller.await
的值v
包装成一个promise隐式 promise
返回给调用者Let’s go through the individual operations step by step. Assume that the thing that is being await
ed is already a promise, which was fulfilled with the value 42
. Then the engine creates a new promise
and resolves that with whatever’s being await
ed. This does deferred chaining of these promises on the next turn, expressed via what the specification calls a PromiseResolveThenableJob.
让我们将单独的操作一步步过一下。假设被await
暂停的东西已经是一个promise,并被值42
满足。然后引擎会创建一个新的promise
,并用无论是什么只要是被await
的东西来resolve。这个行为将会将这些promise链推迟到下一个回合执行,用specification中的话来表达的话,就是PromiseResolveThenableJob。
Then the engine creates another so-called throwaway
promise. It’s called throwaway because nothing is ever chained to it — it’s completely internal to the engine. This throwaway
promise is then chained onto the promise
, with appropriate handlers to resume the async function. This performPromiseThen
operation is essentially what Promise.prototype.then() does, behind the scenes. Finally, execution of the async function is suspended, and control returns to the caller.
接下来引擎就创建了一个被称为throwaway
的promise。它被称为throwaway是因为没有任何东西被链在它上面 - 它完全内置于引擎内。这个throwaway
promise接下来被链到之前的所说的附有handlers用来恢复async函数执行的promise
之上。这个performPromiseThen
操作,本质上就是Promise.prototype.then()在底层所做的事情。最终,async函数的执行被暂停,应用的控制被返回给调用者。