cmd/link: fewer allocs in ld.Arch.Archreloc

Archreloc had this signature:

	func(*Link, *sym.Reloc, *sym.Symbol, *int64) bool

The last *int64 argument is used as out parameter.
Passed valus could be allocated on stack, but escape analysis
fails here, leading to high number of unwanted allocs.

If instead 4th arg is passed by value, and modified values is returned,
no problems with allocations arise:

	func(*Link, *sym.Reloc, *sym.Symbol, int64) (int64, bool)

There are 2 benefits:
1. code becomes more readable.
2. less allocations.

For linking "hello world" example from net/http:

	name      old time/op  new time/op  delta
	Linker-4   530ms ± 2%   520ms ± 2%  -1.83%  (p=0.001 n=17+16)

It's top 1 in alloc_objects from memprofile:

	flat   flat%  sum%       cum    cum%
	229379 33.05% 33.05%     229379 33.05%  cmd/link/internal/ld.relocsym
	...

list relocsym:

	229379     229379 (flat, cum) 33.05% of Total
	229379     229379    183:    var o int64

After the patch, ~230k of int64 allocs (~ 1.75mb) removed.

Passes toolshash-check (toolstash cmp).

Change-Id: I25504fe27967bcff70c4b7338790f3921d15473d
Reviewed-on: https://go-review.googlesource.com/113637
Run-TryBot: Iskander Sharipov <iskander.sharipov@intel.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
isharipo 2018-05-17 19:50:29 +03:00 committed by Brad Fitzpatrick
parent 4201c2077e
commit 328adf9d62
10 changed files with 100 additions and 136 deletions

View file

@ -104,7 +104,7 @@ type Arch struct {
Solarisdynld string
Adddynrel func(*Link, *sym.Symbol, *sym.Reloc) bool
Archinit func(*Link)
Archreloc func(*Link, *sym.Reloc, *sym.Symbol, *int64) bool
Archreloc func(*Link, *sym.Reloc, *sym.Symbol, int64) (int64, bool)
Archrelocvariant func(*Link, *sym.Reloc, *sym.Symbol, int64) int64
Trampoline func(*Link, *sym.Reloc, *sym.Symbol)
Asmb func(*Link)