4 October 2009

I just killed Eclipse

I like enum-s more than most people do.

They are really nice to use and synchronization-safe. But as I found, they have their limits as well.

After generating 1,8Mb Java code for one enum Eclipse dying after most try of refactorization ;), Java was not happy at all about it as well with error:

The code for the static initializer is exceeding the 65535 bytes limit

I already saw bug about it 4262078. I just had no idea that I'll ever reach that huge amount of code in one 'function'.

Lets see what we need deal with!

  • We need to store almost 1000 elements.
  • Each element have up to 30 attributes.
  • Constructor signature look like:
    private Element(int section, int code, String name
            , Pair<ItemAttributes, Object>... attributes) { ... }

Step 1:
Less see how many entries can we have...
Java start to work when file size get down to 380Kb and number of elements was around 130.

Step 2:
Adding magic code...

public class PIA extends Pair<ItemAttributes, Object>{

  public PIA(ItemAttributes key, Object value) {
    super(key, value);
  }

}

Replacing 'Pair<ItemAttributes, Object>' with PIA reduced class size to 1Mb.

Step 3:
More magic code...
renaming ItemAttributes to IA reduced code size to 700Kb, and still not enough.

Step 4:
More magic code...
After simplifying constructor to

private Element(int section, int code, String name, Object[]... attributes) { ... }
source code take 450Kb

Last step
Undo all changes and split elements into enum-s similar with sections.

0 comments / komentarz(y):

Post a Comment