apache/pekko

Feature request: Using SIMD for byte search

He-Pin opened this issue ยท 4 comments

Motivation:
When reading the code in #1247 , I recall that we can improve performance with SIDM.
related blog: https://richardstartin.github.io/posts/finding-bytes

and for reference in Netty: netty/netty#10737

Would you like to take a look at this too @JD557

int position = firstInstance(getWord(new byte[]{1, 2, 0, 3, 4, 10, (byte)'\n', 5}, 0), compilePattern((byte)'\n');
...

private static long compilePattern(byte byteToFind) {
    long pattern = byteToFind & 0xFFL;
    return pattern
        | (pattern << 8)
        | (pattern << 16)
        | (pattern << 24)
        | (pattern << 32)
        | (pattern << 40)
        | (pattern << 48)
        | (pattern << 56);
}

private static int firstInstance(long word, long pattern) {
    long input = word ^ pattern;
    long tmp = (input & 0x7F7F7F7F7F7F7F7FL) + 0x7F7F7F7F7F7F7F7FL;
    tmp = ~(tmp | input | 0x7F7F7F7F7F7F7F7FL);
    return Long.numberOfLeadingZeros(tmp) >>> 3;
}

@He-Pin Thanks for sharing the blog post ๐Ÿ˜„. Interesting stuff

@He-Pin Thanks for sharing the blog post ๐Ÿ˜„. Interesting stuff

Yes, but have no time to test it locally. with it, a single loop will test 8 bytes.

But without the vector api that what we can do now.

so cool

Would you like to take a look at this too @JD557

Unfortunately, I don't think I'll have capacity to look into this in the near future. Also, my knowledge of SIMD is pretty limited.

Having said that:

But without the vector api that what we can do now.

I think there's value in first trying something that is not using the Vector API (like the code suggested above). That way Pekko can keep supporting old Java versions, as well as being easier to cross compile to JS and Native.

Hopefully that would hit the autovectorizaton logic in some of those platforms, but I haven't tested that ๐Ÿ˜