by half-kh-hacker · 3y ago · 0 comments
But then the new APIs in JDK itself are designed such that you have to allocate loads of short-lived small objects. I was told that HotSpot does deal with them reasonably well to avoid them degrading the performance, but apparently it isn't very good at it?
So while it makes sense to avoid unnecessary allocations by using different APIs (e.g. not creating Streams in hot paths), pooling brings far more new problems with it. It might make sense for large objects, but generally requires in-depth analysis to make sure it actually helps.
Also, when plugins come into the equation, you need to make sure that those can't modify objects they aren't meant to modify, which involves copying of objects. Additionally, some objects have different representations in the API (what's used by plugins) vs the implementation (what's used by vanilla minecraft), so converting between those representations is another source of allocations.
No idea how to do it in java but a few pointers ought to be enough. You can also omit clearing the memory area between allocations for things that aren't security sensitive, if that is done in java, which I would assume.
There definitely are scenarios where pooling might make sense, but basically the low hanging fruits in that area in minecraft are already reaped.
I worked on optimising a java library a few years back and one of the bigger speed ups was removing all the object pooling code.
1. by using finalizers
2. by using object initialization blocks
If you avoid these two problems you get nano-second performance, because the JVM does not need to run code during object creation and GC.