leventov / Koloboke

Java Collections till the last breadcrumb of memory and performance
https://koloboke.com/
1.01k stars 139 forks source link

Use Arrays.fill instead of home brewed fill #29

Open nitsanw opened 9 years ago

nitsanw commented 9 years ago

This code:

public static void fillKeys(char[] table, byte key) {
    /* if !(long elem) */
    long base = CHAR_BASE + BYTE_KEY_OFFSET;
    for (long off = CHAR_SCALE * (long) table.length; (off -= CHAR_SCALE) >= 0L;) {
        U.putByte(table, base + off, key);
    }
    /* elif long elem */
    for (int i = 0; i < table.length; i += 2) {
        table[i] = key;
    }
    /* endif */
}

Will perform worse than Arrays.fill which is a (sort of) intrinsic on OpenJDK. The intrinsic version will replace 'fill loops' of the pattern 'for(int i=0;i<length;i++)a[i] = v;" with an optimized version using wider write instructions. This is similar to System.arrayCopy in spirit. The optimization is for all types smaller than long.

peter-lawrey commented 9 years ago

It should really write a 32 - bits or 64 - bits at a time ever for smaller types. On 09/11/2014 9:10 AM, "Nitsan Wakart" notifications@github.com wrote:

This code:

public static void fillKeys(char[] table, byte key) { /* if !(long elem) _/ long base = CHAR_BASE + BYTE_KEY_OFFSET; for (long off = CHAR_SCALE * (long) table.length; (off -= CHARSCALE) >= 0L;) { U.putByte(table, base + off, key); } / elif long elem / for (int i = 0; i < table.length; i += 2) { table[i] = key; } / endif */ }

Will perform worse than Arrays.fill which is a (sort of) intrinsic on OpenJDK. The intrinsic version will replace 'fill loops' of the pattern 'for(int i=0;i<length;i++)a[i] = v;" with an optimized version using wider write instructions. This is similar to System.arrayCopy in spirit. The optimization is for all types smaller than long.

— Reply to this email directly or view it on GitHub https://github.com/OpenHFT/Koloboke/issues/29.