Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How harmful are double braces in GWT?

Tags:

java

syntax

gwt

I tend to use (or even overuse) double braces object intialization in GWT. For me it looks more readable and more declarative.

new FastMap<Object>(){{
    put("Value", 12);
    put("Unit", "Kg");
}};

Before today I was not aware that this syntax is not just instantiate object but also create AnonymousInnerClass for it. Now I am concerned how GWT deal with them.

  1. How this syntax affects perfomance of the execution?
  2. How this syntax affects compiled size?
  3. Any other bad thing?

I have thousands of such initializations in my app.

like image 358
Mike Chaliy Avatar asked Mar 10 '12 21:03

Mike Chaliy


People also ask

What's wrong with double brace initialization in Java?

Disadvantages of Using Double Braces It creates an extra class every time we use it. Doesn't support the use of the “diamond operator” – a feature introduced in Java 7. Doesn't work if the class we are trying to extend is marked final. Holds a hidden reference to the enclosing instance, which may cause memory leaks.

What is double brace?

Double brace initialisation creates an anonymous class derived from the specified class (the outer braces), and provides an initialiser block within that class (the inner braces).


3 Answers

My findings. Test code. I am creating single list with 4 maps with 21 item each. I am measuring size of all generated JavaScript files. Results:

Empty (just empty code to make sure that GWT support code rendered):

  • PRETTY = 533 KB (546 602 bytes)
  • OBF = 212 KB (217 601 bytes)
  • Number of new = 167

Code without double braces:

  • PRETTY = 557 KB (570 682 bytes)
  • OBF = 222 KB (227 663 bytes)
  • Number of new = 171

And same code with double braces:

  • PRETTY = 567 KB (581 004 bytes)
  • OBF = 228 KB (234 089 bytes)
  • Number of new = 177

I think results are pretty self-explanatory.

like image 101
Mike Chaliy Avatar answered Sep 28 '22 09:09

Mike Chaliy


This is standard Java and is independent of GWT. It is dealt with comprehensively in Efficiency of Java “Double Brace Initialization”?.

The biggest problem I have with this syntax is that is doesn't deliver an instance of FastMap, but rather of an anonymous subclass of that. That object doesn't compare as equal to an equivalent instance of FastMap with the data set up in the traditional way. It is also likely to have other gotchas that aren't obvious and are less that straightforward to debug.


The thread here says this

Watch out - double-brace initialization is cool looking, but it's orders of magnitude slower than regular initialization as it must generate an anonymous class.

Also, the generated class is put into the permgen space, which is not garbage collected. The permgen is pretty small by default - and if you fill it up, your system is hosed.

I use them all the time in unit tests, but never in production code.

like image 28
Borodin Avatar answered Sep 28 '22 08:09

Borodin


As far as readability is concerned: I would change it to something like this to make it easy to read:

new FastMap<Object>()
{{
    put("Value", 12);
    put("Unit", "Kg");
}};

You however might want to read this answer to find out more about performance related issues.

like image 45
beta Avatar answered Sep 28 '22 07:09

beta