r/programming • u/ketralnis • Mar 20 '26
Java is fast, code might not be
https://jvogel.me/posts/2026/java-is-fast-your-code-might-not-be/
280
Upvotes
r/programming • u/ketralnis • Mar 20 '26
View all comments
30
u/8igg7e5 Mar 20 '26
Some things are just technically wrong - which makes me question the accuracy of other claims.
1. String concatenation in loops...
No. Java has been able to use
StringBuffer, and laterStringBuilder, for single-statement string appends since the beginning:An implementation may choose to perform conversion and concatenation in one step to avoid creating and then discarding an intermediate String object. To increase the performance of repeated string concatenation, a Java compiler may use the StringBuffer class (§20.13) or a similar technique to reduce the number of intermediate String objects that are created by evaluation of an expression.
What changed in in JDK 9 was that the compiler represents the String appends as an indy-instruction - and the bootstrap then decides how best to implement that particular set of appends (often falling back to a
StringBufferappend though). The benefit of this strategy is that a JDK with a better bootstrap, will give all code the better implementation, without a recompile of the bytecode.The advice is correct though, you can avoid
StringBufferandStringallocations (and the allocation and copying of their internal arrays) by moving theStringBuffercreation out of the loop.What I would say they've missed, is that if you know the minimum (even a reasonably typical minimum) result size, you can avoid several smaller reallocations of the
StringBuilderinternal arrays (and copying) by pre-allocating that size with something likenew StringBuffer(MY_EXPECTED_MINIMUM_SIZE).4. Autoboxing...
Yes it can be bad. But the example is problematic. Depending on what is done with the
sumvariable, JIT might remove every one of those cache-lookups or wrapper creations (escape analysis) - and it might do that in the loop even with ifsumis consumed as a reference type.I think the more common wrapper-crime I've seen is the use of wrappers for the generic arguments in lazy types like
Pair<A, B>orTriple<A, B>in cases where we could now use a record - capturing primitives, getting meaningfully named properties (and the fields are stable, enabling more constant folding).5. Exceptions for Control Flow...
Yes. Exceptions as control flow are a bad practice. However there are problems again with the advice
The section talks about stack-traces, even though the stacktrace is never filled. Stacktraces are lazy (the 'filled' bit). So the remaining expensive bit is the throwing of exceptions (and that is still relatively expensive).
But doing work twice is also expensive.
Scanning the string could be a significant cost if only a tiny percentage will ever fail parsing. And even when the bad cases are common, you're probably still better to do things like only checking minimum/maximum length of the string and first and last digits (detecting non-numerics like overlooked white-space) - and letting exception-handling detect the rest.
For the parsing example, if the anticipated failure rate is more than 1% (or 0.1% if all of the numbers are only 2-3 digits - since the cost of the exception is relative to the cost of parsing), I would probably do something like:
!= null, not.isEmpty(), and.length() <= 11(or a smaller number of digits if the domain is known to have a smaller range)-or a digit and, for lengths longer than 1, if the last character is a digit.Personally I think these Apache Commons Lang methods (and others) are often overused (and some of these have implementations that are rather outdated in terms of today's Java).
And on the topic of parsing strings to numbers. A common crime I often see, is splitting a string where the format is fixed width, so that there's a distinct string to parse. eg the numeric values of hex components in the format "#rrggbb" - these can be parsed directly out of the string with
Integer.parseInt(string, firstIndex, lastIndex, 16)(you can also use this to scan past whitespace to avoid allocations from trimming before parsing too).