Repository: qpid-proton-j
Updated Branches:
  refs/heads/master a9988721e -> 589504d76


PROTON-1963 Refactor string encoding size calculation

Assume ASCII by default when calculating the string size in UTF8 to
allow for inlining of this method in most cases and fallback to a
multi-byte calculation only when non-ASCII chars are detected.

Project: http://git-wip-us.apache.org/repos/asf/qpid-proton-j/repo
Commit: http://git-wip-us.apache.org/repos/asf/qpid-proton-j/commit/391af064
Tree: http://git-wip-us.apache.org/repos/asf/qpid-proton-j/tree/391af064
Diff: http://git-wip-us.apache.org/repos/asf/qpid-proton-j/diff/391af064

Branch: refs/heads/master
Commit: 391af064e2d50dfd29700f160166e86d8f4cba19
Parents: a998872
Author: Timothy Bish <tabish...@gmail.com>
Authored: Tue Nov 13 14:53:13 2018 -0500
Committer: Timothy Bish <tabish...@gmail.com>
Committed: Tue Nov 13 14:53:13 2018 -0500

----------------------------------------------------------------------
 .../apache/qpid/proton/codec/StringType.java    | 32 +++++++++++++++-----
 1 file changed, 24 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/qpid-proton-j/blob/391af064/proton-j/src/main/java/org/apache/qpid/proton/codec/StringType.java
----------------------------------------------------------------------
diff --git 
a/proton-j/src/main/java/org/apache/qpid/proton/codec/StringType.java 
b/proton-j/src/main/java/org/apache/qpid/proton/codec/StringType.java
index 7687ca4..85448b3 100644
--- a/proton-j/src/main/java/org/apache/qpid/proton/codec/StringType.java
+++ b/proton-j/src/main/java/org/apache/qpid/proton/codec/StringType.java
@@ -84,26 +84,42 @@ public class StringType extends 
AbstractPrimitiveType<String>
 
     static int calculateUTF8Length(final String s)
     {
-        int len = s.length();
-        final int length = len;
-        for (int i = 0; i < length; i++)
+        final int stringLength = s.length();
+
+        // ASCII Optimized length case
+        int utf8len = stringLength;
+        int processed = 0;
+        for (; processed < stringLength && s.charAt(processed) < 0x80; 
processed++) {}
+
+        if (processed < stringLength)
+        {
+            // Non-ASCII length remainder
+            utf8len = extendedCalculateUTF8Length(s, processed, stringLength, 
utf8len);
+        }
+
+        return utf8len;
+    }
+
+    static int extendedCalculateUTF8Length(final String s, int index, int 
length, int utf8len) {
+        for (; index < length; index++)
         {
-            int c = s.charAt(i);
+            int c = s.charAt(index);
             if ((c & 0xFF80) != 0)         /* U+0080..    */
             {
-                len++;
+                utf8len++;
                 if(((c & 0xF800) != 0))    /* U+0800..    */
                 {
-                    len++;
+                    utf8len++;
                     // surrogate pairs should always combine to create a code 
point with a 4 octet representation
                     if ((c & 0xD800) == 0xD800 && c < 0xDC00)
                     {
-                        i++;
+                        index++;
                     }
                 }
             }
         }
-        return len;
+
+        return utf8len;
     }
 
     @Override


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@qpid.apache.org
For additional commands, e-mail: commits-h...@qpid.apache.org

Reply via email to