#20 Why Java's TLABs are so important
Executing Code on Spring Boot Application Startup. Does Java Need Inline Types? Cognitive Biases in Software Development
Welcome to the twentieth Dukesletter edition!
The most important property of a program is whether it accomplishes the intention of its user
― C.A.R. Hoare
OpenJDK 8u262 has been released with a lot of securities fixes. The main improvement is the JFR Backport.
The publication of the State of Loom has prompted some common questions. Ron Pressler attempts to answer some of them in Loom Q&A.
Apple is not the only company migrating to ARM architectures. Microsoft is improving its support of Windows 10 for ARM, and as part of this process needs to contribute to the ecosystem that runs on Windows, like the JVM. You can read more about it in “Announcing OpenJDK for Windows 10 on ARM”.
Microsoft team is on fire, and is pushing a new JEP: Improved Stack Allocation in C2, which reduces up to 15% heap allocations. In the latest Fosdem, they presented their idea and prototype in a talk.
Why Java's TLABs are so important
Java Thread-Local Allocation Buffers can be explained with one quote from Operating Systems: Principles and Practice book:
The advantage of subdividing the heap is that multiple threads can each allocate objects at the same time without interfering with one another. Further, by allocating object used by the same thread from the same memory region, cache hit rates may improve.
― Thomas Anderson
To understand how the allocation strategy impacts in the CPU cache, Sadiq Jaffer, Opsian’s CEO, has written an article: “Why Java's TLABs are so important and why write contention is a performance killer in multicore environments”.
But as an introduction to TLABs concepts, I prefer these articles:
Aleksey Shipilёv articles are deeper, and he has two chapters of his “JVM Anatomy Quark” collection dedicated to TLABs:
With the incoming Project Loom with Virtual Threads, will the JVM behave in the same way? will it change the design?
Executing Code on Spring Boot Application Startup
Sometimes your application needs to load or preprocess some information at start-up time. If you are using a complex framework like Spring Boot, which manages the lifecycle of your objects, when is the correct moment? what mechanisms does spring provide to perform that process?
Spring Boot has multiple mechanisms, and Tom Hombergs summarizes the main ones in the following article: “Executing Code on Spring Boot Application Startup”.
As personal advice: never load or process information using @PostConstruct
or InitializingBean
. These methods are called by Spring when it is creating the Application Context, and this process is executed in the main thread, locking other initialization process. If you have multiple objects with @PostConstruct
, they are executed serially in the main thread and you don’t know in which order will be executed.
I personally prefer to use CommandLineRunner
, ApplicationRunner
or ApplicationListener
, because they are executed once the Application context has been created, including the Http Container (Tomcat or Jetty), and because the context is ready you can execute multiple processes in parallel.
How you initialize a server is important if you want to manage correctly how to signal the readiness and liveness of your containers.
Does Java Need Inline Types?
The OpenJDK team is executing in parallel multiple very interesting projects. One of them is Inline Types from Project Valhalla.
If Project Loom will allow us to improve the CPU usage, Inline Types will improve memory usage.
It is a big internal architectural change of the JVM and will take time to finish. If you are interested in the internals of the JVM don’t miss a talk like this:
If you prefer to read, you have the transcription here with its slides.
The JDK team thinks that a lot of classes from the JDK will become Inline Types (Integer, Long, Optionals, etc) and is preparing the migration path. You can read more about it in this draft JEP: Identity Warnings for Inline Class Candidates.
Is this change backward compatible? Will it be traumatic? I think that the advantages will compensate for any potential problem.
Cognitive Biases in Software Development
A cognitive bias is a systematic error in thinking that occurs when people are processing and interpreting information in the world around them and affects the decisions and judgments that they make.
Cognitive biases are often a result of your brain's attempt to simplify information processing. Biases often work as rules of thumb that help you make sense of the world and reach decisions with relative speed.
They help us to survive in a world full of information, but we must train our brain to minimize cognitive biases in thinking.
The best way to avoid them is to know them. In “8 Cognitive Biases in Software Development” you can learn about some of them and how they affect our performance in our daily work. The article gives examples and tips to avoid it, or at least to be aware of it.