@@ -747,9 +747,13 @@ type Session struct {
747
747
options * Options
748
748
xctx XContext
749
749
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.
753
757
importPaths map [string ]map [string ]string
754
758
755
759
// 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) {
918
922
var srcs * sources.Sources
919
923
var err error
920
924
if pkg .IsTest {
921
- srcs , err = s .buildTestPackage (pkg )
925
+ srcs , err = s .loadTestPackage (pkg )
922
926
} else {
923
- srcs , err = s .buildPackages (pkg )
927
+ srcs , err = s .loadPackages (pkg )
924
928
}
925
929
if err != nil {
926
930
return nil , err
@@ -935,12 +939,12 @@ func (s *Session) BuildProject(pkg *PackageData) (*compiler.Archive, error) {
935
939
return s .compilePackages (srcs )
936
940
}
937
941
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 ())
940
944
if err != nil {
941
945
return nil , err
942
946
}
943
- _ , err = s .buildPackages (pkg .XTestPackage ())
947
+ _ , err = s .loadPackages (pkg .XTestPackage ())
944
948
if err != nil {
945
949
return nil , err
946
950
}
@@ -964,7 +968,7 @@ func (s *Session) buildTestPackage(pkg *PackageData) (*sources.Sources, error) {
964
968
965
969
// Import dependencies for the testmain package.
966
970
for _ , importedPkgPath := range srcs .Imports () {
967
- _ , _ , err := s .buildImportPathWithSrcDir (importedPkgPath , pkg .Dir )
971
+ _ , _ , err := s .loadImportPathWithSrcDir (importedPkgPath , pkg .Dir )
968
972
if err != nil {
969
973
return nil , err
970
974
}
@@ -973,11 +977,11 @@ func (s *Session) buildTestPackage(pkg *PackageData) (*sources.Sources, error) {
973
977
return srcs , nil
974
978
}
975
979
976
- // buildImportPathWithSrcDir builds the sources for a package specified by the import path.
980
+ // loadImportPathWithSrcDir gets the parsed package specified by the import path.
977
981
//
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 ) {
981
985
pkg , err := s .xctx .Import (path , srcDir , 0 )
982
986
if s .Watcher != nil && pkg != nil { // add watch even on error
983
987
s .Watcher .Add (pkg .Dir )
@@ -986,7 +990,7 @@ func (s *Session) buildImportPathWithSrcDir(path, srcDir string) (*PackageData,
986
990
return nil , nil , err
987
991
}
988
992
989
- srcs , err := s .buildPackages (pkg )
993
+ srcs , err := s .loadPackages (pkg )
990
994
if err != nil {
991
995
return nil , nil , err
992
996
}
@@ -995,8 +999,8 @@ func (s *Session) buildImportPathWithSrcDir(path, srcDir string) (*PackageData,
995
999
return pkg , srcs , nil
996
1000
}
997
1001
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.
1000
1004
// The given path and source directly are the ones passed into
1001
1005
// XContext.Import to the get the build package originally.
1002
1006
func (s * Session ) cacheImportPath (path , srcDir , importPath string ) {
@@ -1033,7 +1037,7 @@ var getExeModTime = func() func() time.Time {
1033
1037
}
1034
1038
}()
1035
1039
1036
- func (s * Session ) buildPackages (pkg * PackageData ) (* sources.Sources , error ) {
1040
+ func (s * Session ) loadPackages (pkg * PackageData ) (* sources.Sources , error ) {
1037
1041
if srcs , ok := s .sources [pkg .ImportPath ]; ok {
1038
1042
return srcs , nil
1039
1043
}
@@ -1046,7 +1050,7 @@ func (s *Session) buildPackages(pkg *PackageData) (*sources.Sources, error) {
1046
1050
if importedPkgPath == "unsafe" {
1047
1051
continue
1048
1052
}
1049
- importedPkg , _ , err := s .buildImportPathWithSrcDir (importedPkgPath , pkg .Dir )
1053
+ importedPkg , _ , err := s .loadImportPathWithSrcDir (importedPkgPath , pkg .Dir )
1050
1054
if err != nil {
1051
1055
return nil , err
1052
1056
}
@@ -1086,7 +1090,7 @@ func (s *Session) buildPackages(pkg *PackageData) (*sources.Sources, error) {
1086
1090
// Import dependencies from the augmented files,
1087
1091
// whilst skipping any that have been already imported.
1088
1092
for _ , importedPkgPath := range srcs .Imports (pkg .Imports ... ) {
1089
- _ , _ , err := s .buildImportPathWithSrcDir (importedPkgPath , pkg .Dir )
1093
+ _ , _ , err := s .loadImportPathWithSrcDir (importedPkgPath , pkg .Dir )
1090
1094
if err != nil {
1091
1095
return nil , err
1092
1096
}
0 commit comments