From 946784907ad133328578b1ac0f0779bd6d247f51 Mon Sep 17 00:00:00 2001 From: Florin Patan Date: Fri, 1 May 2015 16:56:54 +0200 Subject: [PATCH 1/2] Speed-up for string conversion function --- benchmarks_test.go | 7 +++++++ uuid.go | 18 ++++++++++++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/benchmarks_test.go b/benchmarks_test.go index 6a8bf7e..9a85f7c 100644 --- a/benchmarks_test.go +++ b/benchmarks_test.go @@ -112,3 +112,10 @@ func BenchmarkUnmarshalText(b *testing.B) { u.UnmarshalText(bytes) } } + +func BenchmarkMarshalToString(b *testing.B) { + u := NewV4() + for i := 0; i < b.N; i++ { + u.String() + } +} diff --git a/uuid.go b/uuid.go index a956a41..e846485 100644 --- a/uuid.go +++ b/uuid.go @@ -58,6 +58,9 @@ const ( // UUID epoch (October 15, 1582) and Unix epoch (January 1, 1970). const epochStart = 122192928000000000 +// Used in string method conversion +const dash byte = '-' + // UUID v1/v2 storage. var ( storageMutex sync.Mutex @@ -174,8 +177,19 @@ func (u UUID) Bytes() []byte { // Returns canonical string representation of UUID: // xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx. func (u UUID) String() string { - return fmt.Sprintf("%x-%x-%x-%x-%x", - u[:4], u[4:6], u[6:8], u[8:10], u[10:]) + buf := make([]byte, 36) + + hex.Encode(buf[0:8], u[0:4]) + buf[8] = dash + hex.Encode(buf[9:13], u[4:6]) + buf[13] = dash + hex.Encode(buf[14:18], u[6:8]) + buf[18] = dash + hex.Encode(buf[19:23], u[8:10]) + buf[23] = dash + hex.Encode(buf[24:], u[10:]) + + return string(buf) } // SetVersion sets version bits. From 6fa6b8092ee03271baca48d66a626ea0fef55de4 Mon Sep 17 00:00:00 2001 From: Florin Patan Date: Fri, 1 May 2015 17:07:22 +0200 Subject: [PATCH 2/2] Use docker Travis to speed-up tests --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 3f1de31..0bbdc41 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,5 +5,6 @@ go: - 1.2 - 1.3 - 1.4 +sudo: false notifications: email: false