Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
juj opened this issue Sep 4, 2024 · 2 comments

Comments

@juj
Copy link

juj commented Sep 4, 2024

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

@juj
Copy link
Author

juj commented Sep 4, 2024

(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 #3185 , I see these types of effectively redundant lines in Emscripten output for all users)

@rahul-kamat
Copy link
Contributor

Thanks for the report

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants