From 4f2299dfb2918c7b7b8730abbb7ee2db58d1f180 Mon Sep 17 00:00:00 2001 From: Connor Brewster Date: Tue, 1 Sep 2020 11:29:44 -0600 Subject: [PATCH] Lazily load python package mappings These were always being loaded into memory, causing significant overhead for every UPM command --- internal/backends/python/gen_pypi_map/main.go | 15 +++++++++++---- internal/backends/python/python.go | 6 +++--- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/internal/backends/python/gen_pypi_map/main.go b/internal/backends/python/gen_pypi_map/main.go index b7e80acc..e1807951 100644 --- a/internal/backends/python/gen_pypi_map/main.go +++ b/internal/backends/python/gen_pypi_map/main.go @@ -307,7 +307,11 @@ func main() { // moduleToPypiPackage holds a map of all known modules to their corresponding // best matching package. This helps us guess which packages should be installed // for the given imports. -var moduleToPypiPackage = map[string]string{ +var moduleToPypiPackageCached = map[string]string{} + +func moduleToPypiPackage() map[string]string { + if len(moduleToPypiPackageCached) == 0 { + moduleToPypiPackageCached = map[string]string{ `) addMap := func(mod, pkg, comment string) { @@ -368,7 +372,7 @@ nextpkg: } } - fmt.Fprintf(outgo, "}\n") + fmt.Fprintf(outgo, "}\n}\nreturn moduleToPypiPackageCached }\n") fmt.Fprintf(outgo, ` // pypiPackageToModules holds a map of every known python package to the modules @@ -378,8 +382,11 @@ nextpkg: // // The module names are comma separated because go's compiler seems to vomit // when you create too many slices. +var pypiPackageToModulesCached = map[string]string{} -var pypiPackageToModules = map[string]string{ +func pypiPackageToModules() map[string]string { + if len(pypiPackageToModulesCached) == 0 { + pypiPackageToModulesCached = map[string]string{ `) for _, pkg := range pkgs { @@ -403,7 +410,7 @@ var pypiPackageToModules = map[string]string{ fmt.Fprintf(outgo, "\n") } - fmt.Fprintf(outgo, "}\n") + fmt.Fprintf(outgo, "}\n}\nreturn pypiPackageToModulesCached\n}") err = outgo.Close() if err != nil { diff --git a/internal/backends/python/python.go b/internal/backends/python/python.go index ea8feb94..4ad999a8 100644 --- a/internal/backends/python/python.go +++ b/internal/backends/python/python.go @@ -391,7 +391,7 @@ func guess(python string) (map[api.PkgName]bool, bool) { if knownPkgs, err := listSpecfile(); err == nil { for pkgName := range knownPkgs { - mods, ok := pypiPackageToModules[string(pkgName)] + mods, ok := pypiPackageToModules()[string(pkgName)] if ok { for _, mod := range strings.Split(mods, ",") { availMods[mod] = true @@ -409,13 +409,13 @@ func guess(python string) (map[api.PkgName]bool, bool) { } // If this module has a package pragma, use that - if pragmas.Package != ""{ + if pragmas.Package != "" { name := api.PkgName(pragmas.Package) pkgs[normalizePackageName(name)] = true } else { // Otherwise, try and look it up in Pypi - pkg, ok := moduleToPypiPackage[modname] + pkg, ok := moduleToPypiPackage()[modname] if ok { name := api.PkgName(pkg) pkgs[normalizePackageName(name)] = true