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

Minification & inlining constants #509

Open
AkosLukacs opened this issue May 31, 2019 · 1 comment
Open

Minification & inlining constants #509

AkosLukacs opened this issue May 31, 2019 · 1 comment

Comments

@AkosLukacs
Copy link
Contributor

Creating this issue so someone with more closure compiler experience might be able to help. Or gather information we have.

The issue is to keep devices code readable and maintainable, registers and constant values should not go directly into the code, but kept in separate objects like var C = { ... }. Looks like previously Closure Compiler inlined these constant, but now it doesn't. And if you define long & readable constant names, that can use up quite some variables.

Might worth investigating, and possibly pulling in a specific version, if we can find one that does more aggressive inlining.
Advanced mode I think is pretty much out of question for modules, because it removes and renames too much. Did some fiddling with annotations and advanced optimizations, but no luck. Might work for final application, but didn't try that.

Example

An example using the BME280 as base. The constant is inlined. One thing I noticed is if I add another const, and use it in the constructor, C is no longer inlined. Even if you explicitly tell the compiler it's const.

/** @const */
const C = {/** @const */BME280_ADDRESS: 0x76, /** @const */BLA: 123}
...
// in ctor:
const t_sb = 5+C.BLA;    //The funny thing is, you can calculate this value at "compile time"

// minified (˙C` is renamed to `g`):
a=5+g.BLA<<5|0;

But if I use the same constant in the exports.connect function it's inlined. Even without explicitly telling the compiler that it's a constant:

var C = {BME280_ADDRESS: 0x76, BLA: 123}
...
// in exports.connect
i2c.writeTo(addr, reg+C.BLA);

// minified (123 is inlined):
a.writeTo(b,c+123)

No idea why does it work this way..

The two code samples here
Closure compiler online

@gfwilliams
Copy link
Member

Seems that BME280 is ok now, but BME680 isn't...

Pretty stumped by this as well. I posted a StackOverflow question on https://stackoverflow.com/questions/64697360/closure-why-doesnt-an-object-get-inlined but this is so cryptic it seems reporting on GitHub

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