ijuma commented on a change in pull request #11721:
URL: https://github.com/apache/kafka/pull/11721#discussion_r799456068



##########
File path: 
clients/src/test/java/org/apache/kafka/common/utils/ByteUtilsTest.java
##########
@@ -239,6 +239,66 @@ public void testDouble() throws IOException {
         assertDoubleSerde(Double.NEGATIVE_INFINITY, 0xFFF0000000000000L);
     }
 
+    private static int mathSizeOfUnsignedVarint(int value) {
+        int leadingZeros = Integer.numberOfLeadingZeros(value);
+        // return (38 - leadingZeros) / 7 + leadingZeros / 32;
+        int leadingZerosBelow38DividedBy7 = ((38 - leadingZeros) * 
0b10010010010010011) >>> 19;
+        return leadingZerosBelow38DividedBy7 + (leadingZeros >>> 5);
+    }
+
+    @Test
+    public void testSizeOfUnsignedVarintMath() {
+        for (int i = 0; i < Integer.MAX_VALUE; i++) {

Review comment:
       Is this too slow for a unit test? Similar question for other tests that 
do a large range of values.

##########
File path: 
clients/src/test/java/org/apache/kafka/common/utils/ByteUtilsTest.java
##########
@@ -239,6 +239,66 @@ public void testDouble() throws IOException {
         assertDoubleSerde(Double.NEGATIVE_INFINITY, 0xFFF0000000000000L);
     }
 
+    private static int mathSizeOfUnsignedVarint(int value) {
+        int leadingZeros = Integer.numberOfLeadingZeros(value);
+        // return (38 - leadingZeros) / 7 + leadingZeros / 32;
+        int leadingZerosBelow38DividedBy7 = ((38 - leadingZeros) * 
0b10010010010010011) >>> 19;
+        return leadingZerosBelow38DividedBy7 + (leadingZeros >>> 5);
+    }
+
+    @Test
+    public void testSizeOfUnsignedVarintMath() {
+        for (int i = 0; i < Integer.MAX_VALUE; i++) {
+            final int actual = mathSizeOfUnsignedVarint(i);
+            final int expected = oldSizeOfUnsignedVarint(i);
+            assertEquals(expected, actual);
+        }
+    }
+
+    /**
+     * The old well-known implementation for sizeOfUnsignedVarint
+     */
+    private static int oldSizeOfUnsignedVarint(int value) {

Review comment:
       Maybe we can call this `simpleSizeOfUnsignedVarint` or something like 
that?

##########
File path: 
jmh-benchmarks/src/main/java/org/apache/kafka/jmh/util/ByteUtilsBenchmark.java
##########
@@ -0,0 +1,79 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.kafka.jmh.util;
+
+import java.util.concurrent.ThreadLocalRandom;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.kafka.common.utils.ByteUtils;
+import org.openjdk.jmh.annotations.Benchmark;
+import org.openjdk.jmh.annotations.Fork;
+import org.openjdk.jmh.annotations.Level;
+import org.openjdk.jmh.annotations.Measurement;
+import org.openjdk.jmh.annotations.OutputTimeUnit;
+import org.openjdk.jmh.annotations.Scope;
+import org.openjdk.jmh.annotations.Setup;
+import org.openjdk.jmh.annotations.State;
+import org.openjdk.jmh.annotations.Warmup;
+import org.openjdk.jmh.runner.Runner;
+import org.openjdk.jmh.runner.RunnerException;
+import org.openjdk.jmh.runner.options.Options;
+import org.openjdk.jmh.runner.options.OptionsBuilder;
+
+@State(Scope.Benchmark)
+@OutputTimeUnit(TimeUnit.MILLISECONDS)
+public class ByteUtilsBenchmark {
+    private int input;
+
+    @Setup(Level.Iteration)
+    public void setUp() {
+        input = ThreadLocalRandom.current().nextInt(2 * 1024 * 1024);
+    }
+
+    @Benchmark
+    @Fork(3)
+    @Warmup(iterations = 5, time = 1)
+    @Measurement(iterations = 10, time = 1)
+    public int testSizeOfUnsignedVarint() {

Review comment:
       Shall we also add methods for `sizeOfVarlong`?

##########
File path: 
jmh-benchmarks/src/main/java/org/apache/kafka/jmh/util/ByteUtilsBenchmark.java
##########
@@ -0,0 +1,79 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.kafka.jmh.util;
+
+import java.util.concurrent.ThreadLocalRandom;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.kafka.common.utils.ByteUtils;
+import org.openjdk.jmh.annotations.Benchmark;
+import org.openjdk.jmh.annotations.Fork;
+import org.openjdk.jmh.annotations.Level;
+import org.openjdk.jmh.annotations.Measurement;
+import org.openjdk.jmh.annotations.OutputTimeUnit;
+import org.openjdk.jmh.annotations.Scope;
+import org.openjdk.jmh.annotations.Setup;
+import org.openjdk.jmh.annotations.State;
+import org.openjdk.jmh.annotations.Warmup;
+import org.openjdk.jmh.runner.Runner;
+import org.openjdk.jmh.runner.RunnerException;
+import org.openjdk.jmh.runner.options.Options;
+import org.openjdk.jmh.runner.options.OptionsBuilder;
+
+@State(Scope.Benchmark)
+@OutputTimeUnit(TimeUnit.MILLISECONDS)
+public class ByteUtilsBenchmark {
+    private int input;
+
+    @Setup(Level.Iteration)
+    public void setUp() {
+        input = ThreadLocalRandom.current().nextInt(2 * 1024 * 1024);
+    }
+
+    @Benchmark
+    @Fork(3)
+    @Warmup(iterations = 5, time = 1)
+    @Measurement(iterations = 10, time = 1)
+    public int testSizeOfUnsignedVarint() {
+        return ByteUtils.sizeOfUnsignedVarint(input);
+    }
+
+    @Benchmark
+    @Fork(3)
+    @Warmup(iterations = 5, time = 1)
+    @Measurement(iterations = 10, time = 1)
+    public int testSizeOfUnsignedVarintOriginal() {

Review comment:
       Maybe we can say `Simple` instead of `Original` (or whatever name we 
agree for the test, we should use it here too for consistency).

##########
File path: 
clients/src/test/java/org/apache/kafka/common/utils/ByteUtilsTest.java
##########
@@ -239,6 +239,66 @@ public void testDouble() throws IOException {
         assertDoubleSerde(Double.NEGATIVE_INFINITY, 0xFFF0000000000000L);
     }
 
+    private static int mathSizeOfUnsignedVarint(int value) {
+        int leadingZeros = Integer.numberOfLeadingZeros(value);
+        // return (38 - leadingZeros) / 7 + leadingZeros / 32;
+        int leadingZerosBelow38DividedBy7 = ((38 - leadingZeros) * 
0b10010010010010011) >>> 19;
+        return leadingZerosBelow38DividedBy7 + (leadingZeros >>> 5);
+    }

Review comment:
       Why do we need this here since it matches the default implementation?




-- 
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-unsubscr...@kafka.apache.org

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


Reply via email to