From ed746bd84cf0e35c4efaa864a2675b977ee59e5a Mon Sep 17 00:00:00 2001 From: "duanyi.aster" Date: Mon, 12 Feb 2024 13:53:51 +0800 Subject: [PATCH] opt: use native or std func --- internal/encoder/alg/spec.go | 50 ++++++++++---------- internal/native/neon/fastfloat_arm64_test.go | 13 +++-- 2 files changed, 32 insertions(+), 31 deletions(-) diff --git a/internal/encoder/alg/spec.go b/internal/encoder/alg/spec.go index 3806b4567..7f0e81002 100644 --- a/internal/encoder/alg/spec.go +++ b/internal/encoder/alg/spec.go @@ -151,25 +151,31 @@ func HtmlEscape(dst []byte, src []byte) []byte { return dst } -// func F64toa(buf []byte, v float64) ([]byte) { -// buf = rt.GuardSlice2(buf, 64) -// ret := native.F64toa((*byte)(rt.IndexByte(buf, len(buf))), v) -// if ret > 0 { -// return buf[:len(buf)+ret] -// } else { -// return buf -// } -// } +func F64toa(buf []byte, v float64) ([]byte) { + if v == 0 { + return append(buf, '0') + } + buf = rt.GuardSlice2(buf, 64) + ret := native.F64toa((*byte)(rt.IndexByte(buf, len(buf))), v) + if ret > 0 { + return buf[:len(buf)+ret] + } else { + return buf + } +} -// func F32toa(buf []byte, v float32) ([]byte) { -// buf = rt.GuardSlice2(buf, 64) -// ret := native.F32toa((*byte)(rt.IndexByte(buf, len(buf))), v) -// if ret > 0 { -// return buf[:len(buf)+ret] -// } else { -// return buf -// } -// } +func F32toa(buf []byte, v float32) ([]byte) { + if v == 0 { + return append(buf, '0') + } + buf = rt.GuardSlice2(buf, 64) + ret := native.F32toa((*byte)(rt.IndexByte(buf, len(buf))), v) + if ret > 0 { + return buf[:len(buf)+ret] + } else { + return buf + } +} // func I64toa(buf []byte, v int64) ([]byte) { // if -10 < v && v < 10 { @@ -199,14 +205,6 @@ func HtmlEscape(dst []byte, src []byte) []byte { // } // } -func F64toa(buf []byte, v float64) ([]byte) { - return strconv.AppendFloat(buf, float64(v), 'g', -1, 64) -} - -func F32toa(buf []byte, v float32) ([]byte) { - return strconv.AppendFloat(buf, float64(v), 'g', -1, 32) -} - func I64toa(buf []byte, v int64) ([]byte) { return strconv.AppendInt(buf, int64(v), 10) } diff --git a/internal/native/neon/fastfloat_arm64_test.go b/internal/native/neon/fastfloat_arm64_test.go index 7ff241065..dc00ec967 100644 --- a/internal/native/neon/fastfloat_arm64_test.go +++ b/internal/native/neon/fastfloat_arm64_test.go @@ -120,11 +120,14 @@ func BenchmarkParseFloat32(b *testing.B) { float float32 }{ {"Zero", 0}, - {"Integer", 33909}, - {"ExactFraction", 3.375}, - {"Point", 339.7784}, - {"Exp", -5.09e25}, - {"NegExp", -5.11e-25}, + {"Decimal1", 39}, + {"Decimal2", 33909}, + {"Float1", 3.77}, + {"Float2", 339.778442}, + {"Exp1", 5.9e5}, + {"Exp2", 521.091e19}, + {"NegExp1", -5.1e-5}, + {"NegExp2", -52.132e-19}, {"Shortest", 1.234567e-8}, } for _, c := range f32toaBenches {