Merge pull request #13 from bmatsuo/optimizations

Optimizations
diff --git a/json.go b/json.go
index 760580a..9dda1df 100644
--- a/json.go
+++ b/json.go
@@ -7,17 +7,21 @@
 import "errors"
 
 func (u UUID) MarshalJSON() ([]byte, error) {
-	if len(u) == 0 {
+	if len(u) != 16 {
 		return []byte(`""`), nil
 	}
-	return []byte(`"` + u.String() + `"`), nil
+	var js [38]byte
+	js[0] = '"'
+	encodeHex(js[1:], u)
+	js[37] = '"'
+	return js[:], nil
 }
 
 func (u *UUID) UnmarshalJSON(data []byte) error {
-	if len(data) == 0 || string(data) == `""` {
+	if string(data) == `""` {
 		return nil
 	}
-	if len(data) < 2 || data[0] != '"' || data[len(data)-1] != '"' {
+	if data[0] != '"' {
 		return errors.New("invalid UUID format")
 	}
 	data = data[1 : len(data)-1]
diff --git a/json_test.go b/json_test.go
index b5eae09..2866b8d 100644
--- a/json_test.go
+++ b/json_test.go
@@ -30,3 +30,32 @@
 		t.Errorf("got %#v, want %#v", s2, s1)
 	}
 }
+
+func BenchmarkUUID_MarshalJSON(b *testing.B) {
+	x := &struct {
+		UUID UUID `json:"uuid"`
+	}{}
+	x.UUID = Parse("f47ac10b-58cc-0372-8567-0e02b2c3d479")
+	if x.UUID == nil {
+		b.Fatal("invalid uuid")
+	}
+	for i := 0; i < b.N; i++ {
+		js, err := json.Marshal(x)
+		if err != nil {
+			b.Fatalf("marshal json: %#v (%v)", js, err)
+		}
+	}
+}
+
+func BenchmarkUUID_UnmarshalJSON(b *testing.B) {
+	js := []byte(`{"uuid":"f47ac10b-58cc-0372-8567-0e02b2c3d479"}`)
+	var x *struct {
+		UUID UUID `json:"uuid"`
+	}
+	for i := 0; i < b.N; i++ {
+		err := json.Unmarshal(js, &x)
+		if err != nil {
+			b.Fatalf("marshal json: %#v (%v)", js, err)
+		}
+	}
+}
diff --git a/util.go b/util.go
index de40b10..fc8e052 100644
--- a/util.go
+++ b/util.go
@@ -16,7 +16,7 @@
 }
 
 // xvalues returns the value of a byte as a hexadecimal digit or 255.
-var xvalues = []byte{
+var xvalues = [256]byte{
 	255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
 	255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
 	255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
diff --git a/uuid.go b/uuid.go
old mode 100755
new mode 100644
index 11c8184..c4482cd
--- a/uuid.go
+++ b/uuid.go
@@ -7,6 +7,7 @@
 import (
 	"bytes"
 	"crypto/rand"
+	"encoding/hex"
 	"fmt"
 	"io"
 	"strings"
@@ -54,8 +55,8 @@
 	if s[8] != '-' || s[13] != '-' || s[18] != '-' || s[23] != '-' {
 		return nil
 	}
-	uuid := make([]byte, 16)
-	for i, x := range []int{
+	var uuid [16]byte
+	for i, x := range [16]int{
 		0, 2, 4, 6,
 		9, 11,
 		14, 16,
@@ -67,7 +68,7 @@
 			uuid[i] = v
 		}
 	}
-	return uuid
+	return uuid[:]
 }
 
 // Equal returns true if uuid1 and uuid2 are equal.
@@ -78,23 +79,36 @@
 // String returns the string form of uuid, xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
 // , or "" if uuid is invalid.
 func (uuid UUID) String() string {
-	if uuid == nil || len(uuid) != 16 {
+	if len(uuid) != 16 {
 		return ""
 	}
-	b := []byte(uuid)
-	return fmt.Sprintf("%08x-%04x-%04x-%04x-%012x",
-		b[:4], b[4:6], b[6:8], b[8:10], b[10:])
+	var buf [36]byte
+	encodeHex(buf[:], uuid)
+	return string(buf[:])
 }
 
 // URN returns the RFC 2141 URN form of uuid,
 // urn:uuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx,  or "" if uuid is invalid.
 func (uuid UUID) URN() string {
-	if uuid == nil || len(uuid) != 16 {
+	if len(uuid) != 16 {
 		return ""
 	}
-	b := []byte(uuid)
-	return fmt.Sprintf("urn:uuid:%08x-%04x-%04x-%04x-%012x",
-		b[:4], b[4:6], b[6:8], b[8:10], b[10:])
+	var buf [36 + 9]byte
+	copy(buf[:], "urn:uuid:")
+	encodeHex(buf[9:], uuid)
+	return string(buf[:])
+}
+
+func encodeHex(dst []byte, uuid UUID) {
+	hex.Encode(dst[:], uuid[:4])
+	dst[8] = '-'
+	hex.Encode(dst[9:13], uuid[4:6])
+	dst[13] = '-'
+	hex.Encode(dst[14:18], uuid[6:8])
+	dst[18] = '-'
+	hex.Encode(dst[19:23], uuid[8:10])
+	dst[23] = '-'
+	hex.Encode(dst[24:], uuid[10:])
 }
 
 // Variant returns the variant encoded in uuid.  It returns Invalid if
diff --git a/uuid_test.go b/uuid_test.go
old mode 100755
new mode 100644
index 3c6834b..3835cc8
--- a/uuid_test.go
+++ b/uuid_test.go
@@ -430,3 +430,42 @@
 		t.Errorf("unexecpted duplicates, got %q", uuid1)
 	}
 }
+
+func BenchmarkParse(b *testing.B) {
+	for i := 0; i < b.N; i++ {
+		uuid := Parse("f47ac10b-58cc-0372-8567-0e02b2c3d479")
+		if uuid == nil {
+			b.Fatal("invalid uuid")
+		}
+	}
+}
+
+func BenchmarkNew(b *testing.B) {
+	for i := 0; i < b.N; i++ {
+		New()
+	}
+}
+
+func BenchmarkUUID_String(b *testing.B) {
+	uuid := Parse("f47ac10b-58cc-0372-8567-0e02b2c3d479")
+	if uuid == nil {
+		b.Fatal("invalid uuid")
+	}
+	for i := 0; i < b.N; i++ {
+		if uuid.String() == "" {
+			b.Fatal("invalid uuid")
+		}
+	}
+}
+
+func BenchmarkUUID_URN(b *testing.B) {
+	uuid := Parse("f47ac10b-58cc-0372-8567-0e02b2c3d479")
+	if uuid == nil {
+		b.Fatal("invalid uuid")
+	}
+	for i := 0; i < b.N; i++ {
+		if uuid.URN() == "" {
+			b.Fatal("invalid uuid")
+		}
+	}
+}