Skip to content

Commit

Permalink
syntax: use fewer tabwriter escape characters
Browse files Browse the repository at this point in the history
name     old time/op    new time/op    delta
Print-8    11.5µs ± 0%    11.4µs ± 1%   -1.03%  (p=0.002 n=6+6)

name     old alloc/op   new alloc/op   delta
Print-8      344B ± 0%      312B ± 0%   -9.30%  (p=0.002 n=6+6)

name     old allocs/op  new allocs/op  delta
Print-8      10.0 ± 0%       9.0 ± 0%  -10.00%  (p=0.002 n=6+6)

We'll leave the tabwriter optimizations here for now. I've sent
https://go-review.googlesource.com/c/go/+/166797 to upstream, which
shaves off 27% from our benchmark. In total, the overhead of tabwriter
goes down from the initial ~135% to ~55%, which is much more acceptable.
We even allocate less garbage, too.

name     old time/op    new time/op    delta
Print-8    5.29µs ± 0%    8.21µs ± 1%  +55.19%  (p=0.002 n=6+6)

name     old alloc/op   new alloc/op   delta
Print-8      344B ± 0%      312B ± 0%   -9.30%  (p=0.002 n=6+6)

name     old allocs/op  new allocs/op  delta
Print-8      13.0 ± 0%       9.0 ± 0%  -30.77%  (p=0.002 n=6+6)
  • Loading branch information
mvdan committed Mar 11, 2019
1 parent 419909d commit 6712015
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions syntax/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func (c *colCounter) Reset(w io.Writer) {
type Printer struct {
bufWriter
tabWriter *tabwriter.Writer
cols colCounter
cols colCounter

indentSpaces uint
binNextLine bool
Expand Down Expand Up @@ -297,16 +297,16 @@ func (p *Printer) writeLit(s string) {
p.WriteString(s)
return
}
p.WriteByte('\xff')
for i := 0; i < len(s); i++ {
b := s[i]
if b != '\t' {
p.WriteByte(b)
continue
}
p.WriteByte('\xff')
p.WriteByte(b)
p.WriteByte('\xff')
}
p.WriteByte('\xff')
}

func (p *Printer) incLevel() {
Expand Down Expand Up @@ -1188,9 +1188,13 @@ func (e *extraIndenter) WriteByte(b byte) error {
if b != '\n' {
return nil
}
trimmed := bytes.TrimLeft(e.curLine, "\xff\t")
// divided by 3, as each tab is escaped via "\xff\t\xff"
lineIndent := (len(e.curLine) - len(trimmed)) / 3
line := e.curLine
if bytes.HasPrefix(e.curLine, []byte("\xff")) {
// beginning a multiline sequence, with the leading escape
line = line[1:]
}
trimmed := bytes.TrimLeft(line, "\t")
lineIndent := len(line) - len(trimmed)
if e.firstIndent < 0 {
e.firstIndent = lineIndent
e.firstChange = e.baseIndent - lineIndent
Expand Down

0 comments on commit 6712015

Please sign in to comment.