Write ninja file directly to the output file

Writing the ninja file to a byte buffer causes a significant amount
of time to be spent in memmove when growing the byte slice.  Write
the file directly to disk instead.

This also fixes some unhandled error warnings, which become more
likely when doing disk IO instead of byte buffer writes.

Change-Id: I5094e4c45cab4012713037f60c5a4fb00718f92e
This commit is contained in:
Colin Cross 2019-01-23 13:23:00 -08:00
parent 3a8c025648
commit de7afaaf74
4 changed files with 49 additions and 17 deletions

View file

@ -15,9 +15,10 @@
package bootstrap package bootstrap
import ( import (
"bytes" "bufio"
"flag" "flag"
"fmt" "fmt"
"io"
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
@ -173,20 +174,39 @@ func Main(ctx *blueprint.Context, config interface{}, extraNinjaFileDeps ...stri
} }
deps = append(deps, extraDeps...) deps = append(deps, extraDeps...)
buf := bytes.NewBuffer(nil)
err = ctx.WriteBuildFile(buf)
if err != nil {
fatalf("error generating Ninja file contents: %s", err)
}
if stage == StageMain && emptyNinjaFile {
buf.Reset()
}
const outFilePermissions = 0666 const outFilePermissions = 0666
err = ioutil.WriteFile(outFile, buf.Bytes(), outFilePermissions) var out io.Writer
var f *os.File
var buf *bufio.Writer
if stage != StageMain || !emptyNinjaFile {
f, err = os.OpenFile(outFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, outFilePermissions)
if err != nil {
fatalf("error opening Ninja file: %s", err)
}
buf = bufio.NewWriter(f)
out = buf
} else {
out = ioutil.Discard
}
err = ctx.WriteBuildFile(out)
if err != nil { if err != nil {
fatalf("error writing %s: %s", outFile, err) fatalf("error writing Ninja file contents: %s", err)
}
if buf != nil {
err = buf.Flush()
if err != nil {
fatalf("error flushing Ninja file contents: %s", err)
}
}
if f != nil {
err = f.Close()
if err != nil {
fatalf("error closing Ninja file: %s", err)
}
} }
if globFile != "" { if globFile != "" {

View file

@ -3113,7 +3113,10 @@ func (c *Context) writeNinjaRequiredVersion(nw *ninjaWriter) error {
func (c *Context) writeSubninjas(nw *ninjaWriter) error { func (c *Context) writeSubninjas(nw *ninjaWriter) error {
for _, subninja := range c.subninjas { for _, subninja := range c.subninjas {
nw.Subninja(subninja) err := nw.Subninja(subninja)
if err != nil {
return err
}
} }
return nw.BlankLine() return nw.BlankLine()
} }

View file

@ -410,7 +410,10 @@ func (b *buildDef) WriteTo(nw *ninjaWriter, pkgNames map[*packageContext]string)
} }
if !b.Optional { if !b.Optional {
nw.Default(outputs...) err = nw.Default(outputs...)
if err != nil {
return err
}
} }
return nw.BlankLine() return nw.BlankLine()

View file

@ -117,7 +117,10 @@ func (n *ninjaWriter) Build(comment string, rule string, outputs, implicitOuts,
} }
if comment != "" { if comment != "" {
wrapper.Comment(comment) err := wrapper.Comment(comment)
if err != nil {
return err
}
} }
wrapper.WriteString("build") wrapper.WriteString("build")
@ -237,7 +240,10 @@ func (n *ninjaWriterWithWrap) writeString(s string, space bool) {
n.writtenLen = indentWidth * 2 n.writtenLen = indentWidth * 2
s = strings.TrimLeftFunc(s, unicode.IsSpace) s = strings.TrimLeftFunc(s, unicode.IsSpace)
} else if space { } else if space {
io.WriteString(n.writer, " ") _, n.err = io.WriteString(n.writer, " ")
if n.err != nil {
return
}
n.writtenLen++ n.writtenLen++
} }