RFR: 8358533: Improve performance of java.io.Reader.readAllLines [v8]
Xueming Shen
sherman at openjdk.org
Mon Jun 30 18:32:40 UTC 2025
On Fri, 27 Jun 2025 19:41:01 GMT, Brian Burkhalter <bpb at openjdk.org> wrote:
>> Replaces the implementation `readAllCharsAsString().lines().toList()` with reading into a temporary `char` array which is then processed to detect line terminators and copy non-terminating characters into strings which are added to the list.
>
> Brian Burkhalter has updated the pull request incrementally with one additional commit since the last revision:
>
> 8358533: Make no line terminator case clearer
seems i was wrong about "consolidate two nested parsing loops" :-) the throughput appears to back to normal with 'find term while loop' fast path .
int start = 0;
int pos = 0;
int limit = 0;
boolean skipLF = false;
int n;
while ((n = read(cb, pos, cb.length - pos)) != -1) {
limit = pos + n;
while (pos < limit) {
if (skipLF) {
if (cb[pos] == '\n') {
pos++;
start++;
}
skipLF = false;
}
while (pos < limit) {
char c = cb[pos++];
if (c == '\n' || c == '\r') {
lines.add(new String(cb, start, pos - 1 - start));
skipLF = (c == '\r');
start = pos;
break;
}
}
if (pos == limit) {
int len = limit - start;
if (len >= cb.length) {
// allocate larger buffer and copy chars to beginning
char[] tmp = new char[2 * cb.length];
System.arraycopy(cb, start, tmp, 0, len);
cb = tmp;
} else if (start != 0 && len != 0) {
// move fragment to beginning of buffer
System.arraycopy(cb, start, cb, 0, len);
}
pos = limit = len;
start = 0;
break;
}
}
}
// add a string if EOS terminates the last line
if (limit > start)
lines.add(new String(cb, start, limit - start));
return Collections.unmodifiableList(lines);
}
-------------
PR Comment: https://git.openjdk.org/jdk/pull/25863#issuecomment-3020286174
More information about the core-libs-dev
mailing list