changeset 13668:c3da0d44c900

8148936: Adapt UUID.toString() to Compact Strings Reviewed-by: igerasim, redestad
author shade
date Thu, 04 Feb 2016 16:00:48 +0300
parents d1f14fc9591b
children 44d28eb7dae9
files src/java.base/share/classes/java/lang/Long.java src/java.base/share/classes/java/lang/System.java src/java.base/share/classes/java/util/UUID.java src/java.base/share/classes/jdk/internal/misc/JavaLangAccess.java test/java/util/UUID/UUIDTest.java
diffstat 5 files changed, 41 insertions(+), 36 deletions(-) [+]
line wrap: on
line diff
--- a/src/java.base/share/classes/java/lang/Long.java	Thu Feb 04 09:51:24 2016 +0100
+++ b/src/java.base/share/classes/java/lang/Long.java	Thu Feb 04 16:00:48 2016 +0300
@@ -401,18 +401,6 @@
      * @param offset the offset in the destination buffer to start at
      * @param len the number of characters to write
      */
-     static void formatUnsignedLong(long val, int shift, char[] buf, int offset, int len) {
-        // assert shift > 0 && shift <=5 : "Illegal shift value";
-        // assert offset >= 0 && offset < buf.length : "illegal offset";
-        // assert len > 0 && (offset + len) <= buf.length : "illegal length";
-        int charPos = offset + len;
-        int radix = 1 << shift;
-        int mask = radix - 1;
-        do {
-            buf[--charPos] = Integer.digits[((int) val) & mask];
-            val >>>= shift;
-        } while (charPos > offset);
-    }
 
     /** byte[]/LATIN1 version    */
     static void formatUnsignedLong0(long val, int shift, byte[] buf, int offset, int len) {
@@ -436,6 +424,39 @@
         } while (charPos > offset);
     }
 
+    static String fastUUID(long lsb, long msb) {
+        if (COMPACT_STRINGS) {
+            byte[] buf = new byte[36];
+            formatUnsignedLong0(lsb,        4, buf, 24, 12);
+            formatUnsignedLong0(lsb >>> 48, 4, buf, 19, 4);
+            formatUnsignedLong0(msb,        4, buf, 14, 4);
+            formatUnsignedLong0(msb >>> 16, 4, buf, 9,  4);
+            formatUnsignedLong0(msb >>> 32, 4, buf, 0,  8);
+
+            buf[23] = '-';
+            buf[18] = '-';
+            buf[13] = '-';
+            buf[8]  = '-';
+
+            return new String(buf, LATIN1);
+        } else {
+            byte[] buf = new byte[72];
+
+            formatUnsignedLong0UTF16(lsb,        4, buf, 24, 12);
+            formatUnsignedLong0UTF16(lsb >>> 48, 4, buf, 19, 4);
+            formatUnsignedLong0UTF16(msb,        4, buf, 14, 4);
+            formatUnsignedLong0UTF16(msb >>> 16, 4, buf, 9,  4);
+            formatUnsignedLong0UTF16(msb >>> 32, 4, buf, 0,  8);
+
+            StringUTF16.putChar(buf, 23, '-');
+            StringUTF16.putChar(buf, 18, '-');
+            StringUTF16.putChar(buf, 13, '-');
+            StringUTF16.putChar(buf,  8, '-');
+
+            return new String(buf, UTF16);
+        }
+    }
+
     /**
      * Returns a {@code String} object representing the specified
      * {@code long}.  The argument is converted to signed decimal
--- a/src/java.base/share/classes/java/lang/System.java	Thu Feb 04 09:51:24 2016 +0100
+++ b/src/java.base/share/classes/java/lang/System.java	Thu Feb 04 16:00:48 2016 +0300
@@ -1911,11 +1911,8 @@
             public void invokeFinalize(Object o) throws Throwable {
                 o.finalize();
             }
-            public void formatUnsignedLong(long val, int shift, char[] buf, int offset, int len) {
-                Long.formatUnsignedLong(val, shift, buf, offset, len);
-            }
-            public void formatUnsignedInt(int val, int shift, char[] buf, int offset, int len) {
-                Integer.formatUnsignedInt(val, shift, buf, offset, len);
+            public String fastUUID(long lsb, long msb) {
+                return Long.fastUUID(lsb, msb);
             }
         });
     }
--- a/src/java.base/share/classes/java/util/UUID.java	Thu Feb 04 09:51:24 2016 +0100
+++ b/src/java.base/share/classes/java/util/UUID.java	Thu Feb 04 16:00:48 2016 +0300
@@ -392,17 +392,7 @@
      * @return  A string representation of this {@code UUID}
      */
     public String toString() {
-        char[] chars = new char[36];
-        jla.formatUnsignedLong(mostSigBits >> 32, 4, chars, 0, 8);
-        chars[8] = '-';
-        jla.formatUnsignedLong(mostSigBits >> 16, 4, chars, 9, 4);
-        chars[13] = '-';
-        jla.formatUnsignedLong(mostSigBits, 4, chars, 14, 4);
-        chars[18] = '-';
-        jla.formatUnsignedLong(leastSigBits >> 48, 4, chars, 19, 4);
-        chars[23] = '-';
-        jla.formatUnsignedLong(leastSigBits, 4, chars, 24, 12);
-        return jla.newStringUnsafe(chars);
+        return jla.fastUUID(leastSigBits, mostSigBits);
     }
 
     /**
--- a/src/java.base/share/classes/jdk/internal/misc/JavaLangAccess.java	Thu Feb 04 09:51:24 2016 +0100
+++ b/src/java.base/share/classes/jdk/internal/misc/JavaLangAccess.java	Thu Feb 04 16:00:48 2016 +0300
@@ -124,12 +124,7 @@
     void invokeFinalize(Object o) throws Throwable;
 
     /**
-     * Invokes Long.formatUnsignedLong(long val, int shift, char[] buf, int offset, int len)
+     * Invokes Long.fastUUID
      */
-    void formatUnsignedLong(long val, int shift, char[] buf, int offset, int len);
-
-    /**
-     * Invokes Integer.formatUnsignedInt(long val, int shift, char[] buf, int offset, int len)
-     */
-    void formatUnsignedInt(int val, int shift, char[] buf, int offset, int len);
+    String fastUUID(long lsb, long msb);
 }
--- a/test/java/util/UUID/UUIDTest.java	Thu Feb 04 09:51:24 2016 +0100
+++ b/test/java/util/UUID/UUIDTest.java	Thu Feb 04 16:00:48 2016 +0300
@@ -22,9 +22,11 @@
  */
 
 /* @test
- * @bug 4173528 5068772
+ * @bug 4173528 5068772 8148936
  * @summary Unit tests for java.util.UUID
  * @key randomness
+ * @run main/othervm -XX:+CompactStrings UUIDTest
+ * @run main/othervm -XX:-CompactStrings UUIDTest
  */
 
 import java.util.*;