From 028247d2cd82243b44b00c5da477f971112e5ff9 Mon Sep 17 00:00:00 2001 From: Josh Bleecher Snyder Date: Thu, 11 Jun 2015 13:56:28 -0700 Subject: [PATCH] cmd/compile: reuse []Flow MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Benchmarked using compilebench on a quiet but rather old OS X laptop. Benchmarks from others would be welcome, since the numbers look too good to be true. name old time/op new time/op delta Template 331ms ± 9% 303ms ± 4% -8.25% (p=0.000 n=24+24) GoTypes 946ms ± 4% 888ms ± 3% -6.17% (p=0.000 n=24+25) Compiler 3.20s ± 1% 3.10s ± 2% -3.07% (p=0.000 n=24+25) name old alloc/op new alloc/op delta Template 72.5MB ± 0% 61.8MB ± 0% -14.76% (p=0.000 n=25+24) GoTypes 224MB ± 0% 189MB ± 0% -15.65% (p=0.000 n=25+25) Compiler 695MB ± 0% 561MB ± 0% -19.26% (p=0.000 n=25+25) name old allocs/op new allocs/op delta Template 498k ± 0% 497k ± 0% -0.21% (p=0.000 n=25+23) GoTypes 1.47M ± 0% 1.47M ± 0% -0.25% (p=0.000 n=25+25) Compiler 4.09M ± 0% 4.08M ± 0% -0.18% (p=0.000 n=25+23) Change-Id: I2394bc748128d721863453257fa5756c410f7898 Reviewed-on: https://go-review.googlesource.com/19771 Reviewed-by: Russ Cox Run-TryBot: Russ Cox TryBot-Result: Gobot Gobot --- src/cmd/compile/internal/gc/popt.go | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/cmd/compile/internal/gc/popt.go b/src/cmd/compile/internal/gc/popt.go index 4d71ab643da..b7082228455 100644 --- a/src/cmd/compile/internal/gc/popt.go +++ b/src/cmd/compile/internal/gc/popt.go @@ -241,6 +241,19 @@ var flowmark int // will not have flow graphs and consequently will not be optimized. const MaxFlowProg = 50000 +var ffcache []Flow // reusable []Flow, to reduce allocation + +func growffcache(n int) { + if n > cap(ffcache) { + n = (n * 5) / 4 + if n > MaxFlowProg { + n = MaxFlowProg + } + ffcache = make([]Flow, n) + } + ffcache = ffcache[:n] +} + func Flowstart(firstp *obj.Prog, newData func() interface{}) *Graph { // Count and mark instructions to annotate. nf := 0 @@ -268,7 +281,9 @@ func Flowstart(firstp *obj.Prog, newData func() interface{}) *Graph { // Allocate annotations and assign to instructions. graph := new(Graph) - ff := make([]Flow, nf) + + growffcache(nf) + ff := ffcache start := &ff[0] id := 0 var last *Flow @@ -331,6 +346,10 @@ func Flowend(graph *Graph) { f.Prog.Info.Flags = 0 // drop cached proginfo f.Prog.Opt = nil } + clear := ffcache[:graph.Num] + for i := range clear { + clear[i] = Flow{} + } } // find looping structure