Skip to content

Commit d41a0a0

Browse files
committed
cmd/compile: remove large intermediate slice from gc.scopePCs
Three loops can be converted into one. Minor reviewer-recommended refactoring. Passes toolstash-check. Updates #27739. Change-Id: Ia87a11d88ae3ce56fcc4267fe6c5a9c13bf7f533 Reviewed-on: https://go-review.googlesource.com/c/go/+/176577 Run-TryBot: David Chase <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Josh Bleecher Snyder <[email protected]> Reviewed-by: Alessandro Arzilli <[email protected]>
1 parent 2fd97ee commit d41a0a0

File tree

2 files changed

+18
-30
lines changed

2 files changed

+18
-30
lines changed

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

+4-30
Original file line numberDiff line numberDiff line change
@@ -55,51 +55,25 @@ func scopeVariables(dwarfVars []*dwarf.Var, varScopes []ScopeID, dwarfScopes []d
5555
}
5656
}
5757

58-
// A scopedPCs represents a non-empty half-open interval of PCs that
59-
// share a common source position.
60-
type scopedPCs struct {
61-
start, end int64
62-
pos src.XPos
63-
scope ScopeID
64-
}
65-
6658
// scopePCs assigns PC ranges to their scopes.
6759
func scopePCs(fnsym *obj.LSym, marks []Mark, dwarfScopes []dwarf.Scope) {
6860
// If there aren't any child scopes (in particular, when scope
6961
// tracking is disabled), we can skip a whole lot of work.
7062
if len(marks) == 0 {
7163
return
7264
}
73-
74-
// Break function text into scopedPCs.
75-
var pcs []scopedPCs
7665
p0 := fnsym.Func.Text
66+
scope := findScope(marks, p0.Pos)
7767
for p := fnsym.Func.Text; p != nil; p = p.Link {
7868
if p.Pos == p0.Pos {
7969
continue
8070
}
81-
if p0.Pc < p.Pc {
82-
pcs = append(pcs, scopedPCs{start: p0.Pc, end: p.Pc, pos: p0.Pos})
83-
}
71+
dwarfScopes[scope].AppendRange(dwarf.Range{Start: p0.Pc, End: p.Pc})
8472
p0 = p
73+
scope = findScope(marks, p0.Pos)
8574
}
8675
if p0.Pc < fnsym.Size {
87-
pcs = append(pcs, scopedPCs{start: p0.Pc, end: fnsym.Size, pos: p0.Pos})
88-
}
89-
90-
// Assign scopes to each chunk of instructions.
91-
for i := range pcs {
92-
pcs[i].scope = findScope(marks, pcs[i].pos)
93-
}
94-
95-
// Create sorted PC ranges for each DWARF scope.
96-
for _, pc := range pcs {
97-
r := &dwarfScopes[pc.scope].Ranges
98-
if i := len(*r); i > 0 && (*r)[i-1].End == pc.start {
99-
(*r)[i-1].End = pc.end
100-
} else {
101-
*r = append(*r, dwarf.Range{Start: pc.start, End: pc.end})
102-
}
76+
dwarfScopes[scope].AppendRange(dwarf.Range{Start: p0.Pc, End: fnsym.Size})
10377
}
10478
}
10579

src/cmd/internal/dwarf/dwarf.go

+14
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,20 @@ func (s *Scope) UnifyRanges(c *Scope) {
145145
s.Ranges = out
146146
}
147147

148+
// AppendRange adds r to s, if r is non-empty.
149+
// If possible, it extends the last Range in s.Ranges; if not, it creates a new one.
150+
func (s *Scope) AppendRange(r Range) {
151+
if r.End <= r.Start {
152+
return
153+
}
154+
i := len(s.Ranges)
155+
if i > 0 && s.Ranges[i-1].End == r.Start {
156+
s.Ranges[i-1].End = r.End
157+
return
158+
}
159+
s.Ranges = append(s.Ranges, r)
160+
}
161+
148162
type InlCalls struct {
149163
Calls []InlCall
150164
}

0 commit comments

Comments
 (0)