cmd/cgo,cmd/go: preallocate slices if they have known fixed capacities

This allows for more efficient use of memory.
This commit is contained in:
apocelipes 2024-10-11 09:34:27 +08:00
parent d20a4c2403
commit 19bb96a7cf
18 changed files with 27 additions and 24 deletions

View File

@ -181,7 +181,7 @@ func (f *File) ParseGo(abspath string, src []byte) {
// Like ast.CommentGroup's Text method but preserves
// leading blank lines, so that line numbers line up.
func commentText(g *ast.CommentGroup) string {
var pieces []string
pieces := make([]string, 0, len(g.List))
for _, com := range g.List {
c := com.Text
// Remove comment markers.

View File

@ -83,7 +83,7 @@ func (f *File) offset(p token.Pos) int {
}
func nameKeys(m map[string]*Name) []string {
var ks []string
ks := make([]string, 0, len(m))
for k := range m {
ks = append(ks, k)
}

View File

@ -78,7 +78,7 @@ func relConservative(basepath, targpath string) (string, error) {
// RelPaths returns a copy of paths with absolute paths
// made relative to the current directory if they would be shorter.
func RelPaths(paths []string) []string {
var out []string
out := make([]string, 0, len(paths))
for _, p := range paths {
rel, err := relConservative(Cwd(), p)
if err == nil && len(rel) < len(p) {

View File

@ -340,7 +340,7 @@ func runEnv(ctx context.Context, cmd *base.Command, args []string) {
// Show only the named vars.
if !*envChanged {
if *envJson {
var es []cfg.EnvVar
es := make([]cfg.EnvVar, 0, len(args))
for _, name := range args {
e := cfg.EnvVar{Name: name, Value: findEnv(env, name)}
es = append(es, e)

View File

@ -761,7 +761,7 @@ func glob(dir, pattern string, matches []string) (m []string, e error) {
return // ignore I/O error
}
var names []string
names := make([]string, 0, len(list))
for _, info := range list {
names = append(names, info.Name())
}

View File

@ -98,7 +98,7 @@ Files:
var ErrNoGo = fmt.Errorf("no Go source files")
func keys(m map[string]bool) []string {
var list []string
list := make([]string, 0, len(m))
for k := range m {
list = append(list, k)
}

View File

@ -389,7 +389,7 @@ func (v *jsonFlag) Set(s string) error {
}
func (v *jsonFlag) String() string {
var fields []string
fields := make([]string, 0, len(*v))
for f := range *v {
fields = append(fields, f)
}

View File

@ -94,7 +94,7 @@ func defaultGODEBUG(p *Package, directives, testDirectives, xtestDirectives []bu
m = defaults
}
var keys []string
keys := make([]string, 0, len(m))
for k := range m {
keys = append(keys, k)
}

View File

@ -319,7 +319,7 @@ func (r *gitRepo) Tags(ctx context.Context, prefix string) (*Tags, error) {
// the absence of a specific module version.
// The caller must supply refs, the result of a successful r.loadRefs.
func (r *gitRepo) repoSum(refs map[string]string) string {
var list []string
list := make([]string, 0, len(refs))
for ref := range refs {
list = append(list, ref)
}

View File

@ -915,7 +915,7 @@ func tidyGoSum(data []byte, keep map[module.Version]bool) []byte {
}
}
var mods []module.Version
mods := make([]module.Version, 0, len(goSum.m))
for m := range goSum.m {
mods = append(mods, m)
}

View File

@ -763,8 +763,9 @@ func (r *resolver) performLocalQueries(ctx context.Context) {
pkgPattern, mainModule := modload.MainModules.DirImportPath(ctx, q.pattern)
if pkgPattern == "." {
modload.MustHaveModRoot()
var modRoots []string
for _, m := range modload.MainModules.Versions() {
versions := modload.MainModules.Versions()
modRoots := make([]string, 0, len(versions))
for _, m := range versions {
modRoots = append(modRoots, modload.MainModules.ModRoot(m))
}
var plural string

View File

@ -993,10 +993,11 @@ func loadModFile(ctx context.Context, opts *PackageOpts) (*Requirements, error)
if cfg.BuildMod == "vendor" {
readVendorList(VendorDir())
var indexes []*modFileIndex
var modFiles []*modfile.File
var modRoots []string
for _, m := range MainModules.Versions() {
versions := MainModules.Versions()
indexes := make([]*modFileIndex, 0, len(versions))
modFiles := make([]*modfile.File, 0, len(versions))
modRoots := make([]string, 0, len(versions))
for _, m := range versions {
indexes = append(indexes, MainModules.Index(m))
modFiles = append(modFiles, MainModules.ModFile(m))
modRoots = append(modRoots, MainModules.ModRoot(m))

View File

@ -128,7 +128,7 @@ func runRun(ctx context.Context, cmd *base.Command, args []string) {
base.Fatalf("go: no packages loaded from %s", arg)
}
if len(pkgs) > 1 {
var names []string
names := make([]string, 0, len(pkgs))
for _, p := range pkgs {
names = append(names, p.ImportPath)
}

View File

@ -361,8 +361,9 @@ func ImportPaths(patterns, modRoots []string) []*Match {
// ImportPathsQuiet is like ImportPaths but does not warn about patterns with no matches.
func ImportPathsQuiet(patterns, modRoots []string) []*Match {
var out []*Match
for _, a := range CleanPatterns(patterns) {
patterns = CleanPatterns(patterns)
out := make([]*Match, 0, len(patterns))
for _, a := range patterns {
m := NewMatch(a)
if m.IsLocal() {
m.MatchDirs(modRoots)
@ -399,7 +400,7 @@ func CleanPatterns(patterns []string) []string {
if len(patterns) == 0 {
return []string{"."}
}
var out []string
out := make([]string, 0, len(patterns))
for _, a := range patterns {
var p, v string
if build.IsLocalImport(a) || filepath.IsAbs(a) {

View File

@ -101,7 +101,7 @@ func NewServer() (srv *Server, err error) {
vcs.VCSTestRepoURL = srv.HTTP.URL
vcs.VCSTestHosts = Hosts
var interceptors []web.Interceptor
interceptors := make([]web.Interceptor, 0, 2*len(Hosts))
for _, host := range Hosts {
interceptors = append(interceptors,
web.Interceptor{Scheme: "http", FromHost: host, ToHost: httpURL.Host, Client: srv.HTTP.Client()},

View File

@ -208,7 +208,7 @@ func actionGraphJSON(a *Action) string {
}
}
var list []*actionJSON
list := make([]*actionJSON, 0, len(workq))
for id, a := range workq {
if a.json == nil {
a.json = &actionJSON{

View File

@ -469,7 +469,7 @@ func toolVerify(a *Action, b *Builder, p *load.Package, newTool string, ofile st
}
func (gcToolchain) pack(b *Builder, a *Action, afile string, ofiles []string) error {
var absOfiles []string
absOfiles := make([]string, 0, len(ofiles))
for _, f := range ofiles {
absOfiles = append(absOfiles, mkAbs(a.Objdir, f))
}

View File

@ -230,7 +230,7 @@ func (tools gccgoToolchain) pack(b *Builder, a *Action, afile string, ofiles []s
p := a.Package
sh := b.Shell(a)
objdir := a.Objdir
var absOfiles []string
absOfiles := make([]string, 0, len(ofiles))
for _, f := range ofiles {
absOfiles = append(absOfiles, mkAbs(objdir, f))
}