From ea4631cc0cf301c824bd665a7980c13289ab5c9d Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Fri, 11 Nov 2022 12:36:31 -0500 Subject: internal/godebug: define more efficient API We have been expanding our use of GODEBUG for compatibility, and the current implementation forces a tradeoff between freshness and efficiency. It parses the environment variable in full each time it is called, which is expensive. But if clients cache the result, they won't respond to run-time GODEBUG changes, as happened with x509sha1 (#56436). This CL changes the GODEBUG API to provide efficient, up-to-date results. Instead of a single Get function, New returns a *godebug.Setting that itself has a Get method. Clients can save the result of New, which is no more expensive than errors.New, in a global variable, and then call that variable's Get method to get the value. Get costs only two atomic loads in the case where the variable hasn't changed since the last call. Unfortunately, these changes do require importing sync from godebug, which will mean that sync itself will never be able to use a GODEBUG setting. That doesn't seem like such a hardship. If it was really necessary, the runtime could pass a setting to package sync itself at startup, with the caveat that that setting, like the ones used by runtime itself, would not respond to run-time GODEBUG changes. Change-Id: I99a3acfa24fb2a692610af26a5d14bbc62c966ac Reviewed-on: https://go-review.googlesource.com/c/go/+/449504 Run-TryBot: Russ Cox Auto-Submit: Russ Cox Reviewed-by: Michael Knyszek Reviewed-by: Ian Lance Taylor TryBot-Result: Gopher Robot --- src/math/rand/rand.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'src/math') diff --git a/src/math/rand/rand.go b/src/math/rand/rand.go index f6b015aba2..0157d7198b 100644 --- a/src/math/rand/rand.go +++ b/src/math/rand/rand.go @@ -408,12 +408,14 @@ type lockedSource struct { //go:linkname fastrand64 func fastrand64() uint64 +var randautoseed = godebug.New("randautoseed") + // source returns r.s, allocating and seeding it if needed. // The caller must have locked r. func (r *lockedSource) source() *rngSource { if r.s == nil { var seed int64 - if godebug.Get("randautoseed") == "0" { + if randautoseed.Value() == "0" { seed = 1 } else { seed = int64(fastrand64()) -- cgit v1.3