-
Notifications
You must be signed in to change notification settings - Fork 15.1k
KAFKA-13629: Use faster algorithm for ByteUtils sizeOfXxx algorithm #11721
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b856d3a
KAFKA-13629: use intrinsics for ByteUtils sizeOfXxx algorithm
jasonk000 87d4b19
add benchmark to compare math-based vs lookup-based sizeof calc
jasonk000 bc1afab
add benchmarks of single-iterations to avoid loop unroll
jasonk000 d6aeeb1
remove benchmarks with loops
jasonk000 f040109
switch implementation to use bit-math instead of a lookup table
jasonk000 e891ebf
pr feedback, rename and tidy up code and comments
jasonk000 9dbff97
pr feedback, rename and tidy up comments
jasonk000 dc88a0f
fix spotbugs issue in benchmark
jasonk000 329674e
Remove unnecessary comment
ijuma File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -390,14 +390,25 @@ public static void writeDouble(double value, ByteBuffer buffer) { | |
| * Number of bytes needed to encode an integer in unsigned variable-length format. | ||
| * | ||
| * @param value The signed value | ||
| * | ||
| * @see #writeUnsignedVarint(int, DataOutput) | ||
| */ | ||
| public static int sizeOfUnsignedVarint(int value) { | ||
| int bytes = 1; | ||
| while ((value & 0xffffff80) != 0L) { | ||
| bytes += 1; | ||
| value >>>= 7; | ||
| } | ||
| return bytes; | ||
| // Protocol buffers varint encoding is variable length, with a minimum of 1 byte | ||
| // (for zero). The values themselves are not important. What's important here is | ||
| // any leading zero bits are dropped from output. We can use this leading zero | ||
| // count w/ fast intrinsic to calc the output length directly. | ||
|
|
||
| // Test cases verify this matches the output for loop logic exactly. | ||
|
|
||
| // return (38 - leadingZeros) / 7 + leadingZeros / 32; | ||
|
|
||
| // The above formula provides the implementation, but the Java encoding is suboptimal | ||
| // when we have a narrow range of integers, so we can do better manually | ||
|
|
||
| int leadingZeros = Integer.numberOfLeadingZeros(value); | ||
| int leadingZerosBelow38DividedBy7 = ((38 - leadingZeros) * 0b10010010010010011) >>> 19; | ||
| return leadingZerosBelow38DividedBy7 + (leadingZeros >>> 5); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -413,15 +424,18 @@ public static int sizeOfVarint(int value) { | |
| * Number of bytes needed to encode a long in variable-length format. | ||
| * | ||
| * @param value The signed value | ||
| * @see #sizeOfUnsignedVarint(int) | ||
| */ | ||
| public static int sizeOfVarlong(long value) { | ||
| long v = (value << 1) ^ (value >> 63); | ||
| int bytes = 1; | ||
| while ((v & 0xffffffffffffff80L) != 0L) { | ||
| bytes += 1; | ||
| v >>>= 7; | ||
| } | ||
| return bytes; | ||
|
|
||
| // For implementation notes @see #sizeOfUnsignedVarint(int) | ||
| // Similar logic is applied to allow for 64bit input -> 1-9byte output. | ||
| // return (70 - leadingZeros) / 7 + leadingZeros / 64; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: not sure we need the empty lines between comments here, seems pretty readable without them. |
||
|
|
||
| int leadingZeros = Long.numberOfLeadingZeros(v); | ||
| int leadingZerosBelow70DividedBy7 = ((70 - leadingZeros) * 0b10010010010010011) >>> 19; | ||
| return leadingZerosBelow70DividedBy7 + (leadingZeros >>> 6); | ||
| } | ||
|
|
||
| private static IllegalArgumentException illegalVarintException(int value) { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
jmh-benchmarks/src/main/java/org/apache/kafka/jmh/util/ByteUtilsBenchmark.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| /* | ||
| * 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) | ||
| @Fork(3) | ||
| @Warmup(iterations = 5, time = 1) | ||
| @Measurement(iterations = 10, time = 1) | ||
| public class ByteUtilsBenchmark { | ||
| private int inputInt; | ||
| private long inputLong; | ||
| @Setup(Level.Iteration) | ||
| public void setUp() { | ||
| inputInt = ThreadLocalRandom.current().nextInt(); | ||
| inputLong = ThreadLocalRandom.current().nextLong(); | ||
| } | ||
|
|
||
| @Benchmark | ||
| public int testSizeOfUnsignedVarint() { | ||
|
ijuma marked this conversation as resolved.
|
||
| return ByteUtils.sizeOfUnsignedVarint(inputInt); | ||
| } | ||
|
|
||
| @Benchmark | ||
| public int testSizeOfUnsignedVarintSimple() { | ||
| int value = inputInt; | ||
| int bytes = 1; | ||
| while ((value & 0xffffff80) != 0L) { | ||
| bytes += 1; | ||
| value >>>= 7; | ||
| } | ||
|
jasonk000 marked this conversation as resolved.
|
||
| return bytes; | ||
| } | ||
|
|
||
| @Benchmark | ||
| public int testSizeOfVarlong() { | ||
|
jasonk000 marked this conversation as resolved.
|
||
| return ByteUtils.sizeOfVarlong(inputLong); | ||
| } | ||
|
|
||
| @Benchmark | ||
| public int testSizeOfVarlongSimple() { | ||
| long v = (inputLong << 1) ^ (inputLong >> 63); | ||
| int bytes = 1; | ||
| while ((v & 0xffffffffffffff80L) != 0L) { | ||
| bytes += 1; | ||
| v >>>= 7; | ||
| } | ||
| return bytes; | ||
| } | ||
|
|
||
| public static void main(String[] args) throws RunnerException { | ||
| Options opt = new OptionsBuilder() | ||
| .include(ByteUtilsBenchmark.class.getSimpleName()) | ||
| .forks(2) | ||
| .build(); | ||
|
|
||
| new Runner(opt).run(); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.