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


##########
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) {
+          if (bytes[offset + i] == 0) {
+            count++;
+          }
+        }
+      }
+    }
+    // ascii number > 50%, choose UTF-8
+    if (count >= sampleNum * 0.5) {
+      return UTF8;
+    } else {
+      return UTF16;
+    }
+  }
+
+  public void fastWriteString(MemoryBuffer buffer, String value) {
+    if (STRING_VALUE_FIELD_IS_BYTES) {
+      final byte[] bytes = (byte[]) Platform.getObject(value, 
STRING_VALUE_FIELD_OFFSET);
+      final byte coder = Platform.getByte(value, 
Offset.STRING_CODER_FIELD_OFFSET);
+      if (coder == LATIN1) {
+        // LATIN1
+        writeBytesLatin1(buffer, bytes);
+      } else {
+        if (bestCoder(bytes) == UTF8) {
+          writeBytesUTF8(buffer, bytes);
+        } else {
+          writeBytesUTF16(buffer, bytes);
+        }
+      }
+    } else {
+      final char[] chars = (char[]) Platform.getObject(value, 
STRING_VALUE_FIELD_OFFSET);
+      if (StringUtils.isLatin(chars)) {

Review Comment:
   Yes. it is a good suggestion.



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