cmd/compile: add cache of sizeable objects so they can be reused

We kind of have this mechanism already, just normalizing it and
using it in a bunch of places. Previously a bunch of places cached
slices only for the duration of a single function compilation. Now
we can reuse slices across a whole compiler run.

Use a sync.Pool of powers-of-two sizes. This lets us use not
too much memory, and avoid holding onto memory we're no longer
using when a GC happens.

There's a few different types we need, so generate the code for it.
Generics would be useful here, but we can't use generics in the
compiler because of bootstrapping.

Change-Id: I6cf37e7b7b2e802882aaa723a0b29770511ccd82
Reviewed-on: https://go-review.googlesource.com/c/go/+/444820
Run-TryBot: Keith Randall <khr@golang.org>
Reviewed-by: Heschi Kreinick <heschi@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: David Chase <drchase@google.com>
This commit is contained in:
Keith Randall 2022-10-18 16:07:36 -07:00 committed by Keith Randall
parent 7ddc45263c
commit 68bd383368
21 changed files with 656 additions and 213 deletions

View file

@ -30,7 +30,8 @@ func loopRotate(f *Func) {
return
}
idToIdx := make([]int, f.NumBlocks())
idToIdx := f.Cache.allocIntSlice(f.NumBlocks())
defer f.Cache.freeIntSlice(idToIdx)
for i, b := range f.Blocks {
idToIdx[b.ID] = i
}
@ -92,20 +93,21 @@ func loopRotate(f *Func) {
// Some blocks that are not part of a loop may be placed
// between loop blocks. In order to avoid these blocks from
// being overwritten, use a temporary slice.
newOrder := make([]*Block, 0, f.NumBlocks())
for _, b := range f.Blocks {
oldOrder := f.Cache.allocBlockSlice(len(f.Blocks))
defer f.Cache.freeBlockSlice(oldOrder)
copy(oldOrder, f.Blocks)
for _, b := range oldOrder {
if _, ok := move[b.ID]; ok {
continue
}
newOrder = append(newOrder, b)
f.Blocks[j] = b
j++
for _, a := range after[b.ID] {
newOrder = append(newOrder, a)
f.Blocks[j] = a
j++
}
}
if j != len(f.Blocks) {
if j != len(oldOrder) {
f.Fatalf("bad reordering in looprotate")
}
f.Blocks = newOrder
}