@const tag ignored with SIMPLE_OPTIMIZATIONS

18 views
Skip to first unread message

Arnauld

unread,
Mar 29, 2011, 12:05:20 PM3/29/11
to Closure Compiler Discuss
In one of my projects, I don't really need (and don't want) to use
ADVANCED_OPTIMIZATIONS and I'm very happy with SIMPLE_OPTIMIZATIONS.
However, there's one thing that I find a bit annoying: variables
prefixed with the @const tag are left unchanged.

What is (are) the reason(s) why the @const tag is ignored with
SIMPLE_OPTIMIZATIONS? I can't figure out any concrete case where it
would be a problem. And if such a problem exists, would it make sense
to consider adding a compilation option to explicitly turn the
constants translation on/off? Or is there already a way to do that?

-- Arnauld

John Lenz

unread,
Mar 29, 2011, 12:54:04 PM3/29/11
to closure-comp...@googlegroups.com, Arnauld
On Tue, Mar 29, 2011 at 9:05 AM, Arnauld <arnauld.c...@gmail.com> wrote:
In one of my projects, I don't really need (and don't want) to use
ADVANCED_OPTIMIZATIONS and I'm very happy with SIMPLE_OPTIMIZATIONS.
However, there's one thing that I find a bit annoying: variables
prefixed with the @const tag are left unchanged.

 
Can you provide an example of what you would like to happen?  It isn't clear to me what you are expecting.

Arnauld

unread,
Mar 29, 2011, 1:13:31 PM3/29/11
to Closure Compiler Discuss
John,

Below is a short example:

/** @const */ var MY_CONST = 123;
function test() {
return MY_CONST;
}
alert(test());

ADVANCED_OPTIMIZATIONS will produce the minimalist "alert(123);",
which is fine.

SIMPLE_OPTIMIZATIONS will produce the following output:
var MY_CONST=123;function test(){return MY_CONST}alert(test());

I expect SIMPLE_OPTIMIZATIONS to take @const tags into account and
produce something like:
function test(){return 123}alert(test());

-- Arnauld

John Lenz

unread,
Mar 29, 2011, 1:31:46 PM3/29/11
to closure-comp...@googlegroups.com, Arnauld
We will never do this for SIMPLE, MY_CONST is part of the public interface (tthe global names).  What you want is some way to indicate some names are not part of the public interface,  ADVANCED mode lets you do this.

It is possible we might support replacement of @define values that are provided on the command-line in SIMPLE, but it isn't clear that we would actually inline those.

John Lenz

unread,
Mar 29, 2011, 6:41:53 PM3/29/11
to closure-comp...@googlegroups.com, Arnauld
I should note that we have on and off discussed a MEDIUM mode ("ADVANCED" without property renaming).

Arnauld

unread,
Mar 31, 2011, 11:34:06 AM3/31/11
to Closure Compiler Discuss
On Mar 30, 12:41 am, John Lenz <concavel...@gmail.com> wrote:
> I should note that we have on and off discussed a MEDIUM mode ("ADVANCED"
> without property renaming).

Thanks John for your explanations. That makes a lot of sense. I guess
this proposed MEDIUM mode is what I'm looking for.

John Lenz

unread,
Mar 31, 2011, 1:31:24 PM3/31/11
to closure-comp...@googlegroups.com, Arnauld
One experiment that might be interesting is to create a compiler pass that rewrites all property references so they were all external:

  x.a --> x['a']
  x = {a:1}  -> x = {'a':1}

These quotes are already dropped post-optimization, so there is no additional cost (over simple).  Likely, we would want to leave "prototype" alone, but otherwise the rewriting is dead simple (GETPROP becomes GETELEM, all object literal keys are tagged as quoted), and prevents the need of having a new optimization level.

Variables would still need to be exported, etc.
Reply all
Reply to author
Forward
0 new messages