81 points · by rbxbx · 16y ago · 34 comments
public static Object[] createArray(Object... args) {
return args;
}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.
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/...
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.
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.
I can understand the backwards compatibility argument, but I just don't see the need for this ArrayUtil class.
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.
Java is a harsh mistress. This is why dynamic languages on the JVM are nice, so I never have to write code like this myself.
I'm sure it's legitimate, but you'd have to be insane to look at that code and not question why it exists.
$ svn log -r {2010-11-30}:HEAD http://svn.codehaus.org/groovy/trunk/groovy/groovy-core/src/main/org/codehaus/groovy/runtime/ArrayUtil.java
------------------------------------------------------------------------
r21150 | hamletdrc | 2010-11-30 19:32:11 +0100 (Tue, 30 Nov 2010) | 3 lines
updated javadoc on arrayutil
------------------------------------------------------------------------
r21152 | hamletdrc | 2010-11-30 21:31:24 +0100 (Tue, 30 Nov 2010) | 1 line
Updated ArrayUtil javadoc one more time so that people quite wondering what it is used for.(I guess, then this is also what test cases are for...)
It's one letter longer than `createArray(arg1, arg1...)`, doesn't require you to include that chunk of code, is infinite in length, and allows you to statically-type it in your type of choice. Heck, it's the same code, just removing a layer of indirection.
edit: noticed it's one character longer, not the same length. Maybe that's why?
(0..249).each { println "public static Object[] createArray(${(0..it).collect{'Object arg'+it}.join(', ')}) {\n\treturn new Object[]{\n\t\t${(0..it).collect{'arg'+it}.join(', ')}};\n}\n" }
Sorry it's not the most readable thing ;) public static Object[] createArray(Object... args) {
return args;
}
I haven't done any Java in a while, so I'm just piecing this together from StackOverflow examples (with compiling, of course).Your own favorite dynamic language is most likely doing worse things deep inside the runtime code where you can't see it.
(just as an example ... I'm aware that this is not the case here)