Skip to content

x/crypto/x509roots/fallback: high, unskippable, init cost #73691

Open
@twpayne

Description

@twpayne

Go version

go version go1.24.3 linux/amd64

Output of go env in your module/workspace:

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

What did you do?

I imported golang.org/x/crypto/x509roots/fallback and observed that this slowed down the startup of my CLI application by about 8ms on a fast machine:

package main

import _ "golang.org/x/crypto/x509roots/fallback"

func main() {}
$ go build -o tmp main.go

$ GODEBUG=inittrace=1 ./tmp |& awk '{print $5, $6, $2}' | sort -n | tail -n 3
0.043 ms internal/godebug
0.052 ms runtime
8.8 ms golang.org/x/crypto/x509roots/fallback

What did you see happen?

A 8.8ms delay due to the golang.org/x/crypto/x509roots/fallback package parsing a large number of certificates in its init() function. This is on a fast CPU (AMD Ryzen 8700G). This cost is paid whether or not the fallback X.509 certificates are used or not.

What did you expect to see?

Zero or near-zero startup cost for using golang.org/x/crypto/x509roots/fallback.

From looking at the code, it seems that the CPU is cost is due to parsing about 150 certificates in .PEM format in the package's un-skippable init() function.

The obvious, easy, backwards-compatible fix would be to parse the .PEM files while executing the template that generates bundle.go, instead of parsing them every time at startup.

I would be happy to submit a CL to implement this. Is it likely that such a CL would be accepted?

Metadata

Metadata

Assignees

Labels

NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.PerformanceUnfortunate

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions