1
0
mirror of https://github.com/google/uuid.git synced 2024-11-28 08:49:08 +02:00
uuid/marshal.go

39 lines
907 B
Go
Raw Normal View History

// Copyright 2016 Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package uuid
optimize internal parsing function -- avoid unsafe []byte parsing Making xtob take two byte arguments avoids a lot of slicing. This makes the Parse function faster. In addition, because so much slicing is avoiding, duplicating the parse logic to ParseBytes resulted in the function being faster than Parse (<1ns). The BenchmarkParseBytesNative function has been removed (parseBytes was identical to ParseBytes). And a new benchmark, BenchmarkParseBytesUnsafe, has been added to benchmark the old way of parsing []byte (which is slightly slower than Parse and thus the new ParseBytes implementation). benchmark old ns/op new ns/op delta BenchmarkUUID_MarshalJSON-4 685 667 -2.63% BenchmarkUUID_UnmarshalJSON-4 1145 1162 +1.48% BenchmarkParse-4 61.6 56.5 -8.28% BenchmarkParseBytes-4 65.7 55.9 -14.92% BenchmarkParseBytesCopy-4 121 115 -4.96% BenchmarkNew-4 1665 1643 -1.32% BenchmarkUUID_String-4 112 113 +0.89% BenchmarkUUID_URN-4 117 119 +1.71% benchmark old allocs new allocs delta BenchmarkUUID_MarshalJSON-4 4 4 +0.00% BenchmarkUUID_UnmarshalJSON-4 2 2 +0.00% BenchmarkParse-4 0 0 +0.00% BenchmarkParseBytes-4 0 0 +0.00% BenchmarkParseBytesCopy-4 1 1 +0.00% BenchmarkNew-4 1 1 +0.00% BenchmarkUUID_String-4 1 1 +0.00% BenchmarkUUID_URN-4 1 1 +0.00% benchmark old bytes new bytes delta BenchmarkUUID_MarshalJSON-4 248 248 +0.00% BenchmarkUUID_UnmarshalJSON-4 248 248 +0.00% BenchmarkParse-4 0 0 +0.00% BenchmarkParseBytes-4 0 0 +0.00% BenchmarkParseBytesCopy-4 48 48 +0.00% BenchmarkNew-4 16 16 +0.00% BenchmarkUUID_String-4 48 48 +0.00% BenchmarkUUID_URN-4 48 48 +0.00%
2016-02-26 07:36:20 +02:00
import "fmt"
// MarshalText implements encoding.TextMarshaler.
func (uuid UUID) MarshalText() ([]byte, error) {
var js [36]byte
encodeHex(js[:], uuid)
return js[:], nil
}
// UnmarshalText implements encoding.TextUnmarshaler.
func (uuid *UUID) UnmarshalText(data []byte) error {
optimize internal parsing function -- avoid unsafe []byte parsing Making xtob take two byte arguments avoids a lot of slicing. This makes the Parse function faster. In addition, because so much slicing is avoiding, duplicating the parse logic to ParseBytes resulted in the function being faster than Parse (<1ns). The BenchmarkParseBytesNative function has been removed (parseBytes was identical to ParseBytes). And a new benchmark, BenchmarkParseBytesUnsafe, has been added to benchmark the old way of parsing []byte (which is slightly slower than Parse and thus the new ParseBytes implementation). benchmark old ns/op new ns/op delta BenchmarkUUID_MarshalJSON-4 685 667 -2.63% BenchmarkUUID_UnmarshalJSON-4 1145 1162 +1.48% BenchmarkParse-4 61.6 56.5 -8.28% BenchmarkParseBytes-4 65.7 55.9 -14.92% BenchmarkParseBytesCopy-4 121 115 -4.96% BenchmarkNew-4 1665 1643 -1.32% BenchmarkUUID_String-4 112 113 +0.89% BenchmarkUUID_URN-4 117 119 +1.71% benchmark old allocs new allocs delta BenchmarkUUID_MarshalJSON-4 4 4 +0.00% BenchmarkUUID_UnmarshalJSON-4 2 2 +0.00% BenchmarkParse-4 0 0 +0.00% BenchmarkParseBytes-4 0 0 +0.00% BenchmarkParseBytesCopy-4 1 1 +0.00% BenchmarkNew-4 1 1 +0.00% BenchmarkUUID_String-4 1 1 +0.00% BenchmarkUUID_URN-4 1 1 +0.00% benchmark old bytes new bytes delta BenchmarkUUID_MarshalJSON-4 248 248 +0.00% BenchmarkUUID_UnmarshalJSON-4 248 248 +0.00% BenchmarkParse-4 0 0 +0.00% BenchmarkParseBytes-4 0 0 +0.00% BenchmarkParseBytesCopy-4 48 48 +0.00% BenchmarkNew-4 16 16 +0.00% BenchmarkUUID_String-4 48 48 +0.00% BenchmarkUUID_URN-4 48 48 +0.00%
2016-02-26 07:36:20 +02:00
id, err := ParseBytes(data)
if err != nil {
return err
}
*uuid = id
return nil
}
// MarshalBinary implements encoding.BinaryMarshaler.
func (uuid UUID) MarshalBinary() ([]byte, error) {
return uuid[:], nil
}
// UnmarshalBinary implements encoding.BinaryUnmarshaler.
func (uuid *UUID) UnmarshalBinary(data []byte) error {
if len(data) != 16 {
return fmt.Errorf("invalid UUID (got %d bytes)", len(data))
}
copy(uuid[:], data)
return nil
}