From 68bd383368b5958f8f02c49bc75134a0ef61daec Mon Sep 17 00:00:00 2001 From: Keith Randall Date: Tue, 18 Oct 2022 16:07:36 -0700 Subject: 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 Reviewed-by: Heschi Kreinick TryBot-Result: Gopher Robot Reviewed-by: David Chase --- src/cmd/compile/internal/ssa/stackalloc.go | 26 ++++---------------------- 1 file changed, 4 insertions(+), 22 deletions(-) (limited to 'src/cmd/compile/internal/ssa/stackalloc.go') diff --git a/src/cmd/compile/internal/ssa/stackalloc.go b/src/cmd/compile/internal/ssa/stackalloc.go index d41f3996af..3e24b48a69 100644 --- a/src/cmd/compile/internal/ssa/stackalloc.go +++ b/src/cmd/compile/internal/ssa/stackalloc.go @@ -25,8 +25,6 @@ type stackAllocState struct { values []stackValState interfere [][]ID // interfere[v.id] = values that interfere with v. names []LocalSlot - slots []int - used []bool nArgSlot, // Number of Values sourced to arg slot nNotNeed, // Number of Values not needing a stack slot @@ -57,12 +55,6 @@ func putStackAllocState(s *stackAllocState) { for i := range s.names { s.names[i] = LocalSlot{} } - for i := range s.slots { - s.slots[i] = 0 - } - for i := range s.used { - s.used[i] = false - } s.f.Cache.stackAllocState = s s.f = nil s.live = nil @@ -218,25 +210,15 @@ func (s *stackAllocState) stackalloc() { // Each time we assign a stack slot to a value v, we remember // the slot we used via an index into locations[v.Type]. - slots := s.slots - if n := f.NumValues(); cap(slots) >= n { - slots = slots[:n] - } else { - slots = make([]int, n) - s.slots = slots - } + slots := f.Cache.allocIntSlice(f.NumValues()) + defer f.Cache.freeIntSlice(slots) for i := range slots { slots[i] = -1 } // Pick a stack slot for each value needing one. - var used []bool - if n := f.NumValues(); cap(s.used) >= n { - used = s.used[:n] - } else { - used = make([]bool, n) - s.used = used - } + used := f.Cache.allocBoolSlice(f.NumValues()) + defer f.Cache.freeBoolSlice(used) for _, b := range f.Blocks { for _, v := range b.Values { if !s.values[v.ID].needSlot { -- cgit v1.3-5-g9baa