RFR: 8264777: Overload optimized FileInputStream::readAllBytes

Alan Bateman alanb at openjdk.java.net
Tue May 4 14:09:59 UTC 2021


On Mon, 3 May 2021 20:33:23 GMT, Brian Burkhalter <bpb at openjdk.org> wrote:

> Please consider this request to override the `java.io.InputStream` methods `readAllBytes()` and `readNBytes(int)` in `FileInputStream` with more performant implementations. The method overrides attempt to read all requested bytes into a single array of the required size rather than composing the result from a sequence of smaller arrays. An example of the performance improvements is as follows.
> 
> **readAllBytes**
> Before
> 
> Benchmark                                    (length)   Mode  Cnt      Score     Error  Units
> ReadAllBytes.readAllBytesFileInputStream      1000000  thrpt   20   2412.031 ±   7.309  ops/s
> ReadAllBytes.readAllBytesFileInputStream     10000000  thrpt   20    212.181 ±   0.369  ops/s
> ReadAllBytes.readAllBytesFileInputStream    100000000  thrpt   20     19.860 ±   0.048  ops/s
> ReadAllBytes.readAllBytesFileInputStream   1000000000  thrpt   20      1.298 ±   0.183  ops/s
> 
> After
> 
> Benchmark                                    (length)   Mode  Cnt      Score     Error  Units
> ReadAllBytes.readAllBytesFileInputStream      1000000  thrpt   20   8218.473 ±  43.425  ops/s
> ReadAllBytes.readAllBytesFileInputStream     10000000  thrpt   20    302.781 ±   1.273  ops/s
> ReadAllBytes.readAllBytesFileInputStream    100000000  thrpt   20     22.461 ±   0.084  ops/s
> ReadAllBytes.readAllBytesFileInputStream   1000000000  thrpt   20      1.502 ±   0.003  ops/s
> 
> 
> **readNBytes**
> 
> `N = file_size / 2`
> 
> Before
> 
> Benchmark                                    (length)   Mode  Cnt      Score     Error  Units
> ReadAllBytes.readHalfBytesFileInputStream     1000000  thrpt   20   5585.500 ± 153.353  ops/s
> ReadAllBytes.readHalfBytesFileInputStream    10000000  thrpt   20    436.164 ±   1.104  ops/s
> ReadAllBytes.readHalfBytesFileInputStream   100000000  thrpt   20     40.167 ±   0.141  ops/s
> ReadAllBytes.readHalfBytesFileInputStream  1000000000  thrpt   20      3.291 ±   0.211  ops/s
> 
> 
> After
> 
> Benchmark                                    (length)   Mode  Cnt      Score     Error  Units
> ReadAllBytes.readHalfBytesFileInputStream     1000000  thrpt   20  15463.210 ±  66.045  ops/s
> ReadAllBytes.readHalfBytesFileInputStream    10000000  thrpt   20    561.752 ±   0.951  ops/s
> ReadAllBytes.readHalfBytesFileInputStream   100000000  thrpt   20     45.043 ±   0.102  ops/s
> ReadAllBytes.readHalfBytesFileInputStream  1000000000  thrpt   20      4.629 ±   0.035  ops/s

src/java.base/share/classes/java/io/FileInputStream.java line 342:

> 340: 
> 341:     private native long length() throws IOException;
> 342:     private native long position() throws IOException;

Can you confirm that you've tested with special files? It's very likely that there will be cases where lseek will fail.

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

PR: https://git.openjdk.java.net/jdk/pull/3845


More information about the core-libs-dev mailing list