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

Paul Sandoz paul.sandoz at oracle.com
Tue Dec 19 22:28:48 UTC 2017


On 19 Dec 2017, at 13:43, Brian Burkhalter <brian.burkhalter at oracle.com> wrote:

On Dec 19, 2017, at 12:52 PM, Paul Sandoz <paul.sandoz at oracle.com <mailto: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 == DEAFULTBUFFERSIZE”. 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 == DEAFULTBUFFERSIZE) ? 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/ <http://cr.openjdk.java.net/~bpb/8193832/webrev.01/>

You can also simplify the “for(;;) + break" into a do while loop:

do { int nread = 0; ... } while (n > 0);

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.

I was only suggesting it’s use for the byte[] and ArrayList<byte[]>. IMHO it’s a little subjective but there is less upside for int, although one can argue consistent application and explicit initialization is a good thing here.

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?

My inclination would be to use 8 instead of 128, that allows 2^16 in size by default, rather than 2^20.

Paul.



More information about the core-libs-dev mailing list