]> Cypherpunks repositories - gostls13.git/commit
go/printer: avoid allocating for every position printed
authorDaniel Martí <mvdan@mvdan.cc>
Wed, 15 Jun 2022 21:55:58 +0000 (22:55 +0100)
committerRobert Griesemer <gri@google.com>
Thu, 18 Aug 2022 16:35:13 +0000 (16:35 +0000)
commitb2c0417c429891b9c965abe1d09606b0dba00403
tree1b40b5b1aa9062d4724262132fd51678b1e096b2
parentb6149224499108d4ea6e562fd9fd7eac35f50a6d
go/printer: avoid allocating for every position printed

printer.print is an overloaded method for multiple purposes.
When fed a position, it updates the current position.
When fed a string, it prints the string.
When fed a token, it prints the token. And so on.

However, this overloading comes at a significant cost.
Because the parameters are a list of the `any` interface type,
any type which is not of pointer or interface kind will allocate when
passed as an argument, as interfaces can only contain pointers.

A large portion of the arguments passed to the print method are of type
token.Pos, whose underlying type is int - so it allocates.
Removing those allocations has a significant benefit,
at the cost of some verbosity in the code:

name      old time/op    new time/op    delta
Print-16    6.10ms ± 2%    5.39ms ± 2%  -11.72%  (p=0.000 n=10+10)

name      old speed      new speed      delta
Print-16  8.50MB/s ± 2%  9.63MB/s ± 2%  +13.28%  (p=0.000 n=10+10)

name      old alloc/op   new alloc/op   delta
Print-16     443kB ± 0%     332kB ± 0%  -25.10%  (p=0.000 n=10+9)

name      old allocs/op  new allocs/op  delta
Print-16     17.3k ± 0%      3.5k ± 0%  -80.10%  (p=0.000 n=10+10)

There should be more significant speed-ups left, particularly for the
token.Token, string, and whiteSpace types fed to the same method.
They are left for a future CL, in case this kind of optimization is not
a path we want to take.

Change-Id: I3ff8387242c5a935bb003e60e0813b7b9c65402e
Reviewed-on: https://go-review.googlesource.com/c/go/+/412557
Run-TryBot: Daniel Martí <mvdan@mvdan.cc>
Reviewed-by: hopehook <hopehook@qq.com>
Reviewed-by: Roland Shoemaker <roland@golang.org>
Reviewed-by: Robert Griesemer <gri@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
src/go/printer/nodes.go
src/go/printer/printer.go