RFR 8193832: Performance of InputStream.readAllBytes() could be improved
Remi Forax
forax at univ-mlv.fr
Tue Dec 19 21:35:18 UTC 2017
----- Mail original -----
> De: "Paul Sandoz" <paul.sandoz at oracle.com>
> À: "Brian Burkhalter" <brian.burkhalter at oracle.com>
> Cc: "core-libs-dev" <core-libs-dev at openjdk.java.net>
> Envoyé: Mardi 19 Décembre 2017 21:52:03
> Objet: Re: RFR 8193832: Performance of InputStream.readAllBytes() could be improved
> Hi,
>
> For the case of reading 2^N bytes i believe you can avoid doing a last copy by
> checking if “n < 0" within the “nread > 0” block when “nread ==
> DEAFULT_BUFFER_SIZE”. That might close the perf gap for smaller cases. You can
> also move "nread = 0” to the same block e.g.:
>
> var copy = (n < 0 && nread == DEAFULT_BUFFER_SIZE) ? buf : Arrays.copyOf(buf,
> nread);
> list.add(copy)
> nread = 0;
>
>
> 262 byte[] output = new byte[total];
> 263 int offset = 0;
> 264 int numCached = list.size();
> 265 for (int i = 0; i < numCached; i++) {
> 266 byte[] b = list.get(i);
> 267 System.arraycopy(b, 0, output, offset, b.length);
> 268 offset += b.length;
> 269 }
>
> You can simplify to:
>
> var result = new byte[total];
> int offset = 0;
> for (buf : list) {
> System.arraycopy(buf, 0, result, offset, buf.length);
> offset += buf.length;
> }
>
> s/list/bufs and then you can use var for the declarations at the start of the
> method.
>
> Paul.
About using var, IMO var declaration makes usually the code more readable apart if you mix var declaration and classical declaration and if you call a method that has several overloads.
Is there a usage guide somewhere ?
Rémi
More information about the core-libs-dev
mailing list