You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jira@kafka.apache.org by GitBox <gi...@apache.org> on 2022/06/17 14:04:28 UTC

[GitHub] [kafka] ijuma commented on a diff in pull request #12163: KAFKA-13900 Support Java 9 direct ByteBuffer Checksum methods

ijuma commented on code in PR #12163:
URL: https://github.com/apache/kafka/pull/12163#discussion_r900153506


##########
clients/src/main/java/org/apache/kafka/common/utils/Checksums.java:
##########
@@ -30,21 +36,74 @@
  */
 public final class Checksums {
 
+    private static final Logger LOGGER = LoggerFactory.getLogger(Checksums.class);
+    private static final MethodHandle BYTE_BUFFER_UPDATE;
+
+    static {
+        MethodHandle byteBufferUpdate = null;
+        if (Java.IS_JAVA9_COMPATIBLE) {
+            try {
+                byteBufferUpdate = MethodHandles.publicLookup().findVirtual(Checksum.class, "update", MethodType.methodType(void.class, ByteBuffer.class));
+            } catch (Throwable t) {
+                LOGGER.warn("Cannot uses java.util.zip.Checksum::update(ByteBuffer): direct ByteBuffers checksum won't use optimized platform support", t);
+            }
+        }
+        BYTE_BUFFER_UPDATE = byteBufferUpdate;
+    }
+
     private Checksums() {
     }
 
+    /**
+     * Uses {@link Checksum#update} on {@code buffer}'s content, without modifying its position and limit.<br>
+     * This is semantically equivalent to {@link #update(Checksum, ByteBuffer, int, int)} with {@code offset = 0}.
+     */
     public static void update(Checksum checksum, ByteBuffer buffer, int length) {
         update(checksum, buffer, 0, length);
     }
 
+    /**
+     * Uses {@link Checksum#update} on {@code buffer}'s content, starting from the given {@code offset}
+     * by the provided {@code length}, without modifying its position and limit.
+     */
     public static void update(Checksum checksum, ByteBuffer buffer, int offset, int length) {
         if (buffer.hasArray()) {
             checksum.update(buffer.array(), buffer.position() + buffer.arrayOffset() + offset, length);
-        } else {
-            int start = buffer.position() + offset;
-            for (int i = start; i < start + length; i++)
-                checksum.update(buffer.get(i));
+            return;
+        }
+        if (BYTE_BUFFER_UPDATE != null && buffer.isDirect()) {

Review Comment:
   Do we have any tests for this method that use direct byte buffers? If not, can we please add them?



##########
clients/src/main/java/org/apache/kafka/common/utils/Checksums.java:
##########
@@ -30,21 +36,74 @@
  */
 public final class Checksums {
 
+    private static final Logger LOGGER = LoggerFactory.getLogger(Checksums.class);

Review Comment:
   Nit: we typically name this `log`.



##########
clients/src/main/java/org/apache/kafka/common/utils/Checksums.java:
##########
@@ -30,21 +36,74 @@
  */
 public final class Checksums {
 
+    private static final Logger LOGGER = LoggerFactory.getLogger(Checksums.class);
+    private static final MethodHandle BYTE_BUFFER_UPDATE;
+
+    static {
+        MethodHandle byteBufferUpdate = null;
+        if (Java.IS_JAVA9_COMPATIBLE) {
+            try {
+                byteBufferUpdate = MethodHandles.publicLookup().findVirtual(Checksum.class, "update", MethodType.methodType(void.class, ByteBuffer.class));
+            } catch (Throwable t) {
+                LOGGER.warn("Cannot uses java.util.zip.Checksum::update(ByteBuffer): direct ByteBuffers checksum won't use optimized platform support", t);
+            }
+        }
+        BYTE_BUFFER_UPDATE = byteBufferUpdate;
+    }
+
     private Checksums() {
     }
 
+    /**
+     * Uses {@link Checksum#update} on {@code buffer}'s content, without modifying its position and limit.<br>
+     * This is semantically equivalent to {@link #update(Checksum, ByteBuffer, int, int)} with {@code offset = 0}.
+     */
     public static void update(Checksum checksum, ByteBuffer buffer, int length) {
         update(checksum, buffer, 0, length);
     }
 
+    /**
+     * Uses {@link Checksum#update} on {@code buffer}'s content, starting from the given {@code offset}
+     * by the provided {@code length}, without modifying its position and limit.
+     */
     public static void update(Checksum checksum, ByteBuffer buffer, int offset, int length) {
         if (buffer.hasArray()) {
             checksum.update(buffer.array(), buffer.position() + buffer.arrayOffset() + offset, length);
-        } else {
-            int start = buffer.position() + offset;
-            for (int i = start; i < start + length; i++)
-                checksum.update(buffer.get(i));
+            return;
+        }
+        if (BYTE_BUFFER_UPDATE != null && buffer.isDirect()) {
+            final int oldPosition = buffer.position();
+            final int oldLimit = buffer.limit();
+            try {
+                // save a slice to be used to save an allocation in the hot-path
+                final int start = oldPosition + offset;
+                buffer.limit(start + length);
+                buffer.position(start);
+                BYTE_BUFFER_UPDATE.invokeExact(checksum, buffer);
+                return;

Review Comment:
   Nit: instead of the early return here, why don't we have an if/else? It seems clearer.



##########
clients/src/main/java/org/apache/kafka/common/utils/Checksums.java:
##########
@@ -30,21 +36,74 @@
  */
 public final class Checksums {
 
+    private static final Logger LOGGER = LoggerFactory.getLogger(Checksums.class);
+    private static final MethodHandle BYTE_BUFFER_UPDATE;
+
+    static {
+        MethodHandle byteBufferUpdate = null;
+        if (Java.IS_JAVA9_COMPATIBLE) {
+            try {
+                byteBufferUpdate = MethodHandles.publicLookup().findVirtual(Checksum.class, "update", MethodType.methodType(void.class, ByteBuffer.class));
+            } catch (Throwable t) {
+                LOGGER.warn("Cannot uses java.util.zip.Checksum::update(ByteBuffer): direct ByteBuffers checksum won't use optimized platform support", t);

Review Comment:
   This should not be possible, right? This method is specified to be available in the standard library for Java 9 and newer. Would it make sense to just fail?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org