You've already forked json-iterator
mirror of
https://github.com/json-iterator/go.git
synced 2025-07-15 23:54:21 +02:00
Remove allocs from WriteFloat32/WriteFloat64
The use of strconv.FormatFloat causes a string allocation, by setting aside a reusable buffer and using strconv.AppendFloat this can be avoided. Before: BenchmarkRespond-4 300 5392189 ns/op 618936 B/op 20010 allocs/op After: BenchmarkRespond-4 300 4713746 ns/op 139744 B/op 10 allocs/op This benchmark is using a custom encoder that calls WriteFloat64 20k times, which is the bulk of the work.
This commit is contained in:
@ -14,6 +14,7 @@ type Stream struct {
|
||||
Error error
|
||||
indention int
|
||||
Attachment interface{} // open for customized encoder
|
||||
floatBuf []byte
|
||||
}
|
||||
|
||||
// NewStream create new stream instance.
|
||||
@ -28,6 +29,7 @@ func NewStream(cfg API, out io.Writer, bufSize int) *Stream {
|
||||
n: 0,
|
||||
Error: nil,
|
||||
indention: 0,
|
||||
floatBuf: make([]byte, 0, 32),
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user