是否有可能使Google Closure编译器*不*内联某些函数? [英] Is it possible to make Google Closure compiler *not* inline certain functions?

查看:86
本文介绍了是否有可能使Google Closure编译器*不*内联某些函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Closure编译器正在内联一个函数,但如果该函数没有内联,代码大小会更小(我只关心代码大小 - 这是为了 JS1k )。我可以告诉编译器我不希望内联函数吗?

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?

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

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

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

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

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();
}

新行以某种方式迫使编译器不内联此函数,这给了我993字节。所以,如果我可以摆脱 console.log(); 我将另外保存14个字节。

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.

推荐答案

来自教程


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

*将函数调用移动到Closure Compiler处理的代码中。

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

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.

你可能想要第二个,这是此处讨论,但基本上归结为明确将其设置为 window property:

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;

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

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');

...编译结果是

alert("Hello, New user");

但如果你添加

window['hello'] = hello;

...到最后,编译结果是:

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

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

这篇关于是否有可能使Google Closure编译器*不*内联某些函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆