Commit 51711d14 authored by Russ Cox's avatar Russ Cox

cmd/link: fix accidentally-quadratic library loading

Programs built from N libraries required O(N²) time to do the
deduplication checks, even if there were never any duplicates.
In most programs N is small enough not to worry, but this may
affect large programs.

Noticed by inspection, not any specific bug report.

Fixes #20578.

Change-Id: Ic4108f1058be39da990a79b1e0b8ce95fde44cef
Reviewed-on: https://go-review.googlesource.com/44852
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarIan Lance Taylor <iant@golang.org>
parent 6a425687
...@@ -50,10 +50,8 @@ func addlib(ctxt *Link, src string, obj string, pathname string) *Library { ...@@ -50,10 +50,8 @@ func addlib(ctxt *Link, src string, obj string, pathname string) *Library {
} }
// already loaded? // already loaded?
for i := 0; i < len(ctxt.Library); i++ { if l := ctxt.LibraryByPkg[pkg]; l != nil {
if ctxt.Library[i].Pkg == pkg { return l
return ctxt.Library[i]
}
} }
var pname string var pname string
...@@ -97,18 +95,17 @@ func addlib(ctxt *Link, src string, obj string, pathname string) *Library { ...@@ -97,18 +95,17 @@ func addlib(ctxt *Link, src string, obj string, pathname string) *Library {
* pkg: package import path, e.g. container/vector * pkg: package import path, e.g. container/vector
*/ */
func addlibpath(ctxt *Link, srcref string, objref string, file string, pkg string, shlibnamefile string) *Library { func addlibpath(ctxt *Link, srcref string, objref string, file string, pkg string, shlibnamefile string) *Library {
for i := 0; i < len(ctxt.Library); i++ { if l := ctxt.LibraryByPkg[pkg]; l != nil {
if pkg == ctxt.Library[i].Pkg { return l
return ctxt.Library[i]
}
} }
if ctxt.Debugvlog > 1 { if ctxt.Debugvlog > 1 {
ctxt.Logf("%5.2f addlibpath: srcref: %s objref: %s file: %s pkg: %s shlibnamefile: %s\n", Cputime(), srcref, objref, file, pkg, shlibnamefile) ctxt.Logf("%5.2f addlibpath: srcref: %s objref: %s file: %s pkg: %s shlibnamefile: %s\n", Cputime(), srcref, objref, file, pkg, shlibnamefile)
} }
ctxt.Library = append(ctxt.Library, &Library{}) l := &Library{}
l := ctxt.Library[len(ctxt.Library)-1] ctxt.LibraryByPkg[pkg] = l
ctxt.Library = append(ctxt.Library, l)
l.Objref = objref l.Objref = objref
l.Srcref = srcref l.Srcref = srcref
l.File = file l.File = file
......
...@@ -223,6 +223,7 @@ type Link struct { ...@@ -223,6 +223,7 @@ type Link struct {
Tlsg *Symbol Tlsg *Symbol
Libdir []string Libdir []string
Library []*Library Library []*Library
LibraryByPkg map[string]*Library
Shlibs []Shlib Shlibs []Shlib
Tlsoffset int Tlsoffset int
Textp []*Symbol Textp []*Symbol
......
...@@ -48,6 +48,7 @@ func linknew(arch *sys.Arch) *Link { ...@@ -48,6 +48,7 @@ func linknew(arch *sys.Arch) *Link {
Allsym: make([]*Symbol, 0, 100000), Allsym: make([]*Symbol, 0, 100000),
}, },
Arch: arch, Arch: arch,
LibraryByPkg: make(map[string]*Library),
} }
if objabi.GOARCH != arch.Name { if objabi.GOARCH != arch.Name {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment