From 8b061a21026ee2ef07c95ade56a384b00201625b Mon Sep 17 00:00:00 2001 From: Gustavo Silva Paiva Date: Fri, 1 Nov 2019 16:06:33 -0300 Subject: [PATCH] Improve `Value.Emit()` with strconv. (#262) * use strconv instead of fmt.Sprint * rollback float it has no significant improvement on time and worse allocated bytes. * change int32 and uint32 to separate cases. --- api/core/key.go | 23 ++++++++++++++++++----- api/core/key_test.go | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 5 deletions(-) diff --git a/api/core/key.go b/api/core/key.go index 758e1ba66..5bf8721ed 100644 --- a/api/core/key.go +++ b/api/core/key.go @@ -18,6 +18,7 @@ package core import ( "fmt" + "strconv" "unsafe" ) @@ -303,12 +304,24 @@ func (v *Value) AsInterface() interface{} { // Emit returns a string representation of Value's data. func (v *Value) Emit() string { - if v.Type() == STRING { + switch v.Type() { + case BOOL: + return strconv.FormatBool(v.AsBool()) + case INT32: + return strconv.FormatInt(int64(v.AsInt32()), 10) + case INT64: + return strconv.FormatInt(v.AsInt64(), 10) + case UINT32: + return strconv.FormatUint(uint64(v.AsUint32()), 10) + case UINT64: + return strconv.FormatUint(v.AsUint64(), 10) + case FLOAT32: + return fmt.Sprint(v.AsFloat32()) + case FLOAT64: + return fmt.Sprint(v.AsFloat64()) + case STRING: return v.stringly - } - i := v.AsInterface() - if _, ok := i.(unknownValueType); ok { + default: return "unknown" } - return fmt.Sprint(i) } diff --git a/api/core/key_test.go b/api/core/key_test.go index 410db6f91..a4e299b4b 100644 --- a/api/core/key_test.go +++ b/api/core/key_test.go @@ -203,3 +203,43 @@ func TestEmit(t *testing.T) { }) } } + +func BenchmarkEmitBool(b *testing.B) { + b.ReportAllocs() + for i := 0; i < b.N; i++ { + n := core.Bool(i%2 == 0) + _ = n.Emit() + } +} + +func BenchmarkEmitInt64(b *testing.B) { + b.ReportAllocs() + for i := 0; i < b.N; i++ { + n := core.Int64(int64(i)) + _ = n.Emit() + } +} + +func BenchmarkEmitUInt64(b *testing.B) { + b.ReportAllocs() + for i := 0; i < b.N; i++ { + n := core.Uint64(uint64(i)) + _ = n.Emit() + } +} + +func BenchmarkEmitFloat64(b *testing.B) { + b.ReportAllocs() + for i := 0; i < b.N; i++ { + n := core.Float64(float64(i)) + _ = n.Emit() + } +} + +func BenchmarkEmitFloat32(b *testing.B) { + b.ReportAllocs() + for i := 0; i < b.N; i++ { + n := core.Float32(float32(i)) + _ = n.Emit() + } +}