Am 25.03.2010 22:47, schrieb Martin Buchholz:
Here's another minor performance tweak to
public String(int[] codePoints, int offset, int count) {
that optimizes for BMP.
// Pass 1: Compute precise size of char[]
int n = count;
for (int i = offset; i< end; i++) {
int c = codePoints[i];
if (Character.isBMPCodePoint(c))
;
else if (Character.isSupplementaryCodePoint(c))
n++;
else throw new IllegalArgumentException(Integer.toString(c));
}
Yes, this is a valuable pattern, you found out.
I think, it could look smarter/more clear:
if (Character.isBMPCodePoint(c))
continue;
if (Character.isSupplementaryCodePoint(c))
n++;
else
throw new IllegalArgumentException(Integer.toString(c));
And this would be faster, as isSupplementaryCodePoint is not optimized
for following isBMPCodePoint:
if (Character.isBMPCodePoint(c))
continue;
if (!Character.isValidCodePoint(c))
throw new IllegalArgumentException(Integer.toString(c));
n++;
Before you go to the meeting, maybe scan the JDK for similar use cases,
before I get addicted too, and don't forget to define c as final.
It's enough, that I'm addicted from:
// fill backwards for VM performance reasons, reduces register
pressure, faster compare against 0
for (int i = end; n > 0; ) {
int c = codePoints[--i];
if (Character.isBMPCodePoint(c))
v[--n] = (char)c;
else
Character.toSurrogates(c, v, n-=2);
}
-Ulf
http://cr.openjdk.java.net/~martin/webrevs/openjdk7/public-isBMPCodePoint/
Martin