From 6e203384f8777fc55081065b128bd2b0ba074729 Mon Sep 17 00:00:00 2001 From: Brian Burkhalter Date: Wed, 9 Jul 2025 16:15:36 +0000 Subject: [PATCH] 8358533: Improve performance of java.io.Reader.readAllLines Reviewed-by: rriggs, sherman --- .../share/classes/java/io/Reader.java | 73 +++++++++++++++--- test/jdk/java/io/Reader/ReadAll.java | 75 ++++++++++++++++++- .../bench/java/io/ReaderReadAllLines.java | 66 ++++++++++++++++ 3 files changed, 198 insertions(+), 16 deletions(-) create mode 100644 test/micro/org/openjdk/bench/java/io/ReaderReadAllLines.java diff --git a/src/java.base/share/classes/java/io/Reader.java b/src/java.base/share/classes/java/io/Reader.java index af5ac9f68ec..9656d140023 100644 --- a/src/java.base/share/classes/java/io/Reader.java +++ b/src/java.base/share/classes/java/io/Reader.java @@ -27,8 +27,11 @@ package java.io; import java.nio.CharBuffer; import java.nio.ReadOnlyBufferException; +import java.util.ArrayList; +import java.util.Collections; import java.util.List; import java.util.Objects; +import jdk.internal.util.ArraysSupport; /** * Abstract class for reading character streams. The only methods that a @@ -397,16 +400,6 @@ public abstract class Reader implements Readable, Closeable { */ public abstract int read(char[] cbuf, int off, int len) throws IOException; - private String readAllCharsAsString() throws IOException { - StringBuilder result = new StringBuilder(); - char[] cbuf = new char[TRANSFER_BUFFER_SIZE]; - int nread; - while ((nread = read(cbuf, 0, cbuf.length)) != -1) { - result.append(cbuf, 0, nread); - } - return result.toString(); - } - /** * Reads all remaining characters as lines of text. This method blocks until * all remaining characters have been read and end of stream is detected, @@ -457,7 +450,57 @@ public abstract class Reader implements Readable, Closeable { * @since 25 */ public List readAllLines() throws IOException { - return readAllCharsAsString().lines().toList(); + List lines = new ArrayList<>(); + char[] cb = new char[1024]; + + 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/2) { + // allocate larger buffer and copy chars to beginning + int newLength = ArraysSupport.newLength(cb.length, + TRANSFER_BUFFER_SIZE, cb.length); + char[] tmp = new char[newLength]; + 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); } /** @@ -499,7 +542,13 @@ public abstract class Reader implements Readable, Closeable { * @since 25 */ public String readAllAsString() throws IOException { - return readAllCharsAsString(); + StringBuilder result = new StringBuilder(); + char[] cbuf = new char[TRANSFER_BUFFER_SIZE]; + int nread; + while ((nread = read(cbuf, 0, cbuf.length)) != -1) { + result.append(cbuf, 0, nread); + } + return result.toString(); } /** Maximum skip-buffer size */ diff --git a/test/jdk/java/io/Reader/ReadAll.java b/test/jdk/java/io/Reader/ReadAll.java index 72fcf459eca..fbe1d5a6431 100644 --- a/test/jdk/java/io/Reader/ReadAll.java +++ b/test/jdk/java/io/Reader/ReadAll.java @@ -66,12 +66,55 @@ public class ReadAll { int size = rnd.nextInt(2, 16386); int plen = PHRASE.length(); - List strings = new ArrayList(size); + StringBuilder sb = new StringBuilder(plen); + List strings = new ArrayList<>(size); while (strings.size() < size) { int fromIndex = rnd.nextInt(0, plen / 2); int toIndex = rnd.nextInt(fromIndex, plen); - strings.add(PHRASE.substring(fromIndex, toIndex)); + String s = PHRASE.substring(fromIndex, toIndex); + sb.append(s); + int bound = toIndex - fromIndex; + if (bound > 0) { + int offset = bound/2; + int n = rnd.nextInt(0, bound); + for (int i = 0; i < n; i++) { + String f = null; + switch (rnd.nextInt(7)) { + case 0 -> f = ""; + case 1 -> f = "\r"; + case 2 -> f = "\n"; + case 3 -> f = "\r\n"; + case 4 -> f = "\r\r"; + case 5 -> f = "\n\n"; + case 6 -> f = " "; + } + sb.insert(offset, f); + } + } + strings.add(sb.toString()); + sb.setLength(0); } + + String p4096 = PHRASE.repeat((4096 + plen - 1)/plen); + String p8192 = PHRASE.repeat((8192 + plen - 1)/plen); + String p16384 = PHRASE.repeat((16384 + plen - 1)/plen); + + for (int i = 0; i < 64; i++) { + for (int j = 0; j < 32; j++) { + switch (rnd.nextInt(8)) { + case 0 -> sb.append(""); + case 1 -> sb.append(" "); + case 2 -> sb.append("\n"); + case 3 -> sb.append(PHRASE); + case 5 -> sb.append(p4096); + case 6 -> sb.append(p8192); + case 7 -> sb.append(p16384); + } + } + strings.add(sb.toString()); + sb.setLength(0); + } + Files.write(path, strings); System.out.println(strings.size() + " lines written"); } @@ -85,6 +128,7 @@ public class ReadAll { @Test public void readAllLines() throws IOException { // Reader implementation + System.out.println("Reader implementation"); List lines; try (FileReader fr = new FileReader(file)) { lines = fr.readAllLines(); @@ -92,9 +136,21 @@ public class ReadAll { System.out.println(lines.size() + " lines read"); List linesExpected = Files.readAllLines(path); - assertEquals(linesExpected, lines); + int count = linesExpected.size(); + if (lines.size() != count) + throw new RuntimeException("Size mismatch: " + lines.size() + " != " + count); + for (int i = 0; i < count; i++) { + String expected = linesExpected.get(i); + String actual = lines.get(i); + if (!actual.equals(expected)) { + String msg = String.format("%d: \"%s\" != \"%s\"", + i, actual, expected); + throw new RuntimeException(msg); + } + } // Reader.of implementation + System.out.println("Reader.of implementation"); String stringExpected = Files.readString(path); int n = rnd.nextInt(stringExpected.length()/2); String substringExpected = stringExpected.substring(n); @@ -103,7 +159,18 @@ public class ReadAll { r.skip(n); lines = r.readAllLines(); } - assertEquals(linesExpected, lines); + count = linesExpected.size(); + if (lines.size() != count) + throw new RuntimeException("Size mismatch: " + lines.size() + " != " + count); + for (int i = 0; i < count; i++) { + String expected = linesExpected.get(i); + String actual = lines.get(i); + if (!actual.equals(expected)) { + String msg = String.format("%d: \"%s\" != \"%s\"", + i, actual, expected); + throw new RuntimeException(msg); + } + } } @Test diff --git a/test/micro/org/openjdk/bench/java/io/ReaderReadAllLines.java b/test/micro/org/openjdk/bench/java/io/ReaderReadAllLines.java new file mode 100644 index 00000000000..a3663e083ec --- /dev/null +++ b/test/micro/org/openjdk/bench/java/io/ReaderReadAllLines.java @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package org.openjdk.bench.java.io; + +import java.io.CharArrayReader; +import java.io.IOException; +import java.io.Reader; +import java.util.List; +import java.util.Random; + +import org.openjdk.jmh.annotations.Benchmark; +import org.openjdk.jmh.annotations.Scope; +import org.openjdk.jmh.annotations.Setup; +import org.openjdk.jmh.annotations.State; + +@State(Scope.Benchmark) +public class ReaderReadAllLines { + + private char[] chars = null; + + @Setup + public void setup() throws IOException { + final int len = 128_000; + chars = new char[len]; + Random rnd = new Random(System.nanoTime()); + int off = 0; + while (off < len) { + int lineLen = 40 + rnd.nextInt(8192); + if (lineLen > len - off) { + off = len; + } else { + chars[off + lineLen] = '\n'; + off += lineLen; + } + } + } + + @Benchmark + public List readAllLines() throws IOException { + List lines; + try (Reader reader = new CharArrayReader(chars)) { + lines = reader.readAllLines(); + } + return lines; + } +}