是否可以让 Google Closure 编译器*不*内联某些函数?
闭包编译器内联一个函数,但如果该函数未内联,代码大小会更小(我只关心代码大小 - 这是针对 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.lineTo
s in the code, having this function used is worth it. Like this, my code is 1019 bytes (and all the lineTo
s 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
来自教程:
您可能需要第二个,此处讨论,但基本上归结为显式地将其设置为
window
属性:对于您的 JS1k 提交,您只需保留最后一行即可,因为它是不需要的。请注意,Closure 仍会重命名该函数,但当它开始用名称
a
重命名您的符号并从那里继续时,它不太可能使您的名称总体上更长。您可以使用在线编译器服务进行尝试。如果将其粘贴到:
...编译结果是
但是如果将
...添加到末尾,编译结果是:
From the tutorial:
You probably want the second, which is discussed here, but basically comes down to explicitly setting it as a
window
property: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:
...the compiled result is
But if you add
...to the end, the compiled result is:
这是一个老问题,但从 v20180101,Google Closure Compiler 有一个防止内联的注释:
@noinline
。在原始示例中,您所需要做的就是在不应内联的函数前面添加带有此注释的 jsdoc:
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: