1 2 Previous Next 23 Replies Latest reply on Apr 17, 2004 8:05 AM by Steve Lewis

    object pooling detrimental to performance

    Bill Burke Master

      FYI:

      Did some tests in AOP framework with object pooling through a ThreadLocal. I found that it actually slowed down performance by 20%. This finalizes what I've thought all along that Object pooling is worthless and it is better to let the VM handle it.

      Here's what I implemented:

      public class MethodInvocation extends Invocation
      {
       public Object[] arguments = null; // MARSHALLED
       public transient Method method = null;
       public long methodHash;
      
       public MethodInvocation(org.jboss.aop.advice.Interceptor[] interceptors)
       {
       super(InvocationType.METHOD, interceptors);
       }
      
       protected void clear()
       {
       super.clear();
       arguments = null;
       }
      
       public static class Pool
       {
      
       private ThreadLocal pool = new ThreadLocal();
      
       public MethodInvocation getInstance(Interceptor[] interceptors)
       {
       MethodInvocation[] pooled = (MethodInvocation[]) pool.get();
       if (pooled == null)
       {
       System.out.println("returning new invocation");
       return new MethodInvocation(interceptors);
       }
       MethodInvocation instance = null;
       for (int i = pooled.length - 1; i >= 0; i--)
       {
       if (pooled == null) continue;
       instance = pooled;
       pooled = null;
       break;
       }
       if (instance == null)
       {
       System.out.println("returning new invocation");
       return new MethodInvocation(interceptors);
       }
       instance.interceptors = interceptors;
       return instance;
       }
      
       public void releaseInstance(MethodInvocation invocation)
       {
       invocation.clear();
       MethodInvocation[] pooled = (MethodInvocation[]) pool.get();
       if (pooled == null)
       {
       pooled = new MethodInvocation[1];
       pooled[0] = invocation;
       pool.set(pooled);
       return;
       }
       int i = 0;
       for (; i < pooled.length; i++)
       {
       if (pooled == null)
       {
       pooled = invocation;
       return;
       }
       }
       // pool is too big
       MethodInvocation[] newPooled = new MethodInvocation[pooled.length + 1];
       System.arraycopy(pooled, 0, newPooled, 0, pooled.length);
       newPooled[pooled.length] = invocation;
       pool.set(newPooled);
      
       }
       }
      
       }
      


        1 2 Previous Next