Jason Koch created KAFKA-13629:
----------------------------------

             Summary: Use faster ByteUtils sizeOfXxx algorithm
                 Key: KAFKA-13629
                 URL: https://issues.apache.org/jira/browse/KAFKA-13629
             Project: Kafka
          Issue Type: Improvement
          Components: clients, producer 
            Reporter: Jason Koch


Kafka Java client producer path uses `tryAppend()` requires an estimated size. 
The sizeOf algorithm currently uses an [iterative while 
loop|https://github.com/apache/kafka/blob/trunk/clients/src/main/java/org/apache/kafka/common/utils/ByteUtils.java#L394-L401]
 which can be improved.

On real system this reduces the producer path CPU by 4%. JMH benchmarking shows 
the algorithm is approx 3x faster (90 op/ms -> 290op/ms).

Specifically, we can use a prepared table with the following lookup which 
relies on JVM intrinsic for fast performance, and also avoids any branching:.

--

{{int leadingZeros = Integer.numberOfLeadingZeros(value);}}
{{return LEADING_ZEROS_TO_U_VARINT_SIZE[leadingZeros];}}

--



--
This message was sent by Atlassian Jira
(v8.20.1#820001)

Reply via email to