[cp-patches] RFC: Try to reduce char[] copying in jlr.Proxy

2008-08-29 Thread Ian Rogers

Hi,

the attached patch is trying to continue Andrew's good work and reduce 
the amount of redundant array copying, in this case in jlr.Proxy. There 
is a TODO in that we have no back door to turn a char[] in to a String 
without copying.


Thanks,
Ian
Index: java/lang/reflect/Proxy.java
===
RCS file: /sources/classpath/classpath/java/lang/reflect/Proxy.java,v
retrieving revision 1.30
diff -u -r1.30 Proxy.java
--- java/lang/reflect/Proxy.java16 Mar 2008 22:04:51 -  1.30
+++ java/lang/reflect/Proxy.java29 Aug 2008 11:01:45 -
@@ -1402,7 +1402,12 @@
 {
   String utf8 = toUtf8(str);
   int len = utf8.length();
-  return poolIndex(\1 + (char) (len  8) + (char) (len  0xff) + utf8);
+  CPStringBuilder buf = new CPStringBuilder(len+3);
+  buf.append('\1');
+  buf.append((char) (len  8));
+  buf.append((char) (len  0xff));
+  buf.append(utf8);
+  return poolIndex(buf.toString());
 }
 
 /**
@@ -1416,7 +1421,7 @@
 {
   char index = utf8Info(name);
   char[] c = {7, (char) (index  8), (char) (index  0xff)};
-  return poolIndex(new String(c));
+  return poolIndex(stringFromCharArray(c));
 }
 
 /**
@@ -1452,7 +1457,7 @@
   char[] c = {(char) (structure + 8),
   (char) (cindex  8), (char) (cindex  0xff),
   (char) (ntindex  8), (char) (ntindex  0xff)};
-  return poolIndex(new String(c));
+  return poolIndex(stringFromCharArray(c));
 }
 
 /**
@@ -1469,7 +1474,7 @@
   char tindex = utf8Info(type);
   char[] c = {12, (char) (nindex  8), (char) (nindex  0xff),
   (char) (tindex  8), (char) (tindex  0xff)};
-  return poolIndex(new String(c));
+  return poolIndex(stringFromCharArray(c));
 }
 
 /**
@@ -1541,5 +1546,19 @@
 }
   return (char) i.intValue();
 }
+
+/**
+ * Return a String wrapping the char array avoiding copying the char array
+ * if possible
+ *
+ * @param c the char array
+ * @return the String
+ */
+private String stringFromCharArray(char[] c)
+{
+  // TODO: we need some back door like VMCPStringBuilder to avoid copying
+  //   the character array in this situation
+  return new String(c);
+}
   } // class ClassFactory
 }


Re: [cp-patches] RFC: Try to reduce char[] copying in jlr.Proxy

2008-08-29 Thread Andrew John Hughes
On 29/08/2008, Ian Rogers [EMAIL PROTECTED] wrote:
 Hi,

  the attached patch is trying to continue Andrew's good work and reduce the
 amount of redundant array copying, in this case in jlr.Proxy. There is a
 TODO in that we have no back door to turn a char[] in to a String without
 copying.

  Thanks,
  Ian


VMCPStringBuilder provides such a backdoor already.  It's just package private.
We could make the class public I guess, or try something like Sun's Secrets.
-- 
Andrew :-)

Support Free Java!
Contribute to GNU Classpath and the OpenJDK
http://www.gnu.org/software/classpath
http://openjdk.java.net

PGP Key: 94EFD9D8 (http://subkeys.pgp.net)
Fingerprint: F8EF F1EA 401E 2E60 15FA  7927 142C 2591 94EF D9D8