Koloboke icon indicating copy to clipboard operation
Koloboke copied to clipboard

Use Arrays.fill instead of home brewed fill

Open nitsanw opened this issue 9 years ago • 1 comments

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.

nitsanw avatar Nov 09 '14 09:11 nitsanw

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" [email protected] 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 -= 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.

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

peter-lawrey avatar Nov 09 '14 09:11 peter-lawrey