Skip to content

Commit fb5814d

Browse files
committed
parser: avoid reallocating memory per rune on parsing flags
Signed-off-by: Tonis Tiigi <[email protected]>
1 parent 9aa07e6 commit fb5814d

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

frontend/dockerfile/parser/split_command.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func extractBuilderFlags(line string) (string, []string, error) {
3636

3737
words := []string{}
3838
phase := inSpaces
39-
word := ""
39+
sbuilder := &strings.Builder{}
4040
quote := '\000'
4141
blankOK := false
4242
var ch rune
@@ -62,21 +62,22 @@ func extractBuilderFlags(line string) (string, []string, error) {
6262
phase = inWord // found something with "--", fall through
6363
}
6464
if (phase == inWord || phase == inQuote) && (pos == len(line)) {
65-
if word != "--" && (blankOK || len(word) > 0) {
65+
if word := sbuilder.String(); word != "--" && (blankOK || len(word) > 0) {
6666
words = append(words, word)
6767
}
6868
break
6969
}
7070
if phase == inWord {
7171
if unicode.IsSpace(ch) {
72+
word := sbuilder.String()
7273
phase = inSpaces
7374
if word == "--" {
7475
return line[pos:], words, nil
7576
}
7677
if blankOK || len(word) > 0 {
7778
words = append(words, word)
7879
}
79-
word = ""
80+
sbuilder.Reset()
8081
blankOK = false
8182
continue
8283
}
@@ -93,7 +94,9 @@ func extractBuilderFlags(line string) (string, []string, error) {
9394
pos++
9495
ch = rune(line[pos])
9596
}
96-
word += string(ch)
97+
if _, err := sbuilder.WriteRune(ch); err != nil {
98+
return "", nil, err
99+
}
97100
continue
98101
}
99102
if phase == inQuote {
@@ -109,7 +112,9 @@ func extractBuilderFlags(line string) (string, []string, error) {
109112
pos++
110113
ch = rune(line[pos])
111114
}
112-
word += string(ch)
115+
if _, err := sbuilder.WriteRune(ch); err != nil {
116+
return "", nil, err
117+
}
113118
}
114119
}
115120

0 commit comments

Comments
 (0)