Skip to content
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

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

Open
pierrre opened this issue Jan 28, 2025 · 6 comments
Open

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

pierrre opened this issue Jan 28, 2025 · 6 comments
Labels
Implementation Issues describing a semantics-preserving change to the Go implementation. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@pierrre
Copy link

pierrre commented Jan 28, 2025

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.

@gabyhelp
Copy link

Related Issues

Related Code Changes

(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)

@gabyhelp gabyhelp added the Implementation Issues describing a semantics-preserving change to the Go implementation. label Jan 28, 2025
@cagedmantis cagedmantis added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Jan 28, 2025
@cagedmantis cagedmantis added this to the Backlog milestone Jan 28, 2025
@cagedmantis
Copy link
Contributor

@gopherbot
Copy link
Contributor

Change https://go.dev/cl/645215 mentions this issue: math/big: optimize writeMultiple to use StringWriter and ByteWriter

@Jorropo
Copy link
Member

Jorropo commented Jan 29, 2025

I've submitted a patch and seeing where it is used I've overhauled it:

  • return without trying anything if called with a count of 0.
  • if writing one byte (which happens often when padding with 0 or ) try ByteWriter
  • try StringWriter
  • fallback to Writer

Unlike what is suggested here I did not used io.WriteString as it would make it much worst if for some reason someone passed in a fmt.State that does not implement io.WriteString because io.WriteString would allocate and copy []byte for each iteration of count.

@pierrre
Copy link
Author

pierrre commented Jan 29, 2025

Unlike what is suggested here I did not used io.WriteString as it would make it much worst if for some reason someone passed in a fmt.State that does not implement io.WriteString because io.WriteString would allocate and copy []byte for each iteration of count.

I'm just wondering: are there several implementations of fmt.State ?

@Jorropo
Copy link
Member

Jorropo commented Jan 29, 2025

Idk
This is part of the public API https://pkg.go.dev/math/big#Int.Format anyone is free to plugin their own implementation.
Doing the correct thing is easier than reviewing the open source corpus so I did not tried.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Implementation Issues describing a semantics-preserving change to the Go implementation. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

5 participants