Hacker News new | ask | show | jobs
by bcg1 4113 days ago
Sort of... technically only the '+' operator only applies to strings... however it is essentially the same issue as the pull request, from what I can gather (I assume the issue comes from allocating a new array with size length+1 and copying the original each time a record is added). Same thing happens with java.util.ArrayList.add() and its cousins though, and from my experience people rarely use the constructor specifying an initial capacity, so the default gets used even if it is would obviously be woefully small (default is 10 BTW, in case you're curious & lazy :).

Also one might argue that the problem is actually much worse with strings, because string concatenation is so common and the syntactic sugar of the '+' operator for strings encourages the "wrong" way.

2 comments

Java's JIT compiler optimises string concatenation by substituting a StringBuilder and has been doing so for a while.

As to using the wrong data type, that's really the programmer's fault. If you don't allocate enough capacity or use another data type (e.g. LinkedList) if you don't know the required capacity, you are doing a bad job.

To nitpick, Java's JIT compiler compiles Java bytecodes to machine code, but I know that javac does to the type of optimization that you are describing. There are plenty of scenarios though where it can't/won't do that optimization ... and if you are using the binary version of a library compiled without that optimization I'm pretty sure you're out of luck, especially if the method you're calling is not JIT'd.

I'm not really trying to knock any particular language or runtime here, the point I was trying to make is that nearly every language I've used has quirks that encourage convenience over optimization, and that just because you're coding in language foo, it doesn't mean you're off the hook when it comes to being intentional about the choice between them.

Just to clarify, java.util.ArrayList.add() grows the backing array by 50%, not just by length()+1.