Skip to content

Minification & inlining constants #509

@AkosLukacs

Description

@AkosLukacs

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions