1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-04-11 11:21:59 +02:00

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.
This commit is contained in:
Gustavo Silva Paiva 2019-11-01 16:06:33 -03:00 committed by rghetia
parent 15bfc5bb12
commit 8b061a2102
2 changed files with 58 additions and 5 deletions

View File

@ -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)
}

View File

@ -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()
}
}