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/05/14 04:00:17 UTC

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

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


##########
clients/src/main/java/org/apache/kafka/common/utils/Checksums.java:
##########
@@ -40,10 +55,32 @@ public static void update(Checksum checksum, ByteBuffer buffer, int length) {
     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) {
+            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 = buffer.position() + offset;
+                buffer.position(0);
+                buffer.limit(start + length);
+                buffer.position(start);
+                BYTE_BUFFER_UPDATE.invokeExact(checksum, buffer);
+                return;
+            } catch (Throwable t) {
+                // fallback
+            } finally {
+                // reset buffer's offsets
+                buffer.position(0);

Review Comment:
   I've just read the `ByteBuffer` doc: I have moved first `position` to save `limit`'s change to throw any exception, but I see that it won't happen...let me remove this, good point :+1: 



-- 
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