#1933 remove use of sun.misc.Unsafe by robfrank · Pull Request #1934 · ArcadeData/arcadedb (original) (raw)
If you're using MemorySegments, I think this code could be simplified (and likely doesn't need the Arena given these are heap allocated JVM arrays):
// assumes left and right are non-null and length is non-zero if (left.length < length || right.length < length) return false;
MemorySegment leftSegment = MemorySegment.ofArray(left).asSlice(0, length); MemorySegment rightSegment = MemorySegment.ofArray(right).asSlice(0, length);
// mismatch is optimized and should be faster than a for loop return leftSegment.mismatch(rightSegment) == -1;
Compare needs to take into account the possibility that left or right is a prefix of the other, but would use the same general flow:
// assumes left and right are non-null MemorySegment leftSegment = MemorySegment.ofArray(left); MemorySegment rightSegment = MemorySegment.ofArray(right);
long index = leftSegment.mismatch(rightSegment); if (index == -1) { return Integer.compare(left.length, right.length); }
// index is either the byte offset which differs or the length of the shorter array if (index >= left.length || index >= right.length) { return Integer.compare(left.length, right.length); }
return Integer.compare(Byte.toUnsignedInt(left[(int)index]), Byte.toUnsignedInt(right[(int)index]));