On Fri, Mar 30, 2018 at 5:01 PM, Paul Sandoz <paul.san...@oracle.com> wrote:
> If the res field is non-volatile then is C2 already capable of hoisting > the field access out of the loop as if the result was assigned to a local > variable? > > final ZipFile z = … > final z_res = z.res; > while (...) { > use(z_res); > } > It can do this if it can inline everything and determine that nothing inside the loop writes to res AND there is no synchronization happening (else you would need to prove no other thread is writing to the field). So I would be surprised if any JIT would do this in practice for a complex loop. Relatedly, I have a local mod to ZipFile that does: final byte[] cen; final int[] entries; final int entryCount; final ZipCoder zc = this.zc; // Acquire the lock once briefly to obtain immutable data references. synchronized (ZipFile.this) { ensureOpen(); cen = zsrc.cen; entries = zsrc.entries; entryCount = zsrc.total; } .... use(cen, entries, entryCount, zc)