Skip to content

Commit 2bb5c74

Browse files
Merge branch 'master' of github.com:gopherjs/gopherjs into sourceInsteadOfParsedPackage
2 parents 82034ad + a3f0ab4 commit 2bb5c74

File tree

1 file changed

+23
-19
lines changed

1 file changed

+23
-19
lines changed

build/build.go

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -747,9 +747,13 @@ type Session struct {
747747
options *Options
748748
xctx XContext
749749

750-
// importPaths is a map of the resolved import paths given the srcDir
751-
// and path. This is used to avoid redetermining build packages during
752-
// compilation when we're looking up parsed packages.
750+
// importPaths is a map of the resolved import paths given the
751+
// source directory (first key) and the unresolved import path (second key).
752+
// This is used to cache the resolved import returned from XContext.Import.
753+
// XContent.Import can be slow, so we cache the resolved path that is used
754+
// as the map key by parsedPackages and UpToDateArchives.
755+
// This makes subsequent lookups faster during compilation when all we have
756+
// is the unresolved import path and source directory.
753757
importPaths map[string]map[string]string
754758

755759
// sources is a map of parsed packages that have been built and augmented.
@@ -918,9 +922,9 @@ func (s *Session) BuildProject(pkg *PackageData) (*compiler.Archive, error) {
918922
var srcs *sources.Sources
919923
var err error
920924
if pkg.IsTest {
921-
srcs, err = s.buildTestPackage(pkg)
925+
srcs, err = s.loadTestPackage(pkg)
922926
} else {
923-
srcs, err = s.buildPackages(pkg)
927+
srcs, err = s.loadPackages(pkg)
924928
}
925929
if err != nil {
926930
return nil, err
@@ -935,12 +939,12 @@ func (s *Session) BuildProject(pkg *PackageData) (*compiler.Archive, error) {
935939
return s.compilePackages(srcs)
936940
}
937941

938-
func (s *Session) buildTestPackage(pkg *PackageData) (*sources.Sources, error) {
939-
_, err := s.buildPackages(pkg.TestPackage())
942+
func (s *Session) loadTestPackage(pkg *PackageData) (*sources.Sources, error) {
943+
_, err := s.loadPackages(pkg.TestPackage())
940944
if err != nil {
941945
return nil, err
942946
}
943-
_, err = s.buildPackages(pkg.XTestPackage())
947+
_, err = s.loadPackages(pkg.XTestPackage())
944948
if err != nil {
945949
return nil, err
946950
}
@@ -964,7 +968,7 @@ func (s *Session) buildTestPackage(pkg *PackageData) (*sources.Sources, error) {
964968

965969
// Import dependencies for the testmain package.
966970
for _, importedPkgPath := range srcs.Imports() {
967-
_, _, err := s.buildImportPathWithSrcDir(importedPkgPath, pkg.Dir)
971+
_, _, err := s.loadImportPathWithSrcDir(importedPkgPath, pkg.Dir)
968972
if err != nil {
969973
return nil, err
970974
}
@@ -973,11 +977,11 @@ func (s *Session) buildTestPackage(pkg *PackageData) (*sources.Sources, error) {
973977
return srcs, nil
974978
}
975979

976-
// buildImportPathWithSrcDir builds the sources for a package specified by the import path.
980+
// loadImportPathWithSrcDir gets the parsed package specified by the import path.
977981
//
978-
// Relative import paths are interpreted relative to the passed srcDir. If
979-
// srcDir is empty, current working directory is assumed.
980-
func (s *Session) buildImportPathWithSrcDir(path, srcDir string) (*PackageData, *sources.Sources, error) {
982+
// Relative import paths are interpreted relative to the passed srcDir.
983+
// If srcDir is empty, current working directory is assumed.
984+
func (s *Session) loadImportPathWithSrcDir(path, srcDir string) (*PackageData, *sources.Sources, error) {
981985
pkg, err := s.xctx.Import(path, srcDir, 0)
982986
if s.Watcher != nil && pkg != nil { // add watch even on error
983987
s.Watcher.Add(pkg.Dir)
@@ -986,7 +990,7 @@ func (s *Session) buildImportPathWithSrcDir(path, srcDir string) (*PackageData,
986990
return nil, nil, err
987991
}
988992

989-
srcs, err := s.buildPackages(pkg)
993+
srcs, err := s.loadPackages(pkg)
990994
if err != nil {
991995
return nil, nil, err
992996
}
@@ -995,8 +999,8 @@ func (s *Session) buildImportPathWithSrcDir(path, srcDir string) (*PackageData,
995999
return pkg, srcs, nil
9961000
}
9971001

998-
// cacheImportPath stores the import path for the build package so we can look
999-
// it up later without getting the whole build package.
1002+
// cacheImportPath stores the resolved import path for the build package
1003+
// so we can look it up later without getting the whole build package.
10001004
// The given path and source directly are the ones passed into
10011005
// XContext.Import to the get the build package originally.
10021006
func (s *Session) cacheImportPath(path, srcDir, importPath string) {
@@ -1033,7 +1037,7 @@ var getExeModTime = func() func() time.Time {
10331037
}
10341038
}()
10351039

1036-
func (s *Session) buildPackages(pkg *PackageData) (*sources.Sources, error) {
1040+
func (s *Session) loadPackages(pkg *PackageData) (*sources.Sources, error) {
10371041
if srcs, ok := s.sources[pkg.ImportPath]; ok {
10381042
return srcs, nil
10391043
}
@@ -1046,7 +1050,7 @@ func (s *Session) buildPackages(pkg *PackageData) (*sources.Sources, error) {
10461050
if importedPkgPath == "unsafe" {
10471051
continue
10481052
}
1049-
importedPkg, _, err := s.buildImportPathWithSrcDir(importedPkgPath, pkg.Dir)
1053+
importedPkg, _, err := s.loadImportPathWithSrcDir(importedPkgPath, pkg.Dir)
10501054
if err != nil {
10511055
return nil, err
10521056
}
@@ -1086,7 +1090,7 @@ func (s *Session) buildPackages(pkg *PackageData) (*sources.Sources, error) {
10861090
// Import dependencies from the augmented files,
10871091
// whilst skipping any that have been already imported.
10881092
for _, importedPkgPath := range srcs.Imports(pkg.Imports...) {
1089-
_, _, err := s.buildImportPathWithSrcDir(importedPkgPath, pkg.Dir)
1093+
_, _, err := s.loadImportPathWithSrcDir(importedPkgPath, pkg.Dir)
10901094
if err != nil {
10911095
return nil, err
10921096
}

0 commit comments

Comments
 (0)