google / closure-compiler

A JavaScript checker and optimizer.
https://developers.google.com/closure/compiler/
Apache License 2.0
7.41k stars 1.15k forks source link

Missed optimization: document.querySelector('') turns into var b=document;b.querySelector.call(b, '') #4191

Open juj opened 2 months ago

juj commented 2 months ago

Input JS:

var buffer = new ArrayBuffer(1024);
var HEAPU8 = new Uint8Array(buffer);
var UTF8Decoder = new TextDecoder();

var UTF8ToString = (ptr, maxBytesToRead) => {
  if (!ptr) return '';
  var maxPtr = ptr + maxBytesToRead;
  for (var end = ptr; !(end >= maxPtr) && HEAPU8[end];) ++end;
  return UTF8Decoder.decode(HEAPU8.subarray(ptr, end));
};

function wgpu_canvas_get_webgpu_context(canvasSelector) {
  let canvas = document.querySelector(UTF8ToString(canvasSelector));
  let ctx = canvas.getContext('webgpu');
  return ctx;
}

wgpu_canvas_get_webgpu_context(0);

results in

'use strict';
var a=new ArrayBuffer(1024);
new Uint8Array(a);
new TextDecoder;
var b=document;
b.querySelector.call(b,"").getContext("webgpu");

The last two generated lines look odd. Instead, these two lines would be shorter as

'use strict';
var a=new ArrayBuffer(1024);
new Uint8Array(a);
new TextDecoder;
document.querySelector("").getContext("webgpu");

[Online test](https://closure-compiler.appspot.com/home#code%3D%252F%252F%2520%253D%253DClosureCompiler%253D%253D%250A%252F%252F%2520%2540output_file_name%2520default.js%250A%252F%252F%2520%2540compilation_level%2520ADVANCED_OPTIMIZATIONS%250A%252F%252F%2520%253D%253D%252FClosureCompiler%253D%253D%250A%250Avar%2520buffer%2520%253D%2520new%2520ArrayBuffer(1024)%253B%250Avar%2520HEAPU8%2520%253D%2520new%2520Uint8Array(buffer)%253B%250Avar%2520UTF8Decoder%2520%253D%2520new%2520TextDecoder()%253B%250A%250A%2520%2520var%2520UTF8ToString%2520%253D%2520(ptr%252C%2520maxBytesToRead)%2520%253D%253E%2520%257B%250A%2520%2520%2520%2520%2520%2520if%2520(!ptr)%2520return%2520''%253B%250A%2520%2520%2520%2520%2520%2520var%2520maxPtr%2520%253D%2520ptr%2520%252B%2520maxBytesToRead%253B%250A%2520%2520%2520%2520%2520%2520for%2520(var%2520end%2520%253D%2520ptr%253B%2520!(end%2520%253E%253D%2520maxPtr)%2520%2526%2526%2520HEAPU8%255Bend%255D%253B)%2520%252B%252Bend%253B%250A%2520%2520%2520%2520%2520%2520return%2520UTF8Decoder.decode(HEAPU8.subarray(ptr%252C%2520end))%253B%250A%2520%2520%2520%2520%257D%253B%250A%250A%250Afunction%2520wgpu_canvas_get_webgpu_context(canvasSelector)%2520%257B%250A%2520%2520let%2520canvas%2520%253D%2520document.querySelector(UTF8ToString(canvasSelector))%253B%250A%2520%2520let%2520ctx%2520%253D%2520canvas.getContext('webgpu')%253B%250A%2520%2520return%2520ctx%253B%250A%257D%250A%250Awgpu_canvas_get_webgpu_context(0)%253B%250A)

juj commented 2 months ago

(Lines 2-4 are also something that would be impactful to be able to manually annotate to Closure Compiler to optimize away.. this was discussed in https://github.com/google/closure-compiler/issues/3185 , I see these types of effectively redundant lines in Emscripten output for all users)

rahul-kamat commented 2 months ago

Thanks for the report