chaokunyang commented on code in PR #1890:
URL: https://github.com/apache/fury/pull/1890#discussion_r1805964865


##########
java/fury-core/src/main/java/org/apache/fury/serializer/StringSerializer.java:
##########
@@ -609,6 +638,95 @@ public void writeUTF8String(MemoryBuffer buffer, String 
value) {
     buffer.writeBytes(bytes);
   }
 
+  public static byte bestCoder(char[] chars) {
+    int numChars = chars.length;
+    int vectorizedLen = numChars >> 2;
+    int vectorizedChars = vectorizedLen << 2;
+    // sample 64 chars
+    int sampleNum = Math.min(64, numChars);
+    int endOffset =
+        Math.min(
+            Platform.CHAR_ARRAY_OFFSET + (vectorizedChars << 1),
+            Platform.CHAR_ARRAY_OFFSET + (sampleNum << 1));
+    int count = 0;
+    for (int offset = Platform.CHAR_ARRAY_OFFSET, charOffset = 0;
+        offset < endOffset;
+        offset += 8, charOffset += 4) {
+      long multiChars = Platform.getLong(chars, offset);
+      if ((multiChars & MULTI_CHARS_NON_LATIN_MASK) == 0) {
+        count += 4;
+      } else {
+        for (int i = 0; i < 4; ++i) {
+          if (chars[charOffset + i] < 0x80) {
+            count++;
+          }
+        }
+      }
+    }
+    // ascii number > 50%, choose UTF-8
+    if (count >= sampleNum * 0.5) {
+      return UTF8;
+    } else {
+      return UTF16;
+    }
+  }
+
+  public static byte bestCoder(byte[] bytes) {
+    int numBytes = bytes.length;
+    int vectorizedLen = numBytes >> 3;
+    int vectorizedBytes = vectorizedLen << 3;
+    // sample 64 chars
+    int sampleNum = Math.min(64 << 1, numBytes);
+    int endOffset =
+        Math.min(
+            Platform.BYTE_ARRAY_OFFSET + vectorizedBytes, 
Platform.BYTE_ARRAY_OFFSET + sampleNum);
+    int count = 0;
+    for (int offset = Platform.BYTE_ARRAY_OFFSET; offset < endOffset; offset 
+= 8) {
+      long multiChars = Platform.getLong(bytes, offset);
+      if ((multiChars & MULTI_CHARS_NON_LATIN_MASK) == 0) {
+        count += 4;
+      } else {
+        for (int i = Platform.IS_LITTLE_ENDIAN ? 1 : 0; i < 8; ++i) {

Review Comment:
   I'm not sure whether unrolling will be faster, maybe need a benchmark. One 
drawback is unrolling will make this method have bigger bytecode size. But if 
it's faster, we could afford that.



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