Optimize arg parsing in buildDef.WriteTo

Arguments to build definitions were copied from the input map to an
map with the name and value expanded, then to a list of names for
sorting, and then written, which required iterating over a map three
times.  Expand the name and value into a list of name value pairs,
and then do the rest of the operations on the list instead.

Test: ninja_writer_test.go
Change-Id: Id8ff644dafbaa3b4812747c60dc28cce22e21dbe
This commit is contained in:
Colin Cross 2021-01-21 15:29:16 -08:00
parent c8b9e55289
commit 00890dd8f6

View file

@ -410,25 +410,25 @@ func (b *buildDef) WriteTo(nw *ninjaWriter, pkgNames map[*packageContext]string)
return err
}
args := make(map[string]string)
for argVar, value := range b.Args {
args[argVar.fullName(pkgNames)] = value.Value(pkgNames)
}
err = writeVariables(nw, b.Variables, pkgNames)
if err != nil {
return err
}
var keys []string
for k := range args {
keys = append(keys, k)
type nameValuePair struct {
name, value string
}
sort.Strings(keys)
for _, name := range keys {
err = nw.ScopedAssign(name, args[name])
args := make([]nameValuePair, 0, len(b.Args))
for argVar, value := range b.Args {
fullName := argVar.fullName(pkgNames)
args = append(args, nameValuePair{fullName, value.Value(pkgNames)})
}
sort.Slice(args, func(i, j int) bool { return args[i].name < args[j].name })
for _, pair := range args {
err = nw.ScopedAssign(pair.name, pair.value)
if err != nil {
return err
}