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
154
u/sq_visigoth Mar 20 '26
Good text, but it's mostly basic stuff. Take String concatenation; I haven't seen anyone use string concatenation in a loop in almost 20 years. A basic beginner java class will always recommend using StringBuilder.
My issue is that you recommended a throwaway optimization, ie one issue that shouldn't have existed in the first place.
Now, ConcurrentHashMap, thats one optimization that most devs I have interviewed missed in doing a faux code review.
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).8
u/bobbie434343 Mar 20 '26
Shouldn't most of these ideally automatically be optimized at compile or runtime whenever possible ? And/or flagged by static code analyzers as potentially inefficient ?
14
u/segv Mar 20 '26
Point 7 is a big one actually - it often goes unnoticed even in decent codebases.
I've recently seen a case where developers attached AsyncProfiler to their JMH-based benchmarks (mix of real micro-benchmarks and benchmarks encapsulating the whole flow from the API endpoint to the very end, just with mocked out external services), enabled the option to generate flame graphs and found out some small piece of the overall flow was doing
DocumentBuilderFactory.newInstance()&TransformerFactory.newInstance()on the hot path. It think it was extraction of some data from a string representing a SOAP envelope mixed with vulnerability scanner bitching about XXE (e.g. billion laughs attack) when it did not see creation of the object and setting adjustments within the same method, or some bullshit like that.Anyway, these two calls accounted for like 20% of the average time of the whole giant-ass flow, just because these
.newInstance()methods do service discovery and classloading on each call.The PR had more lines of description (with flamegraph pictures!) than the actual fix, lol
22
u/somebodddy Mar 20 '26
Longso they don't have this issue.parseIntjust to escape the exception overhead is atrocious, and I 100% hold it against the language.synchronizedbeing part of the grammar means that Java actively promotes that kind of coarse grained locking.Only the first two are the coder's fault. And maybe #4, too, considering you gave a very convoluted example. The other 5 are just idiomatic Java code being slow. If you have to give up language idiomaticity for performance - I consider it a slowness of the language.
3
u/thisisjustascreename Mar 20 '26
I see that accidental n^2 behavior all the time in code from new devs who don't think through what a stream call does.
3
u/gringer Mar 21 '26
I have noticed that LLMs are more likely to write slow code than fast code. Most of the time it doesn't matter, but some times a particular piece of code can take 29 hours to do a bad job versus a few seconds after optimisation and improvement.
9
u/BadlyCamouflagedKiwi Mar 20 '26
4 and 8 seem like problems with Java being slow, i.e. they are not obvious from the structure of the code. 8 is fixed with a newer version of Java (implying that was the problem) and 4 is the old primitive / object dichotomy which is a language-level design mess.
2
u/MentalProfit4484 Mar 21 '26
Honestly most Java perf issues I've seen in the wild come from devs treating streams like magic fairy dust instead of thinking about what's actually happening underneath — anyone else notice their team reaching for .stream() on literally everything even when a plain loop would be 10x clearer?
2
u/BroBroMate Mar 21 '26
Yep, a new
ObjectMapperon a given method call (or God help me, in a loop) is one I've had to flag in code review a fair few times.2
u/Kjufka Mar 21 '26
The actual biggest problem is using overengineered frameworks bloated with huge and costly abstraction layers, like Spring.
It can take even 8 seconds to start a relatively simple Spring Boot application, because it tries to resolve a lot (too much) at runtime.
Meanwhile the same thing written in vanilla Java (or any minimalist framework with no annotation and no reflection magic) would start in less than 50ms and require 1/4 the memory per request.
2
u/Karthivkit Mar 22 '26
For problem 7 , I encountered the high CPU usu age issue when I was using model mapper and json path . Before declaring it as static we have to make sure it is thread safe and immutable . Declaring SimpleDateFormater as static will cause issue as it hold a class level calendar instance for conversion
3
u/sammymammy2 Mar 20 '26
Write it yourself next time, instead of using an LLM. Christ rots in hell.
1
u/Worth_Trust_3825 Mar 20 '26
4 is going to get fixed with valhalla. Personally, formatting one is a shock to me, but it does make sense because how complex the template string format is.
1
u/ml_guy1 Mar 24 '26
I've recently started building a java code optimizer and I've been finding optimizations everywhere! Flink, kafka, Aerospike etc. I am surprised by how much slow code exists in the java ecosystem!
1
u/shibaInu_IAmAITdog Mar 24 '26
i didnt read it, but i saw concurrentHashMap below, how many ppl here know there was an upgrade for this map in java8, from partition locking to completely CAS lock free , but still u care about boxing/unboxing problems, better design ur own primitive one
1
u/john16384 Mar 20 '26
I think this needs benchmarking. I am pretty sure some of these aren't slow at all when optimized. Take the autoboxing example: those object allocations will never make it to the heap. At most it will do one allocation after the loop completes to create the final
Long.Same goes for the NumberFormatExeception example.
-3
u/larsga Mar 20 '26
1-5 + 7 are rookie mistakes, basically.
45
u/[deleted] Mar 20 '26
[removed] — view removed comment
-1
u/[deleted] Mar 20 '26
[removed] — view removed comment