Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 32 additions & 20 deletions src/java.base/share/classes/java/lang/AbstractStringBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
*/
abstract sealed class AbstractStringBuilder implements Appendable, CharSequence
permits StringBuilder, StringBuffer {

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

Redundant line.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This extra line existed before this PR

/**
* The value is used for character storage.
*/
Expand Down Expand Up @@ -635,18 +636,21 @@ public AbstractStringBuilder append(CharSequence s) {
}

private AbstractStringBuilder appendNull() {
ensureCapacityInternal(count + 4);
int count = this.count;
ensureCapacityInternal(count + 4);
byte[] val = this.value;
if (isLatin1()) {
val[count++] = 'n';
val[count++] = 'u';
val[count++] = 'l';
val[count++] = 'l';
val[count] = 'n';
val[count + 1] = 'u';
val[count + 2] = 'l';
val[count + 3] = 'l';
} else {
count = StringUTF16.putCharsAt(val, count, 'n', 'u', 'l', 'l');
StringUTF16.putChar(val, count, 'n');
StringUTF16.putChar(val, count + 1, 'u');
StringUTF16.putChar(val, count + 2, 'l');
StringUTF16.putChar(val, count + 3, 'l');
}
this.count = count;
this.count = count + 4;
return this;
}

Expand Down Expand Up @@ -766,30 +770,38 @@ public AbstractStringBuilder append(char[] str, int offset, int len) {
* @return a reference to this object.
*/
public AbstractStringBuilder append(boolean b) {
ensureCapacityInternal(count + (b ? 4 : 5));
int count = this.count;
int spaceNeeded = count + (b ? 4 : 5);
ensureCapacityInternal(spaceNeeded);
byte[] val = this.value;
if (isLatin1()) {
if (b) {
val[count++] = 't';
val[count++] = 'r';
val[count++] = 'u';
val[count++] = 'e';
val[count] = 't';
val[count + 1] = 'r';
val[count + 2] = 'u';
val[count + 3] = 'e';
} else {
val[count++] = 'f';
val[count++] = 'a';
val[count++] = 'l';
val[count++] = 's';
val[count++] = 'e';
val[count] = 'f';
val[count + 1] = 'a';
val[count + 2] = 'l';
val[count + 3] = 's';
val[count + 4] = 'e';
}
} else {
if (b) {
count = StringUTF16.putCharsAt(val, count, 't', 'r', 'u', 'e');
StringUTF16.putChar(val, count, 't');
StringUTF16.putChar(val, count + 1, 'r');
StringUTF16.putChar(val, count + 2, 'u');
StringUTF16.putChar(val, count + 3, 'e');
} else {
count = StringUTF16.putCharsAt(val, count, 'f', 'a', 'l', 's', 'e');
StringUTF16.putChar(val, count, 'f');
StringUTF16.putChar(val, count + 1, 'a');
StringUTF16.putChar(val, count + 2, 'l');
StringUTF16.putChar(val, count + 3, 's');
StringUTF16.putChar(val, count + 4, 'e');
}
}
this.count = count;
this.count = spaceNeeded;
return this;
}

Expand Down
23 changes: 0 additions & 23 deletions src/java.base/share/classes/java/lang/StringUTF16.java
Original file line number Diff line number Diff line change
Expand Up @@ -1552,29 +1552,6 @@ public static boolean contentEquals(byte[] value, CharSequence cs, int len) {
return true;
}

public static int putCharsAt(byte[] value, int i, char c1, char c2, char c3, char c4) {
int end = i + 4;
checkBoundsBeginEnd(i, end, value);
putChar(value, i++, c1);
putChar(value, i++, c2);
putChar(value, i++, c3);
putChar(value, i++, c4);
assert(i == end);
return end;
}

public static int putCharsAt(byte[] value, int i, char c1, char c2, char c3, char c4, char c5) {
int end = i + 5;
checkBoundsBeginEnd(i, end, value);
putChar(value, i++, c1);
putChar(value, i++, c2);
putChar(value, i++, c3);
putChar(value, i++, c4);
putChar(value, i++, c5);
assert(i == end);
return end;
}

public static char charAt(byte[] value, int index) {
checkIndex(index, value);
return getChar(value, index);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,6 @@ public static void main(String[] args) throws Exception {
throw new AssertionError("append");
}

putCharsAt(val2, -1, '1', '2', '3', '4');
putCharsAt(val2, 0, '1', '2', '3', '4');
putCharsAt(val2, 2, '1', '2', '3', '4');
putCharsAt(val2, -1, '1', '2', '3', '4', '5');
putCharsAt(val2, 0, '1', '2', '3', '4', '5');
putCharsAt(val2, 2, '1', '2', '3', '4', '5');

reverse(valHigh, -1);
reverse(valHigh, 2);
reverse(valLow, -1);
Expand Down Expand Up @@ -248,22 +241,6 @@ static void contentEquals(byte[] v, CharSequence cs, int len) {
}
}

static void putCharsAt(byte[] v, int i, char c1, char c2, char c3, char c4) {
try {
Helper.putCharsAt(v, i, c1, c2, c3, c4);
throw new AssertionError("putCharsAt");
} catch (IndexOutOfBoundsException io) {
}
}

static void putCharsAt(byte[] v, int i, char c1, char c2, char c3, char c4, char c5) {
try {
Helper.putCharsAt(v, i, c1, c2, c3, c4, c5);
throw new AssertionError("putCharsAt");
} catch (IndexOutOfBoundsException io) {
}
}

static void reverse(byte[] v, int len) {
try {
Helper.reverse(v, len);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,6 @@ public static boolean contentEquals(byte[] value, CharSequence cs, int len) {
return StringUTF16.contentEquals(value, cs, len);
}

public static int putCharsAt(byte[] value, int i, char c1, char c2, char c3, char c4) {
return StringUTF16.putCharsAt(value, i, c1, c2, c3, c4);
}

public static int putCharsAt(byte[] value, int i, char c1, char c2, char c3, char c4, char c5) {
return StringUTF16.putCharsAt(value, i, c1, c2, c3, c4, c5);
}

public static char charAt(byte[] value, int index) {
return StringUTF16.charAt(value, index);
}
Expand Down
47 changes: 47 additions & 0 deletions test/micro/org/openjdk/bench/java/lang/StringBuilders.java
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,53 @@ public String toStringCharWithBool8() {
}


@Benchmark
public String toStringCharUTF16WithBool8() {
StringBuilder result = new StringBuilder()
.append('\u6e29');
result.append(true);
result.append(false);
result.append(true);
result.append(true);
result.append(false);
result.append(true);
result.append(false);
result.append(false);
return result.toString();
}


@Benchmark
public String toStringCharWithNull8() {
StringBuilder result = new StringBuilder();
result.append((String) null);
result.append((String) null);
result.append((String) null);
result.append((String) null);
result.append((String) null);
result.append((String) null);
result.append((String) null);
result.append((String) null);
return result.toString();
}


@Benchmark
public String toStringCharUTF16WithNull8() {
StringBuilder result = new StringBuilder()
.append('\u6e29');
result.append((String) null);
result.append((String) null);
result.append((String) null);
result.append((String) null);
result.append((String) null);
result.append((String) null);
result.append((String) null);
result.append((String) null);
return result.toString();
}


@Benchmark
public String toStringCharWithFloat8() {
StringBuilder result = new StringBuilder();
Expand Down