costin      01/03/22 18:25:33

  Modified:    src/share/org/apache/tomcat/util/collections SimplePool.java
  Log:
  A small ( or big ) improvement to the SimplePool.
  
  Allow creation of a small pool that will grow to max. That will help
  for scalability and reduce memory usage.
  
  Revision  Changes    Path
  1.3       +45 -15    
jakarta-tomcat/src/share/org/apache/tomcat/util/collections/SimplePool.java
  
  Index: SimplePool.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/util/collections/SimplePool.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- SimplePool.java   2001/02/28 19:24:32     1.2
  +++ SimplePool.java   2001/03/23 02:25:32     1.3
  @@ -1,7 +1,7 @@
   /*
  - * $Header: 
/home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/util/collections/SimplePool.java,v
 1.2 2001/02/28 19:24:32 larryi Exp $
  - * $Revision: 1.2 $
  - * $Date: 2001/02/28 19:24:32 $
  + * $Header: 
/home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/util/collections/SimplePool.java,v
 1.3 2001/03/23 02:25:32 costin Exp $
  + * $Revision: 1.3 $
  + * $Date: 2001/03/23 02:25:32 $
    *
    * ====================================================================
    *
  @@ -69,7 +69,7 @@
    * The pool will ignore overflow and return null if empty.
    *
    * @author Gal Shachor
  - * @author Costin
  + * @author Costin Manolache
    */
   public final class SimplePool  {
       /*
  @@ -78,35 +78,52 @@
       private Object pool[];
   
       private int max;
  +    private int last;
       private int current=-1;
  -
  -    Object lock;
  -    public static final int DEFAULT_SIZE=16;
  +    
  +    private Object lock;
  +    public static final int DEFAULT_SIZE=32;
  +    static final int debug=0;
       
       public SimplePool() {
  -     this.max=DEFAULT_SIZE;
  -     pool=new Object[max];
  -     lock=new Object();
  +     this(DEFAULT_SIZE,DEFAULT_SIZE);
       }
   
  -    public SimplePool(int max) {
  +    public SimplePool(int size) {
  +     this(size, size);
  +    }
  +
  +    public SimplePool(int size, int max) {
        this.max=max;
  -     pool=new Object[max];
  +     pool=new Object[size];
  +     this.last=size-1;
        lock=new Object();
       }
   
       public  void set(Object o) {
        put(o);
       }
  +
       /**
        * Add the object to the pool, silent nothing if the pool is full
        */
       public  void put(Object o) {
        synchronized( lock ) {
  -         if( current < (max-1) ) {
  -             current += 1;
  +         if( current < last ) {
  +             current++;
  +             pool[current] = o;
  +            } else if( current < max ) {
  +             // realocate
  +             int newSize=pool.length*2;
  +             if( newSize > max ) newSize=max+1;
  +             Object tmp[]=new Object[newSize];
  +             last=newSize-1;
  +             System.arraycopy( pool, 0, tmp, 0, pool.length);
  +             pool=tmp;
  +             current++;
                pool[current] = o;
  -            }
  +         }
  +         if( debug > 0 ) log("put " + o + " " + current + " " + max );
        }
       }
   
  @@ -120,6 +137,8 @@
                item = pool[current];
                current -= 1;
            }
  +         if( debug > 0 ) 
  +             log("get " + item + " " + current + " " + max);
        }
        return item;
       }
  @@ -129,5 +148,16 @@
        */
       public int getMax() {
        return max;
  +    }
  +
  +    /**
  +     * Number of object in the pool
  +     */
  +    public int getCount() {
  +     return current+1;
  +    }
  +
  +    private void log( String s ) {
  +     System.out.println("SimplePool: " + s );
       }
   }
  
  
  

Reply via email to