Hello,

I was writing software that uses the BCEL library. Having performance
problems, I found out that generic/ConstantPoolGen.java does unnecessary
StringBuffer expansion: in two cases the length of the resulting string is
known beforehand, but the created StringBuffer is not given a starting
length for the internal buffer.

With the attached patch applied, the running time of my application
decreased from 1 min 45 seconds to 1 min 30 seconds as the generation of
new constant pools was faster.

(However, by rewriting my algorithm to do less constant pool
instantiations, the running time dropped to 25 seconds).

br,
Pietu Pohjalainen


In case of the list software dropping attachments, it is here as well:

--- ConstantPoolGen.orig        2004-10-08 09:41:36.061728000 +0300
+++ ConstantPoolGen.java        2004-10-08 09:47:27.363099600 +0300
@@ -117,11 +117,19 @@

        class_table.put(u8.getBytes(), new Index(i));
       } else if(c instanceof ConstantNameAndType) {
-       ConstantNameAndType n    = (ConstantNameAndType)c;
-       ConstantUtf8        u8   = (ConstantUtf8)constants[n.getNameIndex()];
-       ConstantUtf8        u8_2 = (ConstantUtf8)constants[n.getSignatureIndex()
];
+       ConstantNameAndType n     = (ConstantNameAndType)c;
+       ConstantUtf8        u8    = (ConstantUtf8)constants[n.getNameIndex()];
+       String              u8s   = u8.getBytes();
+       ConstantUtf8        u8_2  = (ConstantUtf8)constants[n.getSignatureIndex(
)];
+       String              u8_2s = u8_2.getBytes();
+
+       int len = (u8s.length() + NAT_DELIM.length() + u8_2s.length());
+       StringBuffer buf = new StringBuffer(len);
+       buf.append(u8s);
+       buf.append(NAT_DELIM);
+       buf.append(u8_2s);

-       n_a_t_table.put(u8.getBytes() + NAT_DELIM + u8_2.getBytes(), new Index(i
));
+       n_a_t_table.put(buf.toString(), new Index(i));
        } else if(c instanceof ConstantUtf8) {
          ConstantUtf8 u = (ConstantUtf8)c;

@@ -147,7 +155,17 @@
        else if(c instanceof ConstantFieldref)
          delim = FIELDREF_DELIM;

-       cp_table.put(class_name + delim + method_name + delim + signature, new I
ndex(i));
+       int len = ( class_name.length() + method_name.length() +
+                   signature.length()  + ( 2 * delim.length() ));
+
+       StringBuffer buf = new StringBuffer(len);
+       buf.append(class_name);
+       buf.append(delim);
+       buf.append(method_name);
+       buf.append(delim);
+       buf.append(signature);
+
+       cp_table.put(buf.toString(), new Index(i));
       }
     }
   }
--- ConstantPoolGen.orig        2004-10-08 09:41:36.061728000 +0300
+++ ConstantPoolGen.java        2004-10-08 09:47:27.363099600 +0300
@@ -117,11 +117,19 @@

        class_table.put(u8.getBytes(), new Index(i));
       } else if(c instanceof ConstantNameAndType) {
-       ConstantNameAndType n    = (ConstantNameAndType)c;
-       ConstantUtf8        u8   = (ConstantUtf8)constants[n.getNameIndex()];
-       ConstantUtf8        u8_2 = (ConstantUtf8)constants[n.getSignatureIndex()
];
+       ConstantNameAndType n     = (ConstantNameAndType)c;
+       ConstantUtf8        u8    = (ConstantUtf8)constants[n.getNameIndex()];
+       String              u8s   = u8.getBytes();
+       ConstantUtf8        u8_2  = (ConstantUtf8)constants[n.getSignatureIndex(
)];
+       String              u8_2s = u8_2.getBytes();
+
+       int len = (u8s.length() + NAT_DELIM.length() + u8_2s.length());
+       StringBuffer buf = new StringBuffer(len);
+       buf.append(u8s);
+       buf.append(NAT_DELIM);
+       buf.append(u8_2s);

-       n_a_t_table.put(u8.getBytes() + NAT_DELIM + u8_2.getBytes(), new Index(i
));
+       n_a_t_table.put(buf.toString(), new Index(i));
        } else if(c instanceof ConstantUtf8) {
          ConstantUtf8 u = (ConstantUtf8)c;

@@ -147,7 +155,17 @@
        else if(c instanceof ConstantFieldref)
          delim = FIELDREF_DELIM;

-       cp_table.put(class_name + delim + method_name + delim + signature, new I
ndex(i));
+       int len = ( class_name.length() + method_name.length() +
+                   signature.length()  + ( 2 * delim.length() ));
+
+       StringBuffer buf = new StringBuffer(len);
+       buf.append(class_name);
+       buf.append(delim);
+       buf.append(method_name);
+       buf.append(delim);
+       buf.append(signature);
+
+       cp_table.put(buf.toString(), new Index(i));
       }
     }
   }

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to