Skip to content

Commit

Permalink
syntax: reuse a single tabwriter in a printer
Browse files Browse the repository at this point in the history
Mostly allows saving allocations.

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

name     old alloc/op   new alloc/op   delta
Print-8    1.06kB ± 0%    0.34kB ± 0%  -67.42%  (p=0.002 n=6+6)

name     old allocs/op  new allocs/op  delta
Print-8      24.0 ± 0%      10.0 ± 0%  -58.33%  (p=0.002 n=6+6)
  • Loading branch information
mvdan committed Mar 11, 2019
1 parent 6276884 commit 419909d
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion syntax/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func NewPrinter(options ...func(*Printer)) *Printer {
p := &Printer{
bufWriter: bufio.NewWriter(nil),
tabsPrinter: new(Printer),
tabWriter: new(tabwriter.Writer),
}
for _, opt := range options {
opt(p)
Expand Down Expand Up @@ -82,7 +83,8 @@ func (p *Printer) Print(w io.Writer, node Node) error {
// indenting with spaces
tabwidth = int(p.indentSpaces)
}
w = tabwriter.NewWriter(w, 0, tabwidth, 1, ' ', twmode)
p.tabWriter.Init(w, 0, tabwidth, 1, ' ', twmode)
w = p.tabWriter

p.bufWriter.Reset(w)
switch x := node.(type) {
Expand Down Expand Up @@ -164,6 +166,7 @@ func (c *colCounter) Reset(w io.Writer) {
// program.
type Printer struct {
bufWriter
tabWriter *tabwriter.Writer
cols colCounter

indentSpaces uint
Expand Down

0 comments on commit 419909d

Please sign in to comment.