aboutsummaryrefslogtreecommitdiff
path: root/src/internal/pkgbits/encoder.go
diff options
context:
space:
mode:
authorDavid Chase <drchase@google.com>2022-08-09 14:07:02 -0400
committerDavid Chase <drchase@google.com>2022-08-10 22:22:48 +0000
commit6b80b62fd1b5338e6ec8bc2ff521b94f2fefae9c (patch)
treeb20281871b3176360edef536bf36017818e991f9 /src/internal/pkgbits/encoder.go
parent8003efe1b5520476c62c7fa6798150a61d621cde (diff)
downloadgo-6b80b62fd1b5338e6ec8bc2ff521b94f2fefae9c.tar.xz
internal/pkgbits: fix performance of rawReloc
There was a TODO about quadratic performance, and indeed, it can get bad. Added a map, made some integers that are unlikely to exceed a few million into 32-bit integers. Change-Id: I6facf2eabc00483e943b326ca8dcae2f778093da Reviewed-on: https://go-review.googlesource.com/c/go/+/422297 Run-TryBot: David Chase <drchase@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Matthew Dempsky <mdempsky@google.com>
Diffstat (limited to 'src/internal/pkgbits/encoder.go')
-rw-r--r--src/internal/pkgbits/encoder.go18
1 files changed, 11 insertions, 7 deletions
diff --git a/src/internal/pkgbits/encoder.go b/src/internal/pkgbits/encoder.go
index 07695b5751..3859b0f091 100644
--- a/src/internal/pkgbits/encoder.go
+++ b/src/internal/pkgbits/encoder.go
@@ -151,8 +151,9 @@ func (pw *PkgEncoder) NewEncoderRaw(k RelocKind) Encoder {
type Encoder struct {
p *PkgEncoder
- Relocs []RelocEnt
- Data bytes.Buffer // accumulated element bitstream data
+ Relocs []RelocEnt
+ RelocMap map[RelocEnt]uint32
+ Data bytes.Buffer // accumulated element bitstream data
encodingRelocHeader bool
@@ -214,15 +215,18 @@ func (w *Encoder) rawVarint(x int64) {
}
func (w *Encoder) rawReloc(r RelocKind, idx Index) int {
- // TODO(mdempsky): Use map for lookup; this takes quadratic time.
- for i, rEnt := range w.Relocs {
- if rEnt.Kind == r && rEnt.Idx == idx {
- return i
+ e := RelocEnt{r, idx}
+ if w.RelocMap != nil {
+ if i, ok := w.RelocMap[e]; ok {
+ return int(i)
}
+ } else {
+ w.RelocMap = make(map[RelocEnt]uint32)
}
i := len(w.Relocs)
- w.Relocs = append(w.Relocs, RelocEnt{r, idx})
+ w.RelocMap[e] = uint32(i)
+ w.Relocs = append(w.Relocs, e)
return i
}