Skip to content

Commit 26e05b9

Browse files
Mark Freemangopherbot
authored andcommitted
internal/pkgbits: specify that RelIdx is an element index
Without this, it's not clear what this is relative to or the granularity of the index. Change-Id: Ibaabe47e089f0ba9b084523969c5347ed4c9dbee Reviewed-on: https://go-review.googlesource.com/c/go/+/674636 Auto-Submit: Mark Freeman <mark@golang.org> Reviewed-by: Robert Griesemer <gri@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
1 parent 4ce1c8e commit 26e05b9

File tree

3 files changed

+38
-32
lines changed

3 files changed

+38
-32
lines changed

src/internal/pkgbits/decoder.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ func (pr *PkgDecoder) Fingerprint() [8]byte {
131131

132132
// AbsIdx returns the absolute index for the given (section, index)
133133
// pair.
134-
func (pr *PkgDecoder) AbsIdx(k SectionKind, idx RelIndex) int {
134+
func (pr *PkgDecoder) AbsIdx(k SectionKind, idx RelElemIdx) int {
135135
absIdx := int(idx)
136136
if k > 0 {
137137
absIdx += int(pr.elemEndsEnds[k-1])
@@ -144,7 +144,7 @@ func (pr *PkgDecoder) AbsIdx(k SectionKind, idx RelIndex) int {
144144

145145
// DataIdx returns the raw element bitstream for the given (section,
146146
// index) pair.
147-
func (pr *PkgDecoder) DataIdx(k SectionKind, idx RelIndex) string {
147+
func (pr *PkgDecoder) DataIdx(k SectionKind, idx RelElemIdx) string {
148148
absIdx := pr.AbsIdx(k, idx)
149149

150150
var start uint32
@@ -157,13 +157,13 @@ func (pr *PkgDecoder) DataIdx(k SectionKind, idx RelIndex) string {
157157
}
158158

159159
// StringIdx returns the string value for the given string index.
160-
func (pr *PkgDecoder) StringIdx(idx RelIndex) string {
160+
func (pr *PkgDecoder) StringIdx(idx RelElemIdx) string {
161161
return pr.DataIdx(SectionString, idx)
162162
}
163163

164164
// NewDecoder returns a Decoder for the given (section, index) pair,
165165
// and decodes the given SyncMarker from the element bitstream.
166-
func (pr *PkgDecoder) NewDecoder(k SectionKind, idx RelIndex, marker SyncMarker) Decoder {
166+
func (pr *PkgDecoder) NewDecoder(k SectionKind, idx RelElemIdx, marker SyncMarker) Decoder {
167167
r := pr.NewDecoderRaw(k, idx)
168168
r.Sync(marker)
169169
return r
@@ -173,7 +173,7 @@ func (pr *PkgDecoder) NewDecoder(k SectionKind, idx RelIndex, marker SyncMarker)
173173
// and decodes the given SyncMarker from the element bitstream.
174174
// If possible the Decoder should be RetireDecoder'd when it is no longer
175175
// needed, this will avoid heap allocations.
176-
func (pr *PkgDecoder) TempDecoder(k SectionKind, idx RelIndex, marker SyncMarker) Decoder {
176+
func (pr *PkgDecoder) TempDecoder(k SectionKind, idx RelElemIdx, marker SyncMarker) Decoder {
177177
r := pr.TempDecoderRaw(k, idx)
178178
r.Sync(marker)
179179
return r
@@ -187,7 +187,7 @@ func (pr *PkgDecoder) RetireDecoder(d *Decoder) {
187187
// NewDecoderRaw returns a Decoder for the given (section, index) pair.
188188
//
189189
// Most callers should use NewDecoder instead.
190-
func (pr *PkgDecoder) NewDecoderRaw(k SectionKind, idx RelIndex) Decoder {
190+
func (pr *PkgDecoder) NewDecoderRaw(k SectionKind, idx RelElemIdx) Decoder {
191191
r := Decoder{
192192
common: pr,
193193
k: k,
@@ -199,13 +199,13 @@ func (pr *PkgDecoder) NewDecoderRaw(k SectionKind, idx RelIndex) Decoder {
199199
r.Relocs = make([]RefTableEntry, r.Len())
200200
for i := range r.Relocs {
201201
r.Sync(SyncReloc)
202-
r.Relocs[i] = RefTableEntry{SectionKind(r.Len()), RelIndex(r.Len())}
202+
r.Relocs[i] = RefTableEntry{SectionKind(r.Len()), RelElemIdx(r.Len())}
203203
}
204204

205205
return r
206206
}
207207

208-
func (pr *PkgDecoder) TempDecoderRaw(k SectionKind, idx RelIndex) Decoder {
208+
func (pr *PkgDecoder) TempDecoderRaw(k SectionKind, idx RelElemIdx) Decoder {
209209
r := Decoder{
210210
common: pr,
211211
k: k,
@@ -223,7 +223,7 @@ func (pr *PkgDecoder) TempDecoderRaw(k SectionKind, idx RelIndex) Decoder {
223223
}
224224
for i := range r.Relocs {
225225
r.Sync(SyncReloc)
226-
r.Relocs[i] = RefTableEntry{SectionKind(r.Len()), RelIndex(r.Len())}
226+
r.Relocs[i] = RefTableEntry{SectionKind(r.Len()), RelElemIdx(r.Len())}
227227
}
228228

229229
return r
@@ -238,7 +238,7 @@ type Decoder struct {
238238
Data strings.Reader
239239

240240
k SectionKind
241-
Idx RelIndex
241+
Idx RelElemIdx
242242
}
243243

244244
func (r *Decoder) checkErr(err error) {
@@ -292,7 +292,7 @@ func (r *Decoder) rawVarint() int64 {
292292
return x
293293
}
294294

295-
func (r *Decoder) rawReloc(k SectionKind, idx int) RelIndex {
295+
func (r *Decoder) rawReloc(k SectionKind, idx int) RelElemIdx {
296296
e := r.Relocs[idx]
297297
assert(e.Kind == k)
298298
return e.Idx
@@ -401,7 +401,7 @@ func (r *Decoder) Code(mark SyncMarker) int {
401401

402402
// Reloc decodes a relocation of expected section k from the element
403403
// bitstream and returns an index to the referenced element.
404-
func (r *Decoder) Reloc(k SectionKind) RelIndex {
404+
func (r *Decoder) Reloc(k SectionKind) RelElemIdx {
405405
r.Sync(SyncUseReloc)
406406
return r.rawReloc(k, r.Len())
407407
}
@@ -478,7 +478,7 @@ func (r *Decoder) bigFloat() *big.Float {
478478

479479
// PeekPkgPath returns the package path for the specified package
480480
// index.
481-
func (pr *PkgDecoder) PeekPkgPath(idx RelIndex) string {
481+
func (pr *PkgDecoder) PeekPkgPath(idx RelElemIdx) string {
482482
var path string
483483
{
484484
r := pr.TempDecoder(SectionPkg, idx, SyncPkgDef)
@@ -493,8 +493,8 @@ func (pr *PkgDecoder) PeekPkgPath(idx RelIndex) string {
493493

494494
// PeekObj returns the package path, object name, and CodeObj for the
495495
// specified object index.
496-
func (pr *PkgDecoder) PeekObj(idx RelIndex) (string, string, CodeObj) {
497-
var ridx RelIndex
496+
func (pr *PkgDecoder) PeekObj(idx RelElemIdx) (string, string, CodeObj) {
497+
var ridx RelElemIdx
498498
var name string
499499
var rcode int
500500
{

src/internal/pkgbits/encoder.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ type PkgEncoder struct {
2727
// stringsIdx maps previously encoded strings to their index within
2828
// the RelocString section, to allow deduplication. That is,
2929
// elems[RelocString][stringsIdx[s]] == s (if present).
30-
stringsIdx map[string]RelIndex
30+
stringsIdx map[string]RelElemIdx
3131

3232
// syncFrames is the number of frames to write at each sync
3333
// marker. A negative value means sync markers are omitted.
@@ -47,7 +47,7 @@ func (pw *PkgEncoder) SyncMarkers() bool { return pw.syncFrames >= 0 }
4747
func NewPkgEncoder(version Version, syncFrames int) PkgEncoder {
4848
return PkgEncoder{
4949
version: version,
50-
stringsIdx: make(map[string]RelIndex),
50+
stringsIdx: make(map[string]RelElemIdx),
5151
syncFrames: syncFrames,
5252
}
5353
}
@@ -106,13 +106,13 @@ func (pw *PkgEncoder) DumpTo(out0 io.Writer) (fingerprint [8]byte) {
106106

107107
// StringIdx adds a string value to the strings section, if not
108108
// already present, and returns its index.
109-
func (pw *PkgEncoder) StringIdx(s string) RelIndex {
109+
func (pw *PkgEncoder) StringIdx(s string) RelElemIdx {
110110
if idx, ok := pw.stringsIdx[s]; ok {
111111
assert(pw.elems[SectionString][idx] == s)
112112
return idx
113113
}
114114

115-
idx := RelIndex(len(pw.elems[SectionString]))
115+
idx := RelElemIdx(len(pw.elems[SectionString]))
116116
pw.elems[SectionString] = append(pw.elems[SectionString], s)
117117
pw.stringsIdx[s] = idx
118118
return idx
@@ -132,7 +132,7 @@ func (pw *PkgEncoder) NewEncoder(k SectionKind, marker SyncMarker) *Encoder {
132132
//
133133
// Most callers should use NewEncoder instead.
134134
func (pw *PkgEncoder) NewEncoderRaw(k SectionKind) *Encoder {
135-
idx := RelIndex(len(pw.elems[k]))
135+
idx := RelElemIdx(len(pw.elems[k]))
136136
pw.elems[k] = append(pw.elems[k], "") // placeholder
137137

138138
return &Encoder{
@@ -154,11 +154,11 @@ type Encoder struct {
154154
encodingRelocHeader bool
155155

156156
k SectionKind
157-
Idx RelIndex // index within relocation section
157+
Idx RelElemIdx // index within relocation section
158158
}
159159

160-
// Flush finalizes the element's bitstream and returns its [RelIndex].
161-
func (w *Encoder) Flush() RelIndex {
160+
// Flush finalizes the element's bitstream and returns its [RelElemIdx].
161+
func (w *Encoder) Flush() RelElemIdx {
162162
var sb strings.Builder
163163

164164
// Backup the data so we write the relocations at the front.
@@ -210,7 +210,7 @@ func (w *Encoder) rawVarint(x int64) {
210210
w.rawUvarint(ux)
211211
}
212212

213-
func (w *Encoder) rawReloc(k SectionKind, idx RelIndex) int {
213+
func (w *Encoder) rawReloc(k SectionKind, idx RelElemIdx) int {
214214
e := RefTableEntry{k, idx}
215215
if w.RelocMap != nil {
216216
if i, ok := w.RelocMap[e]; ok {
@@ -302,7 +302,7 @@ func (w *Encoder) Uint(x uint) { w.Uint64(uint64(x)) }
302302
// Note: Only the index is formally written into the element
303303
// bitstream, so bitstream decoders must know from context which
304304
// section an encoded relocation refers to.
305-
func (w *Encoder) Reloc(k SectionKind, idx RelIndex) {
305+
func (w *Encoder) Reloc(k SectionKind, idx RelElemIdx) {
306306
w.Sync(SyncUseReloc)
307307
w.Len(w.rawReloc(k, idx))
308308
}
@@ -325,7 +325,7 @@ func (w *Encoder) String(s string) {
325325

326326
// StringRef writes a reference to the given index, which must be a
327327
// previously encoded string value.
328-
func (w *Encoder) StringRef(idx RelIndex) {
328+
func (w *Encoder) StringRef(idx RelElemIdx) {
329329
w.Sync(SyncString)
330330
w.Reloc(SectionString, idx)
331331
}

src/internal/pkgbits/reloc.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,26 @@ const (
2828
// particular section.
2929
type Index int32
3030

31-
// TODO(markfreeman): Make RelIndex its own named type once we point external
32-
// references from Index to RelIndex.
33-
type RelIndex = Index
31+
// An AbsElemIdx, or absolute element index, is an index into the elements
32+
// that is not relative to some other index.
33+
type AbsElemIdx = uint32
3434

35+
// TODO(markfreeman): Make this its own type.
36+
// A RelElemIdx, or relative element index, is an index into the elements
37+
// relative to some other index, such as the start of a section.
38+
type RelElemIdx = Index
39+
40+
// TODO(markfreeman): Isn't this strictly less efficient than an AbsElemIdx?
3541
// A RefTableEntry is an entry in an element's reference table. All
3642
// elements are preceded by a reference table which provides locations
3743
// for referenced elements.
3844
type RefTableEntry struct {
3945
Kind SectionKind
40-
Idx RelIndex
46+
Idx RelElemIdx
4147
}
4248

4349
// Reserved indices within the [SectionMeta] section.
4450
const (
45-
PublicRootIdx RelIndex = 0
46-
PrivateRootIdx RelIndex = 1
51+
PublicRootIdx RelElemIdx = 0
52+
PrivateRootIdx RelElemIdx = 1
4753
)

0 commit comments

Comments
 (0)