You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
For example, (Decimal64).Format allocates a buffer to pass to (Decimal64).Append but ends up passing it to (fmt.State).Write, which might be simply copying the buffer into another already allocated buffer.
The solution might be to reverse the roles. Format implements the algorithm and Append calls it like so:
typeappenderstruct {
buf []byteprecint
}
func (a*appender) Write(b []byte) (nint, errerror) {
a.buf=append(a.buf, b...)
returnn, nil
}
func (a*appender) Width() (widint, okbool) {
return0, false
}
func (a*appender) Precision() (precint, okbool) {
returna.prec, true
}
func (a*appender) Flag(cint) bool {
returnfalse
}
// Append appends the text representation of d to buf.func (dDecimal64) Append(buf []byte, formatbyte, precint) []byte {
a:=appender{buf, prec}
d.Format(&a, rune(format))
returna.buf
}
// Format implements fmt.Formatter.func (dDecimal64) Format(s fmt.State, formatrune) {
// Declare big enough local array to avoid dynamic allocation.vardata [25]byte// Use the same algo as Append currently doesbuf:=data[:]
prec, havePrec:=s.Precision()
⋮
if_, err:=s.Write(buf); err!=nil {
panic(err)
}
}
To reiterate, the above is just one case. There might be others. Also, it's possible the above solution yields worse performance, so don't assume, profile.
The text was updated successfully, but these errors were encountered:
For example,
(Decimal64).Format
allocates a buffer to pass to(Decimal64).Append
but ends up passing it to(fmt.State).Write
, which might be simply copying the buffer into another already allocated buffer.The solution might be to reverse the roles.
Format
implements the algorithm andAppend
calls it like so:To reiterate, the above is just one case. There might be others. Also, it's possible the above solution yields worse performance, so don't assume, profile.
The text was updated successfully, but these errors were encountered: