Improve performance of ParseUfloat (#1865)

* Improve performance of ParseUfloat function

Replaced `offset` handling logic with more efficient math.Pow10 based calculation.

goos: linux
goarch: amd64
pkg: github.com/valyala/fasthttp
cpu: Intel(R) Core(TM) i7-4790 CPU @ 3.60GHz
              │   old.txt   │           new.txt           │
              │   sec/op    │   sec/op     vs base        │
ParseUfloat-8   44.22n ± 0%   31.06n ± 0%  -29.76% (n=50)

* fix: lint error return value is not checked

* Handling uint64 overflow issues

* Implement ParseUfloat by calling strconv.ParseFloat

* fix: lint error
This commit is contained in:
Kashiwa
2024-09-22 21:46:29 +08:00
committed by GitHub
parent 7c9c003d1d
commit e2bb2e0d64
3 changed files with 43 additions and 52 deletions
+24
View File
@@ -163,3 +163,27 @@ func BenchmarkAppendUnquotedArgSlowPath(b *testing.B) {
}
})
}
func BenchmarkParseUfloat(b *testing.B) {
src := [][]byte{
[]byte("0"),
[]byte("1234566789."),
[]byte(".1234556778"),
[]byte("123.456"),
[]byte("123456789"),
[]byte("1234e23"),
[]byte("1234E-51"),
[]byte("1.234e+32"),
[]byte("123456789123456789.987654321"),
}
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
for i := range src {
_, err := ParseUfloat(src[i])
if err != nil {
b.Fatalf("unexpected error: %v", err)
}
}
}
})
}