The Buffer implementations of equals and compareTo (for Byte/IntBuffer etc) perform element wise comparison.
The implementations can be modified to leverage the vectorized mismatch functionality currently located in java.util.ArraysSupport and thereby take advantage of vectorized operations that will improve performance.
Heap buffer equality and comparison is relatively straightforward, as is the case for direct byte buffers (heap and direct byte buffers can be compared since the mismatch method does not differentiate between on or off heap regions).
Primitive views of a heap or direct byte buffer are a little more complex because byte buffer order (endianness). If the order differs between two buffers then the vectorized mismatch cannot be applied (and it is questionable that it should be modified to support such cases) and element-wise comparison should be performed.
Buffers come in seven main forms:
1) HeapByteBuffer. Has a specified order for primitive views using 4) or 5)
2) DirectByteBuffer. Has a specified order for primitive views using 4), 5), 6) and 7)
3) Heap*Buffer. Native Endian. For heap buffers of all supported primitive types other than byte.
4) ByteBufferAs*BufferL, Little Endian. For direct buffer views, when unaligned access is not supported and the base address is not aligned for accessing values of the primitive type. For heap buffers views.
5) ByteBufferAs*BufferB, Big Endian. For direct buffer views, when unaligned access is not supported and the base address is not aligned for accessing values of the primitive type. For heap buffers views.
6) Direct*BufferU, Native Endian. For direct buffer views when unaligned access is supported.
7) Direct*BufferS, !Native Endian. For direct buffer views when unaligned access is supported.
It is possible when performing equality or comparison one form may meet the other (on heap or direct). A package private method is likely required to obtain the order rather than performing a sequence of instanceof checks.
Note: it would be desirable to remove 6) and 7) and rely solely on 4) and 5) and there by simplifying the buffer code. The former uses the single addressing mode of Unsafe where as the latter uses the double addressing mode. Performance implications of the double addressing mode when used with direct byte buffers needs to be investigated, specifically related to null checks of the base object reference.
The implementations can be modified to leverage the vectorized mismatch functionality currently located in java.util.ArraysSupport and thereby take advantage of vectorized operations that will improve performance.
Heap buffer equality and comparison is relatively straightforward, as is the case for direct byte buffers (heap and direct byte buffers can be compared since the mismatch method does not differentiate between on or off heap regions).
Primitive views of a heap or direct byte buffer are a little more complex because byte buffer order (endianness). If the order differs between two buffers then the vectorized mismatch cannot be applied (and it is questionable that it should be modified to support such cases) and element-wise comparison should be performed.
Buffers come in seven main forms:
1) HeapByteBuffer. Has a specified order for primitive views using 4) or 5)
2) DirectByteBuffer. Has a specified order for primitive views using 4), 5), 6) and 7)
3) Heap*Buffer. Native Endian. For heap buffers of all supported primitive types other than byte.
4) ByteBufferAs*BufferL, Little Endian. For direct buffer views, when unaligned access is not supported and the base address is not aligned for accessing values of the primitive type. For heap buffers views.
5) ByteBufferAs*BufferB, Big Endian. For direct buffer views, when unaligned access is not supported and the base address is not aligned for accessing values of the primitive type. For heap buffers views.
6) Direct*BufferU, Native Endian. For direct buffer views when unaligned access is supported.
7) Direct*BufferS, !Native Endian. For direct buffer views when unaligned access is supported.
It is possible when performing equality or comparison one form may meet the other (on heap or direct). A package private method is likely required to obtain the order rather than performing a sequence of instanceof checks.
Note: it would be desirable to remove 6) and 7) and rely solely on 4) and 5) and there by simplifying the buffer code. The former uses the single addressing mode of Unsafe where as the latter uses the double addressing mode. Performance implications of the double addressing mode when used with direct byte buffers needs to be investigated, specifically related to null checks of the base object reference.
- relates to
-
JDK-8199773 (bf) XXXBuffer:compareTo method is not working as expected
-
- Closed
-
-
JDK-8193475 Enhance Buffer equals/compareTo
-
- Open
-