是否可以让 Google Closure 编译器*不*内联某些函数?

发布于 2024-10-05 00:56:09 字数 577 浏览 4 评论 0原文

闭包编译器内联一个函数,但如果该函数未内联,代码大小会更小(我只关心代码大小 - 这是针对 JS1k )。我可以告诉编译器我不想内联该函数吗?

编辑:为了更好地解释一下,这是我的函数:

function lineTo(x,y) {
  a.lineTo(x,y);
}

在画布上下文中的 where a 。因为代码中有很多a.lineTo,所以使用这个函数是值得的。像这样,我的代码是 1019 字节(并且所有 lineTo 都被 a.lineTo 替换)。如果我将函数更改为:

function lineTo(x,y) {
  a.lineTo(x,y);
  console.log();
}

新行以某种方式强制编译器不内联此函数,这给了我 993 个字节。因此,如果我可以摆脱 console.log(); 我会再节省 14 个字节。

Closure compiler is inlining a function, but the code size is smaller if that function is not inlined (I only care about code size - this is for JS1k). Can I tell the compiler that I don't want that function inlined?

Edit: Just to explain a bit better, here's my function:

function lineTo(x,y) {
  a.lineTo(x,y);
}

where a in the canvas context. Because there are so many a.lineTos in the code, having this function used is worth it. Like this, my code is 1019 bytes (and all the lineTos are replaced by a.lineTo). If I change the function to:

function lineTo(x,y) {
  a.lineTo(x,y);
  console.log();
}

the new line somehow forces the compiler to not inline this function, which gives me 993 bytes. So if I could get rid of the console.log(); I'd save another 14 bytes.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

红墙和绿瓦 2024-10-12 00:56:09

来自教程

如果...您发现闭包编译器正在删除您想要保留的函数,有两种方法可以防止这种情况:

* 将函数调用移至闭包编译器处理的代码中。

* 导出您想要保留的符号。

您可能需要第二个,此处讨论,但基本上归结为显式地将其设置为 window 属性:

function foo() {
}
window['foo'] = foo;

对于您的 JS1k 提交,您只需保留最后一行即可,因为它是不需要的。请注意,Closure 仍会重命名该函数,但当它开始用名称 a 重命名您的符号并从那里继续时,它不太可能使您的名称总体上更长

您可以使用在线编译器服务进行尝试。如果将其粘贴到:

// ==ClosureCompiler==
// @compilation_level ADVANCED_OPTIMIZATIONS
// @output_file_name default.js
// ==/ClosureCompiler==

// ADD YOUR CODE HERE
function hello(name) {
  alert('Hello, ' + name);
}
hello('New user');

...编译结果是

alert("Hello, New user");

但是如果将

window['hello'] = hello;

...添加到末尾,编译结果是:

function a(b){alert("Hello, "+b)}a("New user");window.hello=a;

From the tutorial:

If...you find that Closure Compiler is removing functions you want to keep, there are two ways to prevent this:

* Move your function calls into the code processed by Closure Compiler.

* Export the symbols you want to keep.

You probably want the second, which is discussed here, but basically comes down to explicitly setting it as a window property:

function foo() {
}
window['foo'] = foo;

For your JS1k submission, you'd just leave the last line off as it's unneeded. note that Closure will still rename the function, but as it starts renaming your symbols with the name a and continues from there, it's unlikely to make your names longer overall.

You can try it out with the online compiler service. If you paste this in:

// ==ClosureCompiler==
// @compilation_level ADVANCED_OPTIMIZATIONS
// @output_file_name default.js
// ==/ClosureCompiler==

// ADD YOUR CODE HERE
function hello(name) {
  alert('Hello, ' + name);
}
hello('New user');

...the compiled result is

alert("Hello, New user");

But if you add

window['hello'] = hello;

...to the end, the compiled result is:

function a(b){alert("Hello, "+b)}a("New user");window.hello=a;
爱冒险 2024-10-12 00:56:09

这是一个老问题,但从 v20180101,Google Closure Compiler 有一个防止内联的注释: @noinline

在原始示例中,您所需要做的就是在不应内联的函数前面添加带有此注释的 jsdoc:

/** @noinline */
function hello(name) {
  alert('Hello, ' + name);
}
hello('New user');

This is an old question, but as of v20180101, Google Closure Compiler has an annotation that prevents inlining: @noinline.

In your original example, all you'd need is to add jsdoc with this annotation in front of the function that shouldn't be inlined:

/** @noinline */
function hello(name) {
  alert('Hello, ' + name);
}
hello('New user');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文