From: Josh Bleecher Snyder Date: Wed, 11 Jan 2017 21:58:20 +0000 (-0800) Subject: cmd/compile: reduce slice growth in fuseBlockPlain X-Git-Tag: go1.9beta1~1783 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=16e430e1ef8b9c9259bb4f07a0787de4310b3041;p=gostls13.git cmd/compile: reduce slice growth in fuseBlockPlain Instead of always appending to c.Values, choose whichever slice is larger; b.Values will be set to nil anyway. Appending once instead of in a loop also limits slice growth to once per function call and is more efficient. Reduces max rss for the program in #18602 by 6.5%, and eliminates fuseBlockPlain from the alloc_space pprof output. fuseBlockPlain previously accounted for 16.74% of allocated memory. Updates #18602. Change-Id: I417b03722d011a59a679157da43dc91f4425210e Reviewed-on: https://go-review.googlesource.com/35114 Run-TryBot: Josh Bleecher Snyder Reviewed-by: Keith Randall TryBot-Result: Gobot Gobot --- diff --git a/src/cmd/compile/internal/ssa/fuse.go b/src/cmd/compile/internal/ssa/fuse.go index d5940da439..94a96120fa 100644 --- a/src/cmd/compile/internal/ssa/fuse.go +++ b/src/cmd/compile/internal/ssa/fuse.go @@ -121,7 +121,14 @@ func fuseBlockPlain(b *Block) bool { // move all of b's values to c. for _, v := range b.Values { v.Block = c - c.Values = append(c.Values, v) + } + // Use whichever value slice is larger, in the hopes of avoiding growth. + // However, take care to avoid c.Values pointing to b.valstorage. + // See golang.org/issue/18602. + if cap(c.Values) >= cap(b.Values) || len(b.Values) <= len(b.valstorage) { + c.Values = append(c.Values, b.Values...) + } else { + c.Values = append(b.Values, c.Values...) } // replace b->c edge with preds(b) -> c