You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@fury.apache.org by "chaokunyang (via GitHub)" <gi...@apache.org> on 2024/04/01 16:06:17 UTC

Re: [PR] chore(java): Reuse unsafePutPositiveVarInt in unsafeWritePositiveVarInt [incubator-fury]

chaokunyang commented on code in PR #1434:
URL: https://github.com/apache/incubator-fury/pull/1434#discussion_r1546512637


##########
java/fury-core/src/main/java/org/apache/fury/memory/MemoryBuffer.java:
##########
@@ -1233,49 +1233,9 @@ public int readVarInt() {
    * to avoid using two memory operations.
    */
   public int unsafeWritePositiveVarInt(int v) {
-    // The encoding algorithm are based on kryo UnsafeMemoryOutput.writeVarInt
-    // varint are written using little endian byte order.
-    // This version should have better performance since it remove an index update.
-    long value = v;
-    final int writerIndex = this.writerIndex;
-    long varInt = (value & 0x7F);
-    value >>>= 7;
-    if (value == 0) {
-      UNSAFE.putByte(heapMemory, address + writerIndex, (byte) varInt);
-      this.writerIndex = writerIndex + 1;
-      return 1;
-    }
-    // bit 8 `set` indicates have next data bytes.
-    varInt |= 0x80;
-    varInt |= ((value & 0x7F) << 8);
-    value >>>= 7;
-    if (value == 0) {
-      unsafePutInt(writerIndex, (int) varInt);
-      this.writerIndex = writerIndex + 2;
-      return 2;
-    }
-    varInt |= (0x80 << 8);
-    varInt |= ((value & 0x7F) << 16);
-    value >>>= 7;
-    if (value == 0) {
-      unsafePutInt(writerIndex, (int) varInt);
-      this.writerIndex = writerIndex + 3;
-      return 3;
-    }
-    varInt |= (0x80 << 16);
-    varInt |= ((value & 0x7F) << 24);
-    value >>>= 7;
-    if (value == 0) {
-      unsafePutInt(writerIndex, (int) varInt);
-      this.writerIndex = writerIndex + 4;
-      return 4;
-    }
-    varInt |= (0x80L << 24);
-    varInt |= ((value & 0x7F) << 32);
-    varInt &= 0xFFFFFFFFFL;
-    unsafePutLong(writerIndex, varInt);
-    this.writerIndex = writerIndex + 5;
-    return 5;
+    int varintBytes = unsafePutPositiveVarInt(writerIndex, v);

Review Comment:
   JVM JIT inline has max depth limit, which is 9 by default. Reuse this method may disabled some jit inline. Could we benchmark `org.apache.fury.benchmark.UserTypeDeserializeSuite` to check this code reuse doesn't introduce a preformance regression?
   
   ```java
     public static void main(String[] args) throws IOException {
       if (args.length == 0) {
         String commandLine =
             "io.*UserTypeDeserializeSuite.fury* -f 0 -wi 3 -i 3 -t 1 -w 2s -r 2s -rf csv";
         System.out.println(commandLine);
         args = commandLine.split(" ");
       }
       Main.main(args);
     }
   ```
   This will run fury serialization only



-- 
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: commits-unsubscribe@fury.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@fury.apache.org
For additional commands, e-mail: commits-help@fury.apache.org