News for hackers. Stuff that matters.
Re:Groovy Array: it just keeps going...and going... by jff on Monday November 29, 2010 @04:22PM (#1951828)
I just went to post that... can anybody explain to me what exactly is going on? I'm not familiar with Groovy, and I try to forget Java when I can, but this just plain doesn't make any sense.
Re:Groovy Array: it just keeps going...and going... by akronim on Monday November 29, 2010 @04:32PM (#1951858)
They're aiming for compatibility with Java 1.4, which doesn't have varargs. In java 1.5 you could just write

  public static Object[] createArray(Object... args) {
    return args;
  }
Re:Groovy Array: it just keeps going...and going... by zmmmmm on Monday November 29, 2010 @06:44PM (#1952348)
I remember discovering this and deciding to eliminate it as part of an effort to compress the groovy jar file a bit more (long story, but basically I was running some Groovy in an applet, every byte counts).

I thought I'd found a jackpot and that killing this class would surely eliminate a nice chunk of byte code. To my amazement it makes almost no difference. With pack200 the redundancy of all these methods is totally stripped out. It took me an hour or so of work and I think it made less than 200 bytes difference in the end.

Re:Groovy Array: it just keeps going...and going... by abp on Monday November 29, 2010 @06:25PM (#1952278)
So just another missing line of essential commentary?
Re:Groovy Array: it just keeps going...and going... by nysauhem on Monday November 29, 2010 @04:33PM (#1951868)
You could accomplish the same thing with a

public static Object[] createArray(Object... objects) { ... }

but doing it statically, hard-coded like this is faster. You leave it to the compiler to determine the length of the arguments, rather than having to count them at run-time.

The Google Guava library does something similar, but not to this extent, only up to 12 args http://guava-libraries.googlecode.com/svn-history/r12/trunk/...

Re:Groovy Array: it just keeps going...and going... by riffraff on Monday November 29, 2010 @04:33PM (#1951866)
well, java has variable length arguments now so the whole file could be simply public static Object[] createArray() { return EMPTY; } public static Object[] createArray(Object... args) { return args; }

My guess is they probably had this before java supported such a syntax so that you could create arrays easily. once you auto-generate the file, you just do it up to 255.

I do not have a good explanation of why this is better than the normal array initializer though, apart from the fact that once you standardize on this you may have some minimal advantage in not allocating empty arrays around.

Re:Groovy Array: it just keeps going...and going... by lepht on Monday November 29, 2010 @11:00PM (#1953022)
>My guess is they probably had this before java supported such a syntax so that you could create arrays easily

varargs came in Java 1.5, September 30, 2004

Groovy 1.0, January 2, 2007

Definitely could be for compiler optimizations or backwards compatibility, just providing some context for any possible historical reasons like the one you stated.

Re:Groovy Array: it just keeps going...and going... by bruceboughton on Tuesday November 30, 2010 @05:01AM (#1953658)
If it's for optimisation, why introduce another method call (ArrayUtil.createArray) instead of using new Object[] { .. } directly?

I can understand the backwards compatibility argument, but I just don't see the need for this ArrayUtil class.

Re:Groovy Array: it just keeps going...and going... by lepht on Tuesday November 30, 2010 @11:59AM (#1954715)
Two things:

Calling new Object[]{} directly is going to call the constructor for Object anyway, so there's no really difference in the number of method calls for either.

Also, in terms of optimization: calling new Object[]{...} makes it so that the size of the array isn't known until runtime when the initialization parameters can be evaluated. Because array length is immutable the JVM must wait until execution to create an array of the proper size.

With the overloaded static createArray() methods used in ArrayUtils the size of the array can be determined and memory allocated (based on the number of parameters at the callsite and hence which variation of the method should be called) at the JRE's convenience/when optimal.

All of this is probably micro-optimization to the who-really-cares level, and might even be irrelevant due to javac/JIT magic, just wanted to show that the existence of the new Object[] syntax doesn't preclude there being performance gains from using something like ArrayUtil.createArray().

Re:Groovy Array: it just keeps going...and going... by GrandMasterBirt on Tuesday November 30, 2010 @10:36AM (#1954355)
Compatibility :)

In any case, you can easily generate an ant script or whatever build groovy uses to either generate the vararg code or the insaneo source code as we see here. The point being that this should only happen when compiling groovy for java 1.4.

Re:Groovy Array: it just keeps going...and going... by lepht on Tuesday November 30, 2010 @11:28AM (#1954586)
>In any case, you can easily generate an ant script or whatever build groovy uses to either generate the vararg code or the insaneo source code as we see here.

Yeah, I don't think anyone is implying this is handwritten. Generating a text file is trivial in any modern language/build system.

>The point being that this should only happen when compiling groovy for java 1.4.

This is a part of the pre-compiled Groovy runtime library, it's used regardless of what JDK version you're targeting for your Groovy application/script.