Skip to content

Commit ee7d9f1

Browse files
committed
cmd/compile: make LivenessMap sparse
We're about to switch to having significantly fewer maps in the liveness map, so switch from a dense representation to a sparse representation. Passes toolstash-check. For #36365. Change-Id: Icb17bd6ace17667a280bc5fba4039cae3020a8d1 Reviewed-on: https://go-review.googlesource.com/c/go/+/230543 Run-TryBot: Austin Clements <austin@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Cherry Zhang <cherryyz@google.com> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
1 parent 601bc41 commit ee7d9f1

File tree

1 file changed

+17
-19
lines changed

1 file changed

+17
-19
lines changed

src/cmd/compile/internal/gc/plive.go

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -147,32 +147,29 @@ type openDeferVarInfo struct {
147147

148148
// LivenessMap maps from *ssa.Value to LivenessIndex.
149149
type LivenessMap struct {
150-
m []LivenessIndex
150+
vals map[ssa.ID]LivenessIndex
151151
}
152152

153-
func (m *LivenessMap) reset(ids int) {
154-
m2 := m.m
155-
if ids > cap(m2) {
156-
m2 = make([]LivenessIndex, ids)
153+
func (m *LivenessMap) reset() {
154+
if m.vals == nil {
155+
m.vals = make(map[ssa.ID]LivenessIndex)
157156
} else {
158-
m2 = m2[:ids]
159-
}
160-
none := LivenessInvalid
161-
for i := range m2 {
162-
m2[i] = none
157+
for k := range m.vals {
158+
delete(m.vals, k)
159+
}
163160
}
164-
m.m = m2
165161
}
166162

167163
func (m *LivenessMap) set(v *ssa.Value, i LivenessIndex) {
168-
m.m[v.ID] = i
164+
m.vals[v.ID] = i
169165
}
170166

171167
func (m LivenessMap) Get(v *ssa.Value) LivenessIndex {
172-
if int(v.ID) < len(m.m) {
173-
return m.m[int(v.ID)]
168+
// All safe-points are in the map, so if v isn't in
169+
// the map, it's an unsafe-point.
170+
if idx, ok := m.vals[v.ID]; ok {
171+
return idx
174172
}
175-
// Not a safe point.
176173
return LivenessInvalid
177174
}
178175

@@ -515,15 +512,16 @@ func newliveness(fn *Node, f *ssa.Func, vars []*Node, idx map[*Node]int32, stkpt
515512

516513
// Significant sources of allocation are kept in the ssa.Cache
517514
// and reused. Surprisingly, the bit vectors themselves aren't
518-
// a major source of allocation, but the slices are.
515+
// a major source of allocation, but the liveness maps are.
519516
if lc, _ := f.Cache.Liveness.(*livenessFuncCache); lc == nil {
520517
// Prep the cache so liveness can fill it later.
521518
f.Cache.Liveness = new(livenessFuncCache)
522519
} else {
523520
if cap(lc.be) >= f.NumBlocks() {
524521
lv.be = lc.be[:f.NumBlocks()]
525522
}
526-
lv.livenessMap = LivenessMap{lc.livenessMap.m[:0]}
523+
lv.livenessMap = LivenessMap{lc.livenessMap.vals}
524+
lc.livenessMap.vals = nil
527525
}
528526
if lv.be == nil {
529527
lv.be = make([]BlockEffects, f.NumBlocks())
@@ -540,7 +538,7 @@ func newliveness(fn *Node, f *ssa.Func, vars []*Node, idx map[*Node]int32, stkpt
540538
be.livein = varRegVec{vars: bulk.next()}
541539
be.liveout = varRegVec{vars: bulk.next()}
542540
}
543-
lv.livenessMap.reset(lv.f.NumValues())
541+
lv.livenessMap.reset()
544542

545543
lv.markUnsafePoints()
546544
return lv
@@ -1559,7 +1557,7 @@ func liveness(e *ssafn, f *ssa.Func, pp *Progs) LivenessMap {
15591557
}
15601558
cache.be = lv.be
15611559
}
1562-
if cap(lv.livenessMap.m) < 2000 {
1560+
if len(lv.livenessMap.vals) < 2000 {
15631561
cache.livenessMap = lv.livenessMap
15641562
}
15651563
}

0 commit comments

Comments
 (0)