Skip to content

Commit

Permalink
[ruby/securerandom] Increase speed of UUID generation
Browse files Browse the repository at this point in the history
  • Loading branch information
robotblake authored and matzbot committed Dec 29, 2023
1 parent 2b96737 commit e86b4c2
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/random/formatter.rb
Expand Up @@ -168,10 +168,10 @@ def urlsafe_base64(n=nil, padding=false)
# See RFC4122[https://datatracker.ietf.org/doc/html/rfc4122] for details of UUID.
#
def uuid
ary = random_bytes(16).unpack("NnnnnN")
ary[2] = (ary[2] & 0x0fff) | 0x4000
ary[3] = (ary[3] & 0x3fff) | 0x8000
"%08x-%04x-%04x-%04x-%04x%08x" % ary
ary = random_bytes(16)
ary.setbyte(6, (ary.getbyte(6) & 0x0f) | 0x40)
ary.setbyte(8, (ary.getbyte(8) & 0x3f) | 0x80)
ary.unpack("H8H4H4H4H12").join(?-)
end

alias uuid_v4 uuid
Expand Down

0 comments on commit e86b4c2

Please sign in to comment.