Skip to content

Commit

Permalink
syntax: avoid using bytes.Buffer for heredoc words
Browse files Browse the repository at this point in the history
We don't need an io.Writer; append is enough.

	name      old time/op    new time/op    delta
	Parse-16    38.8µs ± 0%    38.6µs ± 1%    ~     (p=0.102 n=6+6)

	name      old alloc/op   new alloc/op   delta
	Parse-16    17.1kB ± 0%    17.1kB ± 0%  -0.28%  (p=0.002 n=6+6)

	name      old allocs/op  new allocs/op  delta
	Parse-16       116 ± 0%       116 ± 0%    ~     (all equal)

After this series of commits, comparing the original code against the
latest using the "fair" benchmark gives us good numbers:

	name      old time/op    new time/op    delta
	Parse-16    39.8µs ± 1%    38.6µs ± 1%   -3.03%  (p=0.002 n=6+6)

	name      old alloc/op   new alloc/op   delta
	Parse-16    21.0kB ± 0%    17.1kB ± 0%  -18.84%  (p=0.002 n=6+6)

	name      old allocs/op  new allocs/op  delta
	Parse-16       101 ± 0%       116 ± 0%  +14.85%  (p=0.002 n=6+6)
  • Loading branch information
mvdan committed Jun 18, 2022
1 parent 3d2e71f commit 5146d3e
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions syntax/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -574,39 +574,37 @@ func (p *Parser) postNested(s saveState) {
}

func (p *Parser) unquotedWordBytes(w *Word) ([]byte, bool) {
var buf bytes.Buffer
buf := make([]byte, 0, 4)
didUnquote := false
for _, wp := range w.Parts {
if p.unquotedWordPart(&buf, wp, false) {
didUnquote = true
}
buf, didUnquote = p.unquotedWordPart(buf, wp, false)
}
return buf.Bytes(), didUnquote
return buf, didUnquote
}

func (p *Parser) unquotedWordPart(buf *bytes.Buffer, wp WordPart, quotes bool) (quoted bool) {
func (p *Parser) unquotedWordPart(buf []byte, wp WordPart, quotes bool) (_ []byte, quoted bool) {
switch x := wp.(type) {
case *Lit:
for i := 0; i < len(x.Value); i++ {
if b := x.Value[i]; b == '\\' && !quotes {
if i++; i < len(x.Value) {
buf.WriteByte(x.Value[i])
buf = append(buf, x.Value[i])
}
quoted = true
} else {
buf.WriteByte(b)
buf = append(buf, b)
}
}
case *SglQuoted:
buf.WriteString(x.Value)
buf = append(buf, []byte(x.Value)...)
quoted = true
case *DblQuoted:
for _, wp2 := range x.Parts {
p.unquotedWordPart(buf, wp2, true)
buf, _ = p.unquotedWordPart(buf, wp2, true)
}
quoted = true
}
return
return buf, quoted
}

func (p *Parser) doHeredocs() {
Expand Down

0 comments on commit 5146d3e

Please sign in to comment.