rionmonster commented on code in PR #30272:
URL: https://github.com/apache/flink/pull/30272#discussion_r4809199092


##########
flink-core/src/main/java/org/apache/flink/util/Utf8Utils.java:
##########
@@ -88,19 +88,17 @@ private static int decode4ByteSequence(int b1, int b2, int 
b3, int b4) {
      */
     public static int firstInvalidUtf8ByteIndex(
             final byte[] bytes, final int offset, final int numBytes) {
-        int sp = offset;
-        final int sl = sp + numBytes;
-
-        // ASCII fast-path
-        while (sp < sl && isAsciiByte(bytes[sp])) {
-            sp++;
-        }
+        final int sl = offset + numBytes;
+        // ASCII fast-path, a word at a time
+        int sp = offset + SwarUtils.asciiPrefix(bytes, offset, numBytes);
 
         while (sp < sl) {
             final int start = sp;
             final int b1 = bytes[sp++];
 
             if (isAsciiByte(b1)) {
+                // the rest of this ASCII run, a word at a time
+                sp += SwarUtils.asciiPrefix(bytes, sp, sl - sp);

Review Comment:
   I tested through this locally and it seems the restart negatively affected 
mixed text, specifically with short ASCII:
   
   | Input                                   | This PR vs master |
   |-----------------------------------------|-------------------|
   | ASCII                                   | 15-22x            |
   | Mostly-ASCII JSON (e.g. {"name":"José",...}) | ~2.5x             |
   | CJK + punctuation                       | ~1.0x             |
   | Cyrillic + spaces (e.g. привет мир как дела) | ~0.45x            |
   | Accented Latin (e.g. café crème brûlée)      | ~0.45x            |
   
   I think it's the per-call setup for 1-5 byte runs that the old loop handled 
with a single compare. What does the `ascii=false` look like in your 
benchmarks? Is it worth adding a mixed case like these there? I tinkered with a 
few things on this end (like scanning the first few words byte-by-byte), which 
clawed quite a bit of the regression back (~0.9x as opposed to ~0.45x). 
   
   Something like:
   ```
   final int shortEnd = Math.min(sl, sp + 2 * Long.BYTES);
   while (sp < shortEnd && isAsciiByte(bytes[sp])) {
       sp++;
   }
   if (sp == shortEnd) {
       sp += SwarUtils.asciiPrefix(bytes, sp, sl - sp);
   }
   ```



##########
flink-core/src/main/java/org/apache/flink/util/Utf8Utils.java:
##########
@@ -88,19 +88,17 @@ private static int decode4ByteSequence(int b1, int b2, int 
b3, int b4) {
      */
     public static int firstInvalidUtf8ByteIndex(
             final byte[] bytes, final int offset, final int numBytes) {
-        int sp = offset;
-        final int sl = sp + numBytes;
-
-        // ASCII fast-path
-        while (sp < sl && isAsciiByte(bytes[sp])) {
-            sp++;
-        }
+        final int sl = offset + numBytes;
+        // ASCII fast-path, a word at a time
+        int sp = offset + SwarUtils.asciiPrefix(bytes, offset, numBytes);
 
         while (sp < sl) {
             final int start = sp;
             final int b1 = bytes[sp++];

Review Comment:
   Very minor nit: it was a _bit_ hard to follow all of the deeply truncated 
variable names (e.g., sl, sp, b1), which I suppose is fine for internals that 
no one will _typically_ tinker with as opposed to more human-readable ones 
(e.g., length, position, ... no idea on current byte).



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

Reply via email to