Move RuleBuilder on top of strings.Builder

strings.Builder avoids copying the final byte array to a string by
using unsafe to point the string to the backing array of the byte
slice.

Test: rule_builder_test.go
Change-Id: I3d9a70fb62f8542650cd9ebc65397297bba12585
This commit is contained in:
Colin Cross
2019-07-08 17:07:18 -07:00
parent 188236ff23
commit cfec40c41b

View File

@@ -267,7 +267,7 @@ func (r *RuleBuilder) Tools() Paths {
func (r *RuleBuilder) Commands() []string { func (r *RuleBuilder) Commands() []string {
var commands []string var commands []string
for _, c := range r.commands { for _, c := range r.commands {
commands = append(commands, string(c.buf)) commands = append(commands, c.buf.String())
} }
return commands return commands
} }
@@ -358,7 +358,7 @@ func (r *RuleBuilder) Build(pctx PackageContext, ctx BuilderContext, name string
Flag("--output-root").Text(r.sboxOutDir.String()). Flag("--output-root").Text(r.sboxOutDir.String()).
Flags(sboxOutputs) Flags(sboxOutputs)
commandString = string(sboxCmd.buf) commandString = sboxCmd.buf.String()
tools = append(tools, sboxCmd.tools...) tools = append(tools, sboxCmd.tools...)
} }
@@ -388,7 +388,7 @@ func (r *RuleBuilder) Build(pctx PackageContext, ctx BuilderContext, name string
// RuleBuilderCommand, so they can be used chained or unchained. All methods that add text implicitly add a single // RuleBuilderCommand, so they can be used chained or unchained. All methods that add text implicitly add a single
// space as a separator from the previous method. // space as a separator from the previous method.
type RuleBuilderCommand struct { type RuleBuilderCommand struct {
buf []byte buf strings.Builder
inputs Paths inputs Paths
outputs WritablePaths outputs WritablePaths
depFiles WritablePaths depFiles WritablePaths
@@ -420,10 +420,10 @@ func (c *RuleBuilderCommand) outputStr(path Path) string {
// Text adds the specified raw text to the command line. The text should not contain input or output paths or the // Text adds the specified raw text to the command line. The text should not contain input or output paths or the
// rule will not have them listed in its dependencies or outputs. // rule will not have them listed in its dependencies or outputs.
func (c *RuleBuilderCommand) Text(text string) *RuleBuilderCommand { func (c *RuleBuilderCommand) Text(text string) *RuleBuilderCommand {
if len(c.buf) > 0 { if c.buf.Len() > 0 {
c.buf = append(c.buf, ' ') c.buf.WriteByte(' ')
} }
c.buf = append(c.buf, text...) c.buf.WriteString(text)
return c return c
} }
@@ -608,7 +608,7 @@ func (c *RuleBuilderCommand) FlagWithDepFile(flag string, path WritablePath) *Ru
// String returns the command line. // String returns the command line.
func (c *RuleBuilderCommand) String() string { func (c *RuleBuilderCommand) String() string {
return string(c.buf) return c.buf.String()
} }
func ninjaNameEscape(s string) string { func ninjaNameEscape(s string) string {