Skip to content

Commit

Permalink
cmd/internal/objabi: use sync.OnceValue
Browse files Browse the repository at this point in the history
Change-Id: I09c134fff728d32c5bc475889b8c673cc18120a0
Reviewed-on: https://go-review.googlesource.com/c/go/+/611041
Auto-Submit: Ian Lance Taylor <iant@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
  • Loading branch information
kolyshkin authored and gopherbot committed Sep 6, 2024
1 parent ca2cb8d commit a1c3e24
Showing 1 changed file with 30 additions and 33 deletions.
63 changes: 30 additions & 33 deletions src/cmd/internal/objabi/pkgspecial.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,39 +91,36 @@ var allowAsmABIPkgs = []string{
"runtime/internal/startlinetest",
}

var (
pkgSpecials map[string]PkgSpecial
pkgSpecialsOnce sync.Once
)

// LookupPkgSpecial returns special build properties for the given package path.
func LookupPkgSpecial(pkgPath string) PkgSpecial {
pkgSpecialsOnce.Do(func() {
// Construct pkgSpecials from various package lists. This lets us use
// more flexible logic, while keeping the final map simple, and avoids
// the init-time cost of a map.
pkgSpecials = make(map[string]PkgSpecial)
set := func(elt string, f func(*PkgSpecial)) {
s := pkgSpecials[elt]
f(&s)
pkgSpecials[elt] = s
}
for _, pkg := range runtimePkgs {
set(pkg, func(ps *PkgSpecial) { ps.Runtime = true; ps.NoInstrument = true })
}
for _, pkg := range extraNoInstrumentPkgs {
if pkg[0] == '-' {
set(pkg[1:], func(ps *PkgSpecial) { ps.NoInstrument = false })
} else {
set(pkg, func(ps *PkgSpecial) { ps.NoInstrument = true })
}
}
for _, pkg := range noRaceFuncPkgs {
set(pkg, func(ps *PkgSpecial) { ps.NoRaceFunc = true })
}
for _, pkg := range allowAsmABIPkgs {
set(pkg, func(ps *PkgSpecial) { ps.AllowAsmABI = true })
}
})
return pkgSpecials[pkgPath]
return pkgSpecialsOnce()[pkgPath]
}

var pkgSpecialsOnce = sync.OnceValue(func() map[string]PkgSpecial {
// Construct pkgSpecials from various package lists. This lets us use
// more flexible logic, while keeping the final map simple, and avoids
// the init-time cost of a map.
pkgSpecials := make(map[string]PkgSpecial)
set := func(elt string, f func(*PkgSpecial)) {
s := pkgSpecials[elt]
f(&s)
pkgSpecials[elt] = s
}
for _, pkg := range runtimePkgs {
set(pkg, func(ps *PkgSpecial) { ps.Runtime = true; ps.NoInstrument = true })
}
for _, pkg := range extraNoInstrumentPkgs {
if pkg[0] == '-' {
set(pkg[1:], func(ps *PkgSpecial) { ps.NoInstrument = false })
} else {
set(pkg, func(ps *PkgSpecial) { ps.NoInstrument = true })
}
}
for _, pkg := range noRaceFuncPkgs {
set(pkg, func(ps *PkgSpecial) { ps.NoRaceFunc = true })
}
for _, pkg := range allowAsmABIPkgs {
set(pkg, func(ps *PkgSpecial) { ps.AllowAsmABI = true })
}
return pkgSpecials
})

0 comments on commit a1c3e24

Please sign in to comment.