Good fast UUID package?

9,447 views
Skip to first unread message

Kevin P

unread,
Sep 1, 2013, 2:23:41 PM9/1/13
to golan...@googlegroups.com
There seem to be a lot of UUID packages (http://godoc.org/?q=uuid).

Can someone recommend a UUID package thats stable and fast? 

Kevin P

unread,
Sep 1, 2013, 4:27:44 PM9/1/13
to golan...@googlegroups.com
I did a quick benchmark of few of them.

https://gist.github.com/CodeMonkeyKevin/6407086

Ryan Smith

unread,
Sep 1, 2013, 11:05:57 PM9/1/13
to golan...@googlegroups.com
I have never needed more performance than is provided by standard tooling. For example:

package main

import (
        "os"
        "fmt"
        "log"
)

var Random *os.File

func init() {
        f, err := os.Open("/dev/urandom")
        if err != nil {
                log.Fatal(err)
        }
        Random = f
}

func uuid() string {
        b := make([]byte, 16)
        Random.Read(b)
        return fmt.Sprintf("%x-%x-%x-%x-%x",
                b[0:4], b[4:6], b[6:8], b[8:10], b[10:])
}

Obviously this implementation doesn't satisfy the more advanced and feature-full versions of UUID implementation. 
I would enjoy seeing this simple function compared to libraries you have already benchmarked. 

Kevin P

unread,
Sep 1, 2013, 11:16:25 PM9/1/13
to golan...@googlegroups.com
Thanks Ryan, thats a very nice solution.

Benchmark_uuid()  500000      4741 ns/op

Ryan Smith

unread,
Sep 1, 2013, 11:50:24 PM9/1/13
to Kevin P, golan...@googlegroups.com
I can not take credit for the solution. I copied it from Russ Cox.



--
You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/Rn13T6BZpgE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Michael Hofmann

unread,
Sep 2, 2013, 4:37:19 AM9/2/13
to golan...@googlegroups.com
This implementation can easily be extended so that it generates valid version 4 UUIDs (see RFC 4122, section 4.4):

func uuid() string {
    b := make([]byte, 16)
    Random.Read(b)
    b[6] = (b[6] & 0x0f) | 0x40
    b[8] = (b[8] & 0x3f) | 0x80
    return fmt.Sprintf("%x-%x-%x-%x-%x", b[0:4], b[4:6], b[6:8], b[8:10], b[10:])
}

I would recommend to use crypto/rand instead of reading /dev/urandom directly though. 

Albert Strasheim

unread,
Sep 2, 2013, 11:55:06 AM9/2/13
to golan...@googlegroups.com
On Sunday, September 1, 2013 8:23:41 PM UTC+2, Kevin P wrote:
There seem to be a lot of UUID packages (http://godoc.org/?q=uuid).
Can someone recommend a UUID package thats stable and fast? 

We have some internal code that I want to open source one day.

Our fastest implementation is based on the idea of using AES CTR to generate random bytes for the UUID:


Also, you might want to think about not using []byte. [16]byte can used as a key in maps but can still be copied into a []byte relatively easily.

Cheers

Albert

Kamil Kisiel

unread,
Sep 3, 2013, 1:44:55 PM9/3/13
to golan...@googlegroups.com
I've had good results with https://github.com/nu7hatch/gouuid

Bryan Whitehead

unread,
Sep 4, 2013, 2:20:25 AM9/4/13
to Kamil Kisiel, golan...@googlegroups.com
This looks promising, just not a go version...
https://github.com/twitter/snowflake/network
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an

Kamil Kisiel

unread,
Sep 4, 2013, 2:27:36 AM9/4/13
to golan...@googlegroups.com, Kamil Kisiel
Looks interesting, but it's like swatting flies with a bazooka in this case :)

Kamil Kisiel

unread,
Sep 4, 2013, 2:28:14 AM9/4/13
to golan...@googlegroups.com
Do you have the code for these benchmarks posted somewhere?


On Sunday, September 1, 2013 1:27:44 PM UTC-7, Kevin P wrote:

Sankar

unread,
Dec 31, 2014, 11:44:02 AM12/31/14
to golan...@googlegroups.com
On Monday, 2 September 2013 14:07:19 UTC+5:30, Michael Hofmann wrote:
This implementation can easily be extended so that it generates valid version 4 UUIDs (see RFC 4122, section 4.4):

func uuid() string {
    b := make([]byte, 16)
    Random.Read(b)
    b[6] = (b[6] & 0x0f) | 0x40
    b[8] = (b[8] & 0x3f) | 0x80
    return fmt.Sprintf("%x-%x-%x-%x-%x", b[0:4], b[4:6], b[6:8], b[8:10], b[10:])
}

I would recommend to use crypto/rand instead of reading /dev/urandom directly though. 


Just curios to know why do you think it is better to use crypto/rand instead of /dev/urandom ? platform independence or any other reason ?

Kevin Malachowski

unread,
Dec 31, 2014, 1:25:16 PM12/31/14
to golan...@googlegroups.com
It probably was for platform independence since the any system that has a /dev/urandom will use that as a source of entropy anyway [0]. If you know you'll be on a system with that and need more than one goroutine to use a random stream you probably should just open it yourself so that goroutines dont have to serialize their access. Although, I dont know what the system does to provide urandom so concurrent reads might not really work for that anyway. Seems like it'd be simple to test out if you're curious enough.


[0] https://golang.org/src/crypto/rand/rand_unix.go

Peter Nguyen

unread,
Jan 5, 2015, 2:10:11 PM1/5/15
to golan...@googlegroups.com

roger peppe

unread,
Jan 6, 2015, 4:39:01 AM1/6/15
to Peter Nguyen, golang-nuts
If speed is of the essence and you don't care about RFC 4122,
you might want to consider github.com/rogpeppe/fastuuid.

I benchmarked it at 40ns to generate a new UUID versus 1518ns
for github.com/m4rw3r/uuid. It uses only minimal entropy from /dev/random
too.

It will work well for generating nonces for golang.org/x/crypto/nacl/secretbox
for example.

   cheers,
     rog.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages