closure-compiler icon indicating copy to clipboard operation
closure-compiler copied to clipboard

Async wrapper functions don't get inlined on advanced compilation

Open jespertheend opened this issue 3 years ago • 2 comments

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.

jespertheend avatar May 22 '21 23:05 jespertheend

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.

mprobst avatar May 25 '21 13:05 mprobst

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.

jespertheend avatar May 25 '21 13:05 jespertheend