[jboss-cvs] javassist/src/main/javassist ...

Shigeru Chiba chiba at is.titech.ac.jp
Tue Jul 3 11:31:19 EDT 2007


  User: chiba   
  Date: 07/07/03 11:31:19

  Modified:    src/main/javassist    CtClassType.java CtClass.java
                        ClassPool.java
  Log:
  for reducing memory footprint
  
  Revision  Changes    Path
  1.60      +31 -45    javassist/src/main/javassist/CtClassType.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: CtClassType.java
  ===================================================================
  RCS file: /cvsroot/jboss/javassist/src/main/javassist/CtClassType.java,v
  retrieving revision 1.59
  retrieving revision 1.60
  diff -u -b -r1.59 -r1.60
  --- CtClassType.java	26 Jun 2007 19:01:12 -0000	1.59
  +++ CtClassType.java	3 Jul 2007 15:31:19 -0000	1.60
  @@ -25,7 +25,6 @@
   import java.io.InputStream;
   import java.net.URL;
   import java.util.ArrayList;
  -import java.util.Enumeration;
   import java.util.HashMap;
   import java.util.Hashtable;
   import java.util.List;
  @@ -73,10 +72,8 @@
       private int uniqueNumberSeed;
   
       private boolean doPruning = ClassPool.doPruning;
  -    int getCounter;
  -    private static int readCounter = 0;
  -    private static final int READ_THRESHOLD = 100;  // see getClassFile2()
  -    private static final int GET_THRESHOLD = 2;     // see releaseClassFiles()
  +    private int getCount;
  +    private static final int GET_THRESHOLD = 2;     // see compress()
   
       CtClassType(String name, ClassPool cp) {
           super(name);
  @@ -89,7 +86,7 @@
           fieldInitializers = null;
           hiddenMethods = null;
           uniqueNumberSeed = 0;
  -        getCounter = 0;
  +        getCount = 0;
       }
   
       CtClassType(InputStream ins, ClassPool cp) throws IOException {
  @@ -169,17 +166,13 @@
           if (cfile != null)
               return cfile;
   
  -        if (readCounter++ > READ_THRESHOLD) {
  -            releaseClassFiles();
  -            readCounter = 0;
  -        }
  -
  +        classPool.compress();
           if (rawClassfile != null) {
               try {
                   classfile = new ClassFile(new DataInputStream(
                                               new ByteArrayInputStream(rawClassfile)));
                   rawClassfile = null;
  -                getCounter = GET_THRESHOLD;
  +                getCount = GET_THRESHOLD;
                   return classfile;
               }
               catch (IOException e) {
  @@ -218,11 +211,33 @@
           }
       }
   
  +   /* Inherited from CtClass.  Called by get() in ClassPool.
  +    *
  +    * @see javassist.CtClass#incGetCounter()
  +    * @see #toBytecode(DataOutputStream)
  +    */
  +   final void incGetCounter() { ++getCount; }
  +
  +   /**
  +    * Invoked from ClassPool#compress().
  +    * It releases the class files that have not been recently used
  +    * if they are unmodified. 
  +    */
  +   void compress() {
  +       if (getCount < GET_THRESHOLD)
  +           if (!isModified() && ClassPool.releaseUnmodifiedClassFile)
  +               removeClassFile();
  +           else if (isFrozen() && !wasPruned)
  +               saveClassFile();
  +
  +       getCount = 0;
  +   }
  +
       /**
        * Converts a ClassFile object into a byte array
        * for saving memory space.
        */
  -    public synchronized void saveClassFile() {
  +    private synchronized void saveClassFile() {
           /* getMembers() and releaseClassFile() are also synchronized.
            */
           if (classfile == null || hasMemberCache() != null)
  @@ -239,40 +254,11 @@
           catch (IOException e) {}
       }
   
  -    public synchronized void releaseClassFile() {
  +    private synchronized void removeClassFile() {
           if (classfile != null && !isModified() && hasMemberCache() == null)
               classfile = null;
       }
   
  -    /* Inherited from CtClass.  Called by get() in ClassPool.
  -     *
  -     * @see javassist.CtClass#incGetCounter()
  -     * @see #toBytecode(DataOutputStream)
  -     */
  -    void incGetCounter() { ++getCounter; }
  -
  -    /**
  -     * Releases the class files
  -     * of the CtClasses that have not been recently used
  -     * if they are unmodified. 
  -     */
  -    public void releaseClassFiles() {
  -        Enumeration e = classPool.classes.elements();
  -        while (e.hasMoreElements()) {
  -            Object obj = e.nextElement();
  -            if (obj instanceof CtClassType) {
  -                CtClassType cct = (CtClassType)obj;
  -                if (cct.getCounter < GET_THRESHOLD)
  -                    if (!cct.isModified() && ClassPool.releaseUnmodifiedClassFile)
  -                        cct.releaseClassFile();
  -                    else if (cct.isFrozen() && !cct.wasPruned)
  -                        cct.saveClassFile();
  -
  -                cct.getCounter = 0;
  -            }
  -        }
  -    }
  -
       public ClassPool getClassPool() { return classPool; }
   
       void setClassPool(ClassPool cp) { classPool = cp; }
  @@ -1344,7 +1330,7 @@
                   // classfile = null;
               }
   
  -            getCounter = 0;
  +            getCount = 0;
               wasFrozen = true;
           }
           catch (NotFoundException e) {
  
  
  
  1.88      +5 -0      javassist/src/main/javassist/CtClass.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: CtClass.java
  ===================================================================
  RCS file: /cvsroot/jboss/javassist/src/main/javassist/CtClass.java,v
  retrieving revision 1.87
  retrieving revision 1.88
  diff -u -b -r1.87 -r1.88
  --- CtClass.java	18 Jun 2007 23:51:07 -0000	1.87
  +++ CtClass.java	3 Jul 2007 15:31:19 -0000	1.88
  @@ -1330,4 +1330,9 @@
       public String makeUniqueName(String prefix) {
           throw new RuntimeException("not available in " + getName());
       }
  +
  +    /* Invoked from ClassPool#compress().
  +     * This method is overridden by CtClassType.
  +     */
  +    void compress() {}
   }
  
  
  
  1.64      +19 -0     javassist/src/main/javassist/ClassPool.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: ClassPool.java
  ===================================================================
  RCS file: /cvsroot/jboss/javassist/src/main/javassist/ClassPool.java,v
  retrieving revision 1.63
  retrieving revision 1.64
  diff -u -b -r1.63 -r1.64
  --- ClassPool.java	8 Jun 2007 13:32:10 -0000	1.63
  +++ ClassPool.java	3 Jul 2007 15:31:19 -0000	1.64
  @@ -29,6 +29,7 @@
   import java.util.Hashtable;
   import java.util.Iterator;
   import java.util.ArrayList;
  +import java.util.Enumeration;
   import javassist.bytecode.Descriptor;
   
   /**
  @@ -119,6 +120,9 @@
        */
       public static boolean doPruning = false;
   
  +    private int compressCount;
  +    private static final int COMPRESS_THRESHOLD = 100;
  +
       /* releaseUnmodifiedClassFile was introduced for avoiding a bug
          of JBoss AOP.  So the value should be true except for JBoss AOP.
        */
  @@ -184,6 +188,7 @@
           }
   
           this.cflow = null;
  +        this.compressCount = 0;
           clearImportedPackages();
       }
   
  @@ -263,6 +268,19 @@
       }
   
       /**
  +     * This method is periodically invoked so that memory
  +     * footprint will be minimized.
  +     */
  +    void compress() {
  +        if (compressCount++ > COMPRESS_THRESHOLD) {
  +            compressCount = 0;
  +            Enumeration e = classes.elements();
  +            while (e.hasMoreElements())
  +                ((CtClass)e.nextElement()).compress();
  +        }
  +    }
  +
  +    /**
        * Record a package name so that the Javassist compiler searches
        * the package to resolve a class name.
        * Don't record the <code>java.lang</code> package, which has
  @@ -607,6 +625,7 @@
       public CtClass makeClass(InputStream classfile, boolean ifNotFrozen)
           throws IOException, RuntimeException
       {
  +        compress();
           classfile = new BufferedInputStream(classfile);
           CtClass clazz = new CtClassType(classfile, this);
           clazz.checkModify();
  
  
  



More information about the jboss-cvs-commits mailing list