Since my raytracer needs to create a lot of vector objects, I thought it might be a good idea to create a vector pool. Surprisingly, it wasn’t. Or maybe I’m just too stupid.
I tried an ArrayList as pool container and pool access like this (V3 is a math. 3d vector type):
private static final ArrayList <V3> pool = new ArrayList<V3>(8192);
public static V3 make(final V3 other)
{
V3 v= null;
synchronized(pool)
{
if(pool.size() > 0)
{
v= pool.remove(pool.size()-1);
}
}
if(v== null)
{
v= new V3(other);
}
return v;
}
public static void put(final V3 v)
{
synchronized(pool)
{
pool.add(v);
}
}
This turned out to be much slower than just "new()"ing all the objects and rely on the gc to clean up well. Has object pooling become obsolete with modern JVMs? Am I doing it wrong? Is there a sort of pool container which work without synchronization?