opitimize json interface methods
A common helper function is used for UUID.String(), UUID.URN(), and
UUID.MarshalJSON(). Any perforance hit in UUID.String() and
UUID.Marshal() appears to be negligable. The benefit to
UUID.MarshalJSON() is several hundred nanoseconds (23% faster) and 2
allocations (21% fewer bytes).
Some redundant checks are removed from UUID.UnmarshalJSON() method. The
"encoding/json".Unmarshaler interface specifies that implementations can
assume input is valid JSON content. This allows one to assume that (1)
input is not empty and (2) if index 0 is a quote, then the content is a
json string and the last index will contain a terminating quote. The
second point is not completely explicit in the documentation but it is
true in practice (and it is safe to assume -- errors will be caught).
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/uuid.go b/uuid.go
index 3e28a6c..10af11e 100644
--- a/uuid.go
+++ b/uuid.go
@@ -83,15 +83,7 @@
return ""
}
var buf [36]byte
- hex.Encode(buf[:], uuid[:4])
- buf[8] = '-'
- hex.Encode(buf[9:13], uuid[4:6])
- buf[13] = '-'
- hex.Encode(buf[14:18], uuid[6:8])
- buf[18] = '-'
- hex.Encode(buf[19:23], uuid[8:10])
- buf[23] = '-'
- hex.Encode(buf[24:], uuid[10:])
+ encodeHex(buf[:], uuid)
return string(buf[:])
}
@@ -103,19 +95,22 @@
}
var buf [36 + 9]byte
copy(buf[:], "urn:uuid:")
- _buf := buf[9:]
- hex.Encode(_buf[:], uuid[:4])
- _buf[8] = '-'
- hex.Encode(_buf[9:13], uuid[4:6])
- _buf[13] = '-'
- hex.Encode(_buf[14:18], uuid[6:8])
- _buf[18] = '-'
- hex.Encode(_buf[19:23], uuid[8:10])
- _buf[23] = '-'
- hex.Encode(_buf[24:], uuid[10:])
+ 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
// uuid is invalid.
func (uuid UUID) Variant() Variant {