1
0
mirror of https://github.com/google/uuid.git synced 2025-09-16 09:16:30 +02:00

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).
This commit is contained in:
Bryan Matsuo
2015-10-10 20:56:32 -07:00
parent 0ec82b41c6
commit 1c7b0fea16
2 changed files with 22 additions and 23 deletions

12
json.go
View File

@@ -7,17 +7,21 @@ package uuid
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]

33
uuid.go
View File

@@ -83,15 +83,7 @@ func (uuid UUID) String() string {
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 @@ func (uuid UUID) URN() string {
}
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 {