RFR 8193832: Performance of InputStream.readAllBytes() could be improved

Brian Burkhalter brian.burkhalter at oracle.com
Tue Dec 19 21:43:28 UTC 2017


On Dec 19, 2017, at 12:52 PM, Paul Sandoz <paul.sandoz at oracle.com> wrote:

> 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;

Definitely improves performance and memory footprint for this case.

> 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;
> }

Oh, yes, of course.

> s/list/bufs and then you can use var for the declarations at the start of the method.

Done. Updated: http://cr.openjdk.java.net/~bpb/8193832/webrev.01/

Good suggestions! Not sure however about line 237 as with var it has to be “var n = 0;” instead of simply “int n;” with no initialization. Also I’ve not tested the effect of the initial List capacity at line 233: the current value of 128 is arbitrary - might be better to go with the default?

Thanks,

Brian


More information about the core-libs-dev mailing list