[PATCH] Performance bug in String(byte[],int,int,Charset)
Markus Gaisbauer
markus.gaisbauer at gmail.com
Fri Nov 23 21:58:45 UTC 2007
A bug in java.lang.StringCoding causes a full and unnecessary copy of
the byte array given as the first argument.
This results in severe slow down of the Constructor if the byte array is
big.
The attached patch, should fix the problem.
Unfortunately I do not (yet) have an official bug id for this, as this
seems to take a while (reported 2 weeks ago).
To reproduce the problem run the following test program:
import java.nio.charset.Charset;
public class StringTest {
public static void main(String[] args) throws Exception {
long before;
long after;
byte[] data;
data = new byte[1024*1024*16]; // 16 megabyte
data[0] = 'X';
// warmup
new String(data, 0, 1);
new String(data, 0, 1, Charset.forName("UTF8"));
new String(data, 0, 1, "UTF8");
before = System.nanoTime();
new String(data, 0, 1);
after = System.nanoTime();
System.out.println((after - before) / 1000000 + "ms");
before = System.nanoTime();
new String(data, 0, 1, Charset.forName("UTF8"));
after = System.nanoTime();
System.out.println((after - before) / 1000000 + "ms");
before = System.nanoTime();
new String(data, 0, 1, "UTF8");
after = System.nanoTime();
System.out.println((after - before) / 1000000 + "ms");
}
}
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: StringCoding.diff
URL: <http://mail.openjdk.java.net/pipermail/core-libs-dev/attachments/20071123/029c10e7/StringCoding.diff>
More information about the core-libs-dev
mailing list