aboutsummaryrefslogtreecommitdiff
path: root/src/runtime
diff options
context:
space:
mode:
authorDaniel Martí <mvdan@mvdan.cc>2022-06-15 22:55:58 +0100
committerRobert Griesemer <gri@google.com>2022-08-18 16:35:13 +0000
commitb2c0417c429891b9c965abe1d09606b0dba00403 (patch)
tree1b40b5b1aa9062d4724262132fd51678b1e096b2 /src/runtime
parentb6149224499108d4ea6e562fd9fd7eac35f50a6d (diff)
downloadgo-b2c0417c429891b9c965abe1d09606b0dba00403.tar.xz
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>
Diffstat (limited to 'src/runtime')
0 files changed, 0 insertions, 0 deletions