RFR: 8339531: Improve performance of MemorySegment::mismatch

Maurizio Cimadamore mcimadamore at openjdk.org
Wed Sep 4 10:17:19 UTC 2024


On Wed, 4 Sep 2024 09:09:32 GMT, Per Minborg <pminborg at openjdk.org> wrote:

> This PR proposes to improve the performance of `MemorySegment::mismatch` by using Java code rather than transitioning to native code.

src/java.base/share/classes/jdk/internal/foreign/SegmentBulkOperations.java line 123:

> 121:     // This method is intended for 0 <= bytes < 7
> 122:     @ForceInline
> 123:     private static long mismatchSmall(AbstractMemorySegmentImpl src, long srcOffset,

Question. Isn't this:


// 0...0X00
            if (remaining >= 4) {
                if (SCOPED_MEMORY_ACCESS.getInt(src.sessionImpl(), src.unsafeGetBase(), src.unsafeGetOffset() + srcFromOffset + offset) !=
                        SCOPED_MEMORY_ACCESS.getInt(dst.sessionImpl(), dst.unsafeGetBase(), dst.unsafeGetOffset() + dstFromOffset + offset)) {
                    return mismatchSmall(src, srcFromOffset + offset, dst, dstFromOffset + offset, offset, 4, srcAndDstBytesDiffer);
                }
                offset += 4;
                remaining -= 4;
            }
            // 0...00X0
            if (remaining >= 2) {
                if (SCOPED_MEMORY_ACCESS.getShort(src.sessionImpl(), src.unsafeGetBase(), src.unsafeGetOffset() + srcFromOffset + offset) !=
                        SCOPED_MEMORY_ACCESS.getShort(dst.sessionImpl(), dst.unsafeGetBase(), dst.unsafeGetOffset() + dstFromOffset + offset)) {
                    return mismatchSmall(src, srcFromOffset + offset, dst, dstFromOffset + offset, offset, 2, srcAndDstBytesDiffer);
                }
                offset += 2;
                remaining -= 2;
            }
            // 0...000X
            if (remaining == 1) {
                if (SCOPED_MEMORY_ACCESS.getByte(src.sessionImpl(), src.unsafeGetBase(), src.unsafeGetOffset() + srcFromOffset + offset) !=
                        SCOPED_MEMORY_ACCESS.getByte(dst.sessionImpl(), dst.unsafeGetBase(), dst.unsafeGetOffset() + dstFromOffset + offset)) {
                    return offset;
                }
            }


An optimized version of "mismatchSmall" ? Can we reuse the code?

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/20848#discussion_r1743461331


More information about the core-libs-dev mailing list