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:
parent
15bfc5bb12
commit
8b061a2102
|
|
@ -18,6 +18,7 @@ package core
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strconv"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -303,12 +304,24 @@ func (v *Value) AsInterface() interface{} {
|
||||||
|
|
||||||
// Emit returns a string representation of Value's data.
|
// Emit returns a string representation of Value's data.
|
||||||
func (v *Value) Emit() string {
|
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
|
return v.stringly
|
||||||
}
|
default:
|
||||||
i := v.AsInterface()
|
|
||||||
if _, ok := i.(unknownValueType); ok {
|
|
||||||
return "unknown"
|
return "unknown"
|
||||||
}
|
}
|
||||||
return fmt.Sprint(i)
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue