closure-compiler
closure-compiler copied to clipboard
Async wrapper functions don't get inlined on advanced compilation
When building the following code: (appspot repro)
// ==ClosureCompiler==
// @compilation_level ADVANCED_OPTIMIZATIONS
// @output_file_name default.js
// @formatting pretty_print
// @language_in ES_NEXT
// @language_out ES_NEXT
// @debug true
// ==/ClosureCompiler==
//async functions
async function waitLog(message){
console.log(message);
}
async function wrapperFnWait(message){
await waitLog(message);
}
//synchronous functions
function directLog(message){
console.log(message);
}
function wrapperFnDirect(message){
directLog(message);
}
//main function
async function main(){
await wrapperFnWait("hello");
wrapperFnDirect("hello2");
}
main();
This is the result:
'use strict';
async function $waitLog$$() {
console.log("hello");
}
async function $wrapperFnWait$$() {
await $waitLog$$();
}
(async function() {
await $wrapperFnWait$$();
console.log("hello2");
})();
The wrapperFnDirect()
and directLog()
functions get completely inlined so it simply becomes console.log("hello2");
.
But wrapperFnWait()
and waitLog()
leave behind a lot of code that seems like it could be reduced further.
Ack, I can reproduce this. We'll discuss in our bug meeting.
Note that await
has side effects, even when called on an immediate value. I believe it de-schedules the current micro-thread, which can have very subtle, but observable behaviour changes. Which is to say, this might be hard to do better without introducing risky assumptions.
Ah yeah, I was worried that would be the case. I couldn't think of any situation where this would be an issue but I'm sure there are a few.