Object pooling is a programming technique
where instead of creating new objects to service requests, a small
pool of objects is created and the objects within the pool are reused
repeatedly to service requests instead. This was traditionally used
by Java programs as a performance optimization in an attempt to
reduce memory allocations and therefore reduce the amount of
garbage collection that occurs. On modern
Java virtual machines, however, object pooling
as a means to improve performance in this manner is strongly
contraindicated: Object allocations are extremely fast (on the order
of a few tens of nanoseconds), escape analysis
often eliminates allocations entirely, and modern garbage collectors
are optimized to make short-lived objects essentially free.
With this in mind, it may not be clear why the
com.io7m.jpuddle package
should exist at all! The answer is that object pooling is still
useful when the objects represent external resources that may be
very expensive to acquire and/or the program should avoid acquiring
too many of these resources at any given time. An example of this
sort of use case is allocating short-lived framebuffer objects
on a GPU. Graphics memory is typically in relatively short supply
and creating an object on the GPU is generally considered to be
an expensive and slow process (relative to simply allocating an
object on the CPU side). A pool of framebuffer objects
can be created that the application can reuse repeatedly without
needing to create new objects, and the size of the pool can be
bounded so that the application does not try to exceed the available
GPU memory.