LiangliangSui commented on code in PR #1450:
URL: https://github.com/apache/incubator-fury/pull/1450#discussion_r1547815390


##########
java/fury-core/src/main/java/org/apache/fury/memory/MemoryBuffer.java:
##########
@@ -1316,24 +1316,15 @@ public int readPositiveVarInt() {
   }
 
   private int readPositiveVarIntSlow() {
-    int b = readByte();
-    int result = b & 0x7F;
-    if ((b & 0x80) != 0) {
+    int shift = 0;

Review Comment:
   Use the following code to run the benchmark
   ```java
   package org.example;
   
   import org.openjdk.jmh.annotations.*;
   import org.openjdk.jmh.results.format.ResultFormatType;
   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;
   
   import java.util.concurrent.TimeUnit;
   
   @BenchmarkMode(Mode.Throughput)
   @OutputTimeUnit(TimeUnit.SECONDS)
   @Fork(5)
   @Warmup(iterations = 3, time = 2, timeUnit = TimeUnit.SECONDS)
   @Measurement(iterations = 3, time = 2, timeUnit = TimeUnit.SECONDS)
   @State(Scope.Benchmark)
   @CompilerControl(CompilerControl.Mode.INLINE)
   public class Main {
     public static void main(String[] args) throws RunnerException {
       Options opt = new OptionsBuilder()
           .include(Main.class.getName())
           .resultFormat(ResultFormatType.CSV)
           .build();
   
       new Runner(opt).run();
     }
   
     private byte getBytes(int shift) {
       if (shift == 4) {
         return 0x75;
       }
   
       return (byte) 0x85;
     }
   
     @Benchmark
     public int readPositiveVarIntSlowBefore() {
       int b = getBytes(0);
       int result = b & 0x7F;
       if ((b & 0x80) != 0) {
         b = getBytes(1);
         result |= (b & 0x7F) << 7;
         if ((b & 0x80) != 0) {
           b = getBytes(2);
           result |= (b & 0x7F) << 14;
           if ((b & 0x80) != 0) {
             b = getBytes(3);
             result |= (b & 0x7F) << 21;
             if ((b & 0x80) != 0) {
               b = getBytes(4);
               result |= (b & 0x7F) << 28;
             }
           }
         }
       }
       return result;
     }
   
   
     @Benchmark
     public int readPositiveVarIntSlowAfter() {
       int shift = 0;
       int result = 0;
       byte b;
       do {
         b = getBytes(shift);
         result |= (b & 0x7f) << (shift * 7);
         shift++;
       } while ((b & 0x80) != 0);
   
       return result;
     }
   }
   ```
   
   
![image](https://github.com/apache/incubator-fury/assets/116876207/ffb1315e-dc30-4cc6-9e42-0ca65a7d80d5)
   
   
   The performance before modification is far better than the performance after 
modification.
   
   But this coding method affects the readability of the code. Should we add 
some comments to explain why we do this, so that we can also convey the purpose 
of this behavior to subsequent developers?



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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to