I recently stumbled upon a blog
run by Peter Lawrey . There you can find interesting information that relates to the performance of the code under the JVM.
There I found an unusual approach, which is to reuse objects. Unusual for several reasons:
- The generally accepted view that the creation of objects is now so fast that you should not pay attention to it
- This approach is rarely used, and there must be reasons for this.
But there is an area where the benefits of this concept can be seen. This is serialization (or rather, deserialization) of objects. The fact is that in this case there is a constant creation of new objects, which negatively affects the performance. Moreover, this process pushes data from CPU caches.
By
reference, you can find the test code (on Kotlin: trollface :). The code deserializes the collection of objects from the ByteBuffer. Results are sorted in ascending order.
')

As you can see, deserialization takes more time when you have to create new objects. Sometimes, the difference is quite noticeable. Although, it is not surprising that the creation of new objects means a large overhead and more frequent triggered GC.

This chart shows the worst 5%. The difference is due to GC triggering.