Skip to content

Commit 4d79ae3

Browse files
committed
parser: optimize memory allocation for command word parsing
This could be optimized further to reuse buffers between commands but that would require chaning signature for parser dispatch function. Signed-off-by: Tonis Tiigi <[email protected]>
1 parent 4c935cf commit 4d79ae3

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

frontend/dockerfile/parser/line_parsers.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,11 @@ func parseWords(rest string, d *directives) []string {
5959

6060
words := []string{}
6161
phase := inSpaces
62-
word := ""
6362
quote := '\000'
6463
blankOK := false
6564
var ch rune
6665
var chWidth int
66+
var sbuilder strings.Builder
6767

6868
for pos := 0; pos <= len(rest); pos += chWidth {
6969
if pos != len(rest) {
@@ -80,18 +80,18 @@ func parseWords(rest string, d *directives) []string {
8080
phase = inWord // found it, fall through
8181
}
8282
if (phase == inWord || phase == inQuote) && (pos == len(rest)) {
83-
if blankOK || len(word) > 0 {
84-
words = append(words, word)
83+
if blankOK || sbuilder.Len() > 0 {
84+
words = append(words, sbuilder.String())
8585
}
8686
break
8787
}
8888
if phase == inWord {
8989
if unicode.IsSpace(ch) {
9090
phase = inSpaces
91-
if blankOK || len(word) > 0 {
92-
words = append(words, word)
91+
if blankOK || sbuilder.Len() > 0 {
92+
words = append(words, sbuilder.String())
9393
}
94-
word = ""
94+
sbuilder.Reset()
9595
blankOK = false
9696
continue
9797
}
@@ -107,11 +107,11 @@ func parseWords(rest string, d *directives) []string {
107107
// If we're not quoted and we see an escape token, then always just
108108
// add the escape token plus the char to the word, even if the char
109109
// is a quote.
110-
word += string(ch)
110+
sbuilder.WriteRune(ch)
111111
pos += chWidth
112112
ch, chWidth = utf8.DecodeRuneInString(rest[pos:])
113113
}
114-
word += string(ch)
114+
sbuilder.WriteRune(ch)
115115
continue
116116
}
117117
if phase == inQuote {
@@ -125,10 +125,10 @@ func parseWords(rest string, d *directives) []string {
125125
continue // just skip the escape token at end
126126
}
127127
pos += chWidth
128-
word += string(ch)
128+
sbuilder.WriteRune(ch)
129129
ch, chWidth = utf8.DecodeRuneInString(rest[pos:])
130130
}
131-
word += string(ch)
131+
sbuilder.WriteRune(ch)
132132
}
133133
}
134134

0 commit comments

Comments
 (0)