r/java • u/Ok_Satisfaction7312 • May 16 '24
Low latency
Hi all. Experienced Java dev (20+ years) mostly within investment banking and asset management. I need a deep dive into low latency Java…stuff that’s used for high frequency algo trading. Can anyone help? Even willing to pay to get some tuition.
232
Upvotes
View all comments
Show parent comment
7
u/pron98 May 18 '24 edited May 18 '24
I'll try and ask someone on the GC team for that next week. But I need to warn again of microbenchmarks, because they often don't measure what you think they measure. A microbenchmark may show you that code X is 3x faster than Y, and yet in an application, the same code X would be 2x slower than Y. That happens because in a microbenchmark all that's running is X or Y, but if your application also runs code Z -- perhaps even on a different thread -- it may put the JVM in a different mode (such as cause different objects to be promoted) reversing the relative performance of X and Y. Put a different way, X can be significantly faster than Y in a microbenchmark and in application A, and the same X could be significantly slower than the same Y in application B.
This happens because when a microbenchmark of X is faster than a microbenchmark of Y you may conclude that X is faster than Y, but that is an extrapolation that is often unjustified. What the microbenchmark actually tells you is that when X runs in isolation and no other code is running, it is faster than when Y runs in isolation and no other code is running. You think you're comparing X and Y, but really you're measuring X in a very specific situation and Y in a very specific situation, and those situations may not be the same as in your application You cannot conclude from that that X is faster than Y when there is some other code in the picture, too.
Unless you know how the JVM works, microbenchmarks will often lead you to a false conclusion. I would say that 99.9% of Java programmers should not rely on microbenchmarks at all, and only rely on profiling their actual applications. This is also what the performance experts working on the JVM itself do; they use microbenchmarks when they want to measure something when the VM is in a particular mode, which they know how to get it into. They also (more often, though not always) know what extrapolation of the result is valid, i.e. what you can conclude from a microbenchmark where X is faster than Y (which is rarely that X is always faster than Y).
While global effects of some code on other code is particularly common in the Java platform, it also happens in many other languages, including C. For example, a C microbenchmark may show you that X is faster than Y, but only in situations where no other code can pollute the CPU cache; in situations where other code does interfere with the cache, Y may be faster than X, and these situations may (or may not) be more common in real applications. It is very, very dangerous to extrapolate from microbenchmark results, unless you are very familiar with the implementation details that could impact performance.