using types for optimization and code removing

49 views
Skip to first unread message

Peter StJ

unread,
Jan 18, 2013, 1:25:18 PM1/18/13
to closure-comp...@googlegroups.com
I have a question: I am trying to use the advanced mode with the following code:

http://jsfiddle.net/TPdaC/

I used the following compiler command:

java -jar ../../compiler/compiler.jar --compilation_level ADVANCED_OPTIMIZATIONS  --formatting PRETTY_PRINT --use_types_for_optimization  --js ../../library/closure/goog/base.js --js compile.js

Basically I want the compiler to strip out the code that is not used.

The compiler code looks like this:

function b(a, c) {
  function d() {
  }
  d.prototype = c.prototype;
  a.a = c.prototype;
  a.prototype = new d
}
;Animal = function(a) {
  this.name = a
};
Mamal = function(a) {
  Animal.call(this, a)
};
b(Mamal, Animal);
Human = function(a) {
  Mamal.call(this, a)
};
b(Human, Mamal);
console.log("Hello out there.");

The compiler correctly removes the unused methods from the classes - this is fine. However I am wondering the following:
a) an instance was never created for any of those classes, why are they not removed?
b) wll it make sence for the compiler to flatten out the inheritance chain in such cases where the parent classes are not really used directly

Thanks


Ilia Mirkin

unread,
Jan 18, 2013, 1:32:38 PM1/18/13
to closure-comp...@googlegroups.com
With the proper annotations, it works as you expect:

// ==ClosureCompiler==
// @compilation_level ADVANCED_OPTIMIZATIONS
// @output_file_name default.js
// @use_closure_library true
// ==/ClosureCompiler==
/** @constructor */
var Animal = function(a) {
this.name = a
};
/** @constructor
@extends {Animal} */
var Mamal = function(a) {
Animal.call(this, a)
};
goog.inherits(Mamal, Animal);
/** @constructor
@extends {Mamal} */
var Human = function(a) {
Mamal.call(this, a)
};
goog.inherits(Human, Mamal);
console.log("Hello out there.");

I was playing around with stuff, not sure if all the changes are
necessary. But at http://closure-compiler.appspot.com/home this
reduces down to the console.log.

Nick Santos

unread,
Jan 18, 2013, 2:26:43 PM1/18/13
to closure-comp...@googlegroups.com
If you're using advanced optimizations, it generally helps to turn on
VERBOSE warnings, as that will tell you a lot about where the
type-based optimizations are falling down.

Peter StJ

unread,
Jan 19, 2013, 1:29:46 PM1/19/13
to closure-comp...@googlegroups.com
Thanks, turning on verbose was a nice hint. Also thanks to Ilia Mirkin, I was able to figure out the problem, because the classes were not namespaces the compiler did not remove them, adding var in front of those or namespacing them actually removed them completely which was the expected result.
Reply all
Reply to author
Forward
0 new messages