On 2021-02-16 Brett Okken wrote:
> We found in LZDecoder that using System.arrayCopy with doubling size
> is faster than Arrays.fill (especially for larger arrays).
> We can apply that knowledge in the BasicArrayCache, where there are
> some use cases which require clearing out the array prior to returning
> it.

A simple micro-benchmark gives me a very different result. The
alternative method is roughly 70 % slower than Arrays.fill on my system
with a big array. If Arrays.fill were so terrible, it should be
improved instead. Even if the alternative method were faster, it would
need to be a lot faster to be worth the extra complexity.

If Arrays.fill version (uncomment/comment the code) is slower for you,
it must depend on the Java runtime or operating system or such things.

import java.util.Arrays;

public class Foo {
    public static void main(String[] args) throws Exception {
        byte[] buf = new byte[10 << 20];

        for (int i = 0; i < 4000; ++i) {
            //Arrays.fill(buf, (byte)0);

            buf[0] = (byte)0;
            buf[1] = (byte)0;
            buf[2] = (byte)0;
            buf[3] = (byte)0;
            int toCopy = 4;
            int remaining = buf.length - toCopy;
            do {
                System.arraycopy(buf, 0, buf, toCopy, toCopy);
                remaining -= toCopy;
                toCopy <<= 1;
            } while (remaining >= toCopy);

            if (remaining != 0) {
                System.arraycopy(buf, 0, buf, toCopy, remaining);
            }
        }
    }
}

-- 
Lasse Collin  |  IRC: Larhzu @ IRCnet & Freenode

Reply via email to