Skip to content

Remove allocs from WriteFloat32/WriteFloat64 #234

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions feature_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type Stream struct {
Error error
indention int
Attachment interface{} // open for customized encoder
floatBuf []byte
}

// NewStream create new stream instance.
Expand All @@ -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),
}
}

Expand Down
8 changes: 6 additions & 2 deletions feature_stream_float.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ func (stream *Stream) WriteFloat32(val float32) {
fmt = 'e'
}
}
stream.WriteRaw(strconv.FormatFloat(float64(val), fmt, -1, 32))
stream.floatBuf = strconv.AppendFloat(stream.floatBuf, float64(val), fmt, -1, 32)
stream.Write(stream.floatBuf)
stream.floatBuf = stream.floatBuf[:0]
}

// WriteFloat32Lossy write float32 to stream with ONLY 6 digits precision although much much faster
Expand Down Expand Up @@ -63,7 +65,9 @@ func (stream *Stream) WriteFloat64(val float64) {
fmt = 'e'
}
}
stream.WriteRaw(strconv.FormatFloat(float64(val), fmt, -1, 64))
stream.floatBuf = strconv.AppendFloat(stream.floatBuf, float64(val), fmt, -1, 64)
stream.Write(stream.floatBuf)
stream.floatBuf = stream.floatBuf[:0]
}

// WriteFloat64Lossy write float64 to stream with ONLY 6 digits precision although much much faster
Expand Down