RFR: 8305811: (bf) Improve heap buffer performance of CharBuffer::append(CharSequence)

Brian Burkhalter bpb at openjdk.org
Tue Apr 11 18:18:38 UTC 2023


On Tue, 11 Apr 2023 00:11:50 GMT, Brian Burkhalter <bpb at openjdk.org> wrote:

> Use the `getChars` method of `String`, `StringBuffer`, and `StringBuilder` to load the chars directly into the array of the heap buffer.

A quick rerun of the benchmark gives:

**Before**

Benchmark                              Mode  Cnt        Score       Error  Units
CharBufferAppend.appendString         thrpt    5  3746966.312 ± 83845.495  ops/s
CharBufferAppend.appendStringBuffer   thrpt    5  3758222.865 ± 81606.385  ops/s
CharBufferAppend.appendStringBuilder  thrpt    5  1146190.655 ± 16056.970  ops/s

**After**

s
CharBufferAppend.appendString         thrpt    5  3820766.510 ±  63702.622  ops/s
CharBufferAppend.appendStringBuffer   thrpt    5  3670142.062 ±  39796.749  ops/s
CharBufferAppend.appendStringBuilder  thrpt    5  4469108.687 ± 175344.953  ops/s

which is consistent with previous data. Note that the "before" version of `append(CharSequence)` is equivalent to

    public $Type$Buffer append(CharSequence csq) {
        String src = csq.toString();
        return put(src, 0, src.length());
    }

Internally, `put(String,int,int)` uses `getChars()`. The `toString()` implementations are as

| Class               | toString()                        |
|---------------|-------------------------|
| String              | return this;                      |
| StringBuffer    | return toStringCache;    |
| StringBuilder   | return new String(this); |

The `StringBuilder.toString()` performs a copy of the internal `char`s and this is avoided in the modified code.

-------------

PR Comment: https://git.openjdk.org/jdk/pull/13415#issuecomment-1503872617


More information about the nio-dev mailing list