Skip to content

math/big: reduce allocations in writeMultiple() #71465

@pierrre

Description

@pierrre

Go version

go version go1.23.5 linux/amd64

Output of go env in your module/workspace:

GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/home/pierre/.cache/go-build'
GOENV='/home/pierre/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/home/pierre/go/pkg/mod'
GONOPROXY='xxx'
GONOSUMDB='xxx'
GOOS='linux'
GOPATH='/home/pierre/go'
GOPRIVATE='xxx'
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/lib/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/lib/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.23.5'
GODEBUG=''
GOTELEMETRY='on'
GOTELEMETRYDIR='/home/pierre/.config/go/telemetry'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD='/dev/null'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build458815232=/tmp/go-build -gno-record-gcc-switches'

What did you do?

I'm trying to write a big.Int to a io.Writer.
I'm using fmt.FPrintf, and it calls big.Int.Format().

What did you see happen?

I did a benchmark, and noticed that many allocations are done in writeMultiple():

func writeMultiple(s fmt.State, text string, count int) {
	if len(text) > 0 {
		b := []byte(text)
		for ; count > 0; count-- {
			s.Write(b)
		}
	}
}

=> The string to []byte conversion.

What did you expect to see?

I think we can replace

s.Write(b)

with

io.WriteString(w, text)

and remove

b := []byte(text)

because fmt.State implementations implements WriteString.

Metadata

Metadata

Assignees

No one assigned

    Labels

    ImplementationIssues describing a semantics-preserving change to the Go implementation.NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.Performance

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions