This is the mail archive of the
java-patches@gcc.gnu.org
mailing list for the Java project.
Patch: Updated `String +' patch
- From: Tom Tromey <tromey at redhat dot com>
- To: Gcc Patch List <gcc-patches at gcc dot gnu dot org>
- Cc: Java Patch List <java-patches at gcc dot gnu dot org>
- Date: 10 Dec 2001 22:17:41 -0700
- Subject: Patch: Updated `String +' patch
- Reply-to: tromey at redhat dot com
This patch changes gcj to use a private, unsychronized copy of
StringBuffer. This speeds up String `+' operations quite a bit.
I tested this by rebuilding libgcj with it.
I also ran performance tests which I posted a while back.
Ok to commit?
Tom
Index: gcc/java/ChangeLog
from Tom Tromey <tromey@redhat.com>
* lex.c (java_init_lex): Define wfl_to_string as
gnu.gcj.runtime.StringBuffer unless generating bytecode.
Index: gcc/java/lex.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/java/lex.c,v
retrieving revision 1.75
diff -u -r1.75 lex.c
--- gcc/java/lex.c 2001/12/09 05:06:45 1.75
+++ gcc/java/lex.c 2001/12/11 05:08:55
@@ -118,7 +118,10 @@
wfl_append = build_expr_wfl (get_identifier ("append"), NULL, 0, 0);
if (!wfl_string_buffer)
wfl_string_buffer =
- build_expr_wfl (get_identifier ("java.lang.StringBuffer"), NULL, 0, 0);
+ build_expr_wfl (get_identifier (flag_emit_class_files
+ ? "java.lang.StringBuffer"
+ : "gnu.gcj.runtime.StringBuffer"),
+ NULL, 0, 0);
if (!wfl_to_string)
wfl_to_string = build_expr_wfl (get_identifier ("toString"), NULL, 0, 0);
Index: libjava/ChangeLog
from Tom Tromey <tromey@redhat.com>
* java/lang/String.java (String): New constructor.
* gnu/gcj/runtime/natStringBuffer.cc: New file.
* gnu/gcj/runtime/StringBuffer.java: New file.
* Makefile.in: Rebuilt.
* Makefile.am (ordinary_java_source_files): Added
gnu/gcj/runtime/StringBuffer.java.
(nat_source_files): Added gnu/gcj/runtime/natStringBuffer.cc.
Index: libjava/Makefile.am
===================================================================
RCS file: /cvs/gcc/gcc/libjava/Makefile.am,v
retrieving revision 1.187
diff -u -r1.187 Makefile.am
--- libjava/Makefile.am 2001/12/10 00:30:24 1.187
+++ libjava/Makefile.am 2001/12/11 05:09:05
@@ -1257,6 +1257,7 @@
gnu/gcj/runtime/FirstThread.java \
gnu/gcj/runtime/JNIWeakRef.java \
gnu/gcj/runtime/SharedLibLoader.java \
+gnu/gcj/runtime/StringBuffer.java \
gnu/gcj/runtime/VMClassLoader.java \
gnu/java/io/ClassLoaderObjectInputStream.java \
gnu/java/io/NullOutputStream.java \
@@ -1617,6 +1618,7 @@
gnu/gcj/runtime/natFinalizerThread.cc \
gnu/gcj/runtime/natFirstThread.cc \
gnu/gcj/runtime/natSharedLibLoader.cc \
+gnu/gcj/runtime/natStringBuffer.cc \
java/io/natFile.cc \
java/io/natFileDescriptor.cc \
java/io/natObjectInputStream.cc \
Index: libjava/Makefile.in
===================================================================
RCS file: /cvs/gcc/gcc/libjava/Makefile.in,v
retrieving revision 1.199
diff -u -r1.199 Makefile.in
--- libjava/Makefile.in 2001/12/10 00:30:24 1.199
+++ libjava/Makefile.in 2001/12/11 05:09:07
@@ -1002,6 +1002,7 @@
gnu/gcj/runtime/FirstThread.java \
gnu/gcj/runtime/JNIWeakRef.java \
gnu/gcj/runtime/SharedLibLoader.java \
+gnu/gcj/runtime/StringBuffer.java \
gnu/gcj/runtime/VMClassLoader.java \
gnu/java/io/ClassLoaderObjectInputStream.java \
gnu/java/io/NullOutputStream.java \
@@ -1361,6 +1362,7 @@
gnu/gcj/runtime/natFinalizerThread.cc \
gnu/gcj/runtime/natFirstThread.cc \
gnu/gcj/runtime/natSharedLibLoader.cc \
+gnu/gcj/runtime/natStringBuffer.cc \
java/io/natFile.cc \
java/io/natFileDescriptor.cc \
java/io/natObjectInputStream.cc \
@@ -1524,7 +1526,8 @@
gnu/gcj/convert/natOutput_SJIS.lo gnu/gcj/io/natSimpleSHSStream.lo \
gnu/gcj/io/shs.lo gnu/gcj/protocol/core/natCoreInputStream.lo \
gnu/gcj/runtime/natFinalizerThread.lo gnu/gcj/runtime/natFirstThread.lo \
-gnu/gcj/runtime/natSharedLibLoader.lo java/io/natFile.lo \
+gnu/gcj/runtime/natSharedLibLoader.lo \
+gnu/gcj/runtime/natStringBuffer.lo java/io/natFile.lo \
java/io/natFileDescriptor.lo java/io/natObjectInputStream.lo \
java/io/natObjectOutputStream.lo java/lang/natCharacter.lo \
java/lang/natClass.lo java/lang/natClassLoader.lo \
@@ -1643,10 +1646,12 @@
.deps/gnu/gcj/runtime/FinalizerThread.P \
.deps/gnu/gcj/runtime/FirstThread.P .deps/gnu/gcj/runtime/JNIWeakRef.P \
.deps/gnu/gcj/runtime/SharedLibLoader.P \
+.deps/gnu/gcj/runtime/StringBuffer.P \
.deps/gnu/gcj/runtime/VMClassLoader.P \
.deps/gnu/gcj/runtime/natFinalizerThread.P \
.deps/gnu/gcj/runtime/natFirstThread.P \
-.deps/gnu/gcj/runtime/natSharedLibLoader.P .deps/gnu/gcj/xlib/Clip.P \
+.deps/gnu/gcj/runtime/natSharedLibLoader.P \
+.deps/gnu/gcj/runtime/natStringBuffer.P .deps/gnu/gcj/xlib/Clip.P \
.deps/gnu/gcj/xlib/Colormap.P .deps/gnu/gcj/xlib/Display.P \
.deps/gnu/gcj/xlib/Drawable.P .deps/gnu/gcj/xlib/Font.P \
.deps/gnu/gcj/xlib/GC.P .deps/gnu/gcj/xlib/Pixmap.P \
Index: libjava/gnu/gcj/runtime/StringBuffer.java
===================================================================
RCS file: StringBuffer.java
diff -N StringBuffer.java
--- /dev/null Tue May 5 13:32:27 1998
+++ libjava/gnu/gcj/runtime/StringBuffer.java Mon Dec 10 21:09:09 2001
@@ -0,0 +1,237 @@
+// This is a simplified copy of java.lang.StringBuffer with
+// `synchronized' removed.
+
+/* StringBuffer.java -- Growable strings
+ Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+02111-1307 USA.
+
+As a special exception, if you link this library with other files to
+produce an executable, this library does not by itself cause the
+resulting executable to be covered by the GNU General Public License.
+This exception does not however invalidate any other reasons why the
+executable file might be covered by the GNU General Public License. */
+
+package gnu.gcj.runtime;
+
+public final class StringBuffer
+{
+ /** Append the <code>String</code> value of the argument to this <code>StringBuffer</code>.
+ * Uses <code>String.valueOf()</code> to convert to
+ * <code>String</code>.
+ * @param bool the <code>boolean</code> to convert and append.
+ * @return this <code>StringBuffer</code>.
+ * @see java.lang.String#valueOf(boolean)
+ */
+ public StringBuffer append (boolean bool)
+ {
+ return append (String.valueOf(bool));
+ }
+
+ /** Append the <code>char</code> to this <code>StringBuffer</code>.
+ * @param c the <code>char</code> to append.
+ * @return this <code>StringBuffer</code>.
+ */
+ public StringBuffer append (char ch)
+ {
+ ensureCapacity_unsynchronized (count + 1);
+ value[count++] = ch;
+ return this;
+ }
+
+ /** Append the <code>String</code> value of the argument to this <code>StringBuffer</code>.
+ * Uses <code>String.valueOf()</code> to convert to
+ * <code>String</code>.
+ * @param inum the <code>int</code> to convert and append.
+ * @return this <code>StringBuffer</code>.
+ * @see java.lang.String#valueOf(int)
+ */
+ public native StringBuffer append (int inum);
+
+ /** Append the <code>String</code> value of the argument to this <code>StringBuffer</code>.
+ * Uses <code>String.valueOf()</code> to convert to
+ * <code>String</code>.
+ * @param lnum the <code>long</code> to convert and append.
+ * @return this <code>StringBuffer</code>.
+ * @see java.lang.String#valueOf(long)
+ */
+ public StringBuffer append (long lnum)
+ {
+ return append (String.valueOf(lnum));
+ }
+
+ /** Append the <code>String</code> value of the argument to this <code>StringBuffer</code>.
+ * Uses <code>String.valueOf()</code> to convert to
+ * <code>String</code>.
+ * @param fnum the <code>float</code> to convert and append.
+ * @return this <code>StringBuffer</code>.
+ * @see java.lang.String#valueOf(float)
+ */
+ public StringBuffer append (float fnum)
+ {
+ return append (String.valueOf(fnum));
+ }
+
+ /** Append the <code>String</code> value of the argument to this <code>StringBuffer</code>.
+ * Uses <code>String.valueOf()</code> to convert to
+ * <code>String</code>.
+ * @param dnum the <code>double</code> to convert and append.
+ * @return this <code>StringBuffer</code>.
+ * @see java.lang.String#valueOf(double)
+ */
+ public StringBuffer append (double dnum)
+ {
+ return append (String.valueOf(dnum));
+ }
+
+ /** Append the <code>String</code> value of the argument to this <code>StringBuffer</code>.
+ * Uses <code>String.valueOf()</code> to convert to
+ * <code>String</code>.
+ * @param obj the <code>Object</code> to convert and append.
+ * @return this <code>StringBuffer</code>.
+ * @see java.lang.String#valueOf(java.lang.Object)
+ */
+ public StringBuffer append (Object obj)
+ {
+ return append (String.valueOf(obj));
+ }
+
+ /** Append the <code>String</code> to this <code>StringBuffer</code>.
+ * @param str the <code>String</code> to append.
+ * @return this <code>StringBuffer</code>.
+ */
+ public StringBuffer append (String str)
+ {
+ if (str == null)
+ str = "null";
+ int len = str.length();
+ ensureCapacity_unsynchronized (count + len);
+ str.getChars(0, len, value, count);
+ count += len;
+ return this;
+ }
+
+ /** Append the <code>char</code> array to this <code>StringBuffer</code>.
+ * @param data the <code>char[]</code> to append.
+ * @return this <code>StringBuffer</code>.
+ * @exception NullPointerException if <code>str</code> is <code>null</code>.
+ */
+ public StringBuffer append (char[] data)
+ {
+ return append (data, 0, data.length);
+ }
+
+ /** Append the <code>char</code> array to this <code>StringBuffer</code>.
+ * @param data the <code>char[]</code> to append.
+ * @param offset the place to start grabbing characters from
+ * <code>str</code>.
+ * @param count the number of characters to get from <code>str</code>.
+ * @return this <code>StringBuffer</code>.
+ * @exception NullPointerException if <code>str</code> is <code>null</code>.
+ * @exception IndexOutOfBoundsException if <code>offset</code> or
+ * <code>offset+len</code> is out of range.
+ */
+ public StringBuffer append (char[] data, int offset, int count)
+ {
+ ensureCapacity_unsynchronized (this.count + count);
+ System.arraycopy(data, offset, value, this.count, count);
+ this.count += count;
+ return this;
+ }
+
+ // ensureCapacity is used by several synchronized methods in StringBuffer.
+ // There's no need to synchronize again.
+ private void ensureCapacity_unsynchronized (int minimumCapacity)
+ {
+ if (shared || minimumCapacity > value.length)
+ {
+ // We don't want to make a larger vector when `shared' is
+ // set. If we do, then setLength becomes very inefficient
+ // when repeatedly reusing a StringBuffer in a loop.
+ int max = (minimumCapacity > value.length
+ ? value.length*2+2
+ : value.length);
+ minimumCapacity = (minimumCapacity < max ? max : minimumCapacity);
+ char[] nb = new char[minimumCapacity];
+ System.arraycopy(value, 0, nb, 0, count);
+ value = nb;
+ shared = false;
+ }
+ }
+
+ /** Create a new StringBuffer with default capacity 16.
+ * @see JLS 20.13.1
+ */
+ public StringBuffer ()
+ {
+ this (DEFAULT_CAPACITY);
+ }
+
+ /** Create an empty <code>StringBuffer</code> with the specified initial capacity.
+ * @param capacity the initial capacity.
+ */
+ public StringBuffer (int capacity)
+ {
+ count = 0;
+ value = new char[capacity];
+ shared = false;
+ }
+
+ /** Create a new <code>StringBuffer</code> with the characters in the specified <code>String</code>.
+ * Initial capacity will be the size of the String plus 16.
+ * @param str the <code>String</code> to make a <code>StringBuffer</code> out of.
+ * @XXX optimize for sharing.
+ */
+ public StringBuffer (String str)
+ {
+ // The documentation is not clear, but experimentation with
+ // other implementations indicates that StringBuffer(null)
+ // should throw a NullPointerException.
+ count = str.length();
+ // JLS: The initial capacity of the string buffer is 16 plus the
+ // length of the argument string.
+ value = new char[count + DEFAULT_CAPACITY];
+ str.getChars(0, count, value, 0);
+ shared = false;
+ }
+
+ /** Convert this <code>StringBuffer</code> to a <code>String</code>.
+ * @return the characters in this StringBuffer
+ */
+ // This is native because efficient implementation requires avoiding
+ // the Java protection mechanism.
+ public native String toString ();
+
+ // Index of next available character. Note that this has
+ // permissions set this way so that String can get the value.
+ int count;
+
+ // The buffer. Note that this has permissions set this way so that
+ // String can get the value.
+ char[] value;
+
+ // True if we need to copy the buffer before writing to it again.
+ // FIXME: JDK 1.2 doesn't specify this. The new buffer-growing
+ // semantics make this less useful in that case, too. Note that
+ // this has permissions set this way so that String can get the
+ // value.
+ boolean shared;
+
+ static final long serialVersionUID = 3388685877147921107L;
+ private final static int DEFAULT_CAPACITY = 16; // JLS 20.13.1
+}
Index: libjava/gnu/gcj/runtime/natStringBuffer.cc
===================================================================
RCS file: natStringBuffer.cc
diff -N natStringBuffer.cc
--- /dev/null Tue May 5 13:32:27 1998
+++ libjava/gnu/gcj/runtime/natStringBuffer.cc Mon Dec 10 21:09:09 2001
@@ -0,0 +1,36 @@
+// natStringBuffer.cc - Implementation of java.lang.StringBuffer native methods.
+
+/* Copyright (C) 2001 Free Software Foundation
+
+ This file is part of libgcj.
+
+This software is copyrighted work licensed under the terms of the
+Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
+details. */
+
+#include <config.h>
+#include <gcj/cni.h>
+#include <gnu/gcj/runtime/StringBuffer.h>
+#include <java/lang/String.h>
+
+gnu::gcj::runtime::StringBuffer *
+gnu::gcj::runtime::StringBuffer::append (jint num)
+{
+ // Use an array large enough for "-2147483648"; i.e. 11 chars.
+ jchar buffer[11];
+ int i = _Jv_FormatInt (buffer+11, num);
+ jint needed = count + i;
+ ensureCapacity_unsynchronized (needed);
+ jchar* dst = elements (value) + count;
+ jchar* src = buffer+11-i;
+ while (--i >= 0)
+ *dst++ = *src++;
+ count = needed;
+ return this;
+}
+
+java::lang::String *
+gnu::gcj::runtime::StringBuffer::toString ()
+{
+ return new java::lang::String (this);
+}
Index: libjava/java/lang/String.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/lang/String.java,v
retrieving revision 1.16
diff -u -r1.16 String.java
--- libjava/java/lang/String.java 2001/10/02 20:59:31 1.16
+++ libjava/java/lang/String.java 2001/12/11 05:09:10
@@ -92,6 +92,16 @@
}
}
+ // This is used by gnu.gcj.runtime.StringBuffer, so it must have
+ // package-private protection. It is accessed via CNI and so avoids
+ // ordinary protection mechanisms.
+ String (gnu.gcj.runtime.StringBuffer buffer)
+ {
+ // No need to synchronize or mark the buffer, since we know it is
+ // only used once.
+ init (buffer.value, 0, buffer.count, true);
+ }
+
public String (char[] data)
{
init(data, 0, data.length, false);