]> Cypherpunks repositories - gostls13.git/commitdiff
internal/pkgbits: fix performance of rawReloc
authorDavid Chase <drchase@google.com>
Tue, 9 Aug 2022 18:07:02 +0000 (14:07 -0400)
committerDavid Chase <drchase@google.com>
Wed, 10 Aug 2022 22:22:48 +0000 (22:22 +0000)
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>
src/internal/pkgbits/encoder.go
src/internal/pkgbits/reloc.go

index 07695b57512a73cbcae8efde57b0392ee7e2e9a9..3859b0f0910162d639b46c211dc7776ed9db55e3 100644 (file)
@@ -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
 }
 
index 7a8f04ab3fc665448d6ffc97f5cf6271ee39c5c5..fcdfb97ca992612442adf5f837649a9dff9d90bc 100644 (file)
@@ -5,11 +5,11 @@
 package pkgbits
 
 // A RelocKind indicates a particular section within a unified IR export.
-type RelocKind int
+type RelocKind int32
 
 // An Index represents a bitstream element index within a particular
 // section.
-type Index int
+type Index int32
 
 // A relocEnt (relocation entry) is an entry in an element's local
 // reference table.