Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6202042

dead code in java.util.UUID.randomUUID()

XMLWordPrintable

    • b17
    • generic, x86
    • generic, linux, windows_xp

      Bill Pugh writes:

      We've been working on tuning the dead store detector, and came across
      this
      in java.util.UUID:

           /**
            * Static factory to retrieve a type 4 (pseudo randomly generated)
      UUID.
            *
            * The <code>UUID</code> is generated using a cryptographically
      strong
            * pseudo random number generator.
            *
            * @return a randomly generated <tt>UUID</tt>.
            */
           public static UUID randomUUID() {
               SecureRandom ng = numberGenerator;
               if (ng == null) {
                   numberGenerator = ng = new SecureRandom();
               }

               byte[] randomBytes = new byte[16];
               ng.nextBytes(randomBytes);
               randomBytes[6] &= 0x0f; /* clear version */
               randomBytes[6] |= 0x40; /* set to version 4 */
               randomBytes[8] &= 0x3f; /* clear variant */
               randomBytes[8] |= 0x80; /* set to IETF variant */
               UUID result = new UUID(randomBytes);
               return new UUID(randomBytes);
           }



      Result of this is that for each call, you create two UUID objects, one
      of which is thrown away.

      Only a performance penalty, rather than correctness. But still, it
      should be fixed.



      Bill


            martin Martin Buchholz
            martin Martin Buchholz
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: