scwhittle commented on code in PR #36959:
URL: https://github.com/apache/beam/pull/36959#discussion_r2580927393
##########
sdks/java/core/src/main/java/org/apache/beam/sdk/util/VarInt.java:
##########
@@ -136,16 +136,19 @@ public static long decodeLong(InputStream stream) throws
IOException {
/** Returns the length of the encoding of the given value (in bytes). */
public static int getLength(int v) {
- return getLength(convertIntToLongNoSignExtend(v));
+ // log2(v) / 7 + 1 rewritten as multiplication by 9/64 instead of a
division by 7.
+ // Log2 is performed using a bit counting instruction.
+ // Multiplication by 9 is performed using a 3-bit left shift and add.
+ // Division by 64 is performed using a 6-bit right shift.
+ return ((Integer.SIZE * 9 + (1 << 6)) - (Integer.numberOfLeadingZeros(v) *
9)) >>> 6;
Review Comment:
could we instead call the public static methods on CodedOutputStream?
ie CodedOutputStream.computeUInt32SizeNoTag here and 64 below?
Then if further improvements are made we get them for free.
##########
sdks/java/core/src/main/java/org/apache/beam/sdk/util/VarInt.java:
##########
@@ -136,16 +136,19 @@ public static long decodeLong(InputStream stream) throws
IOException {
/** Returns the length of the encoding of the given value (in bytes). */
public static int getLength(int v) {
- return getLength(convertIntToLongNoSignExtend(v));
+ // log2(v) / 7 + 1 rewritten as multiplication by 9/64 instead of a
division by 7.
Review Comment:
can't add a comment above, but CodedInputStream also unrolls some decoding
that we could do in decodeLong and decodeInt:
https://github.com/protocolbuffers/protobuf/blob/main/java/core/src/main/java/com/google/protobuf/CodedInputStream.java#L1861
encode is also different, it isn't unrolled in protobuf:
https://github.com/protocolbuffers/protobuf/blob/main/java/core/src/main/java/com/google/protobuf/CodedOutputStream.java#L1602
Might be worth putting together a jmh benchmark and copying that over.
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]