@@ -788,9 +788,13 @@ type Session struct {
788
788
options * Options
789
789
xctx XContext
790
790
791
- // importPaths is a map of the resolved import paths given the srcDir
792
- // and path. This is used to avoid redetermining build packages during
793
- // compilation when we're looking up parsed packages.
791
+ // importPaths is a map of the resolved import paths given the
792
+ // source directory (first key) and the unresolved import path (second key).
793
+ // This is used to cache the resolved import returned from XContext.Import.
794
+ // XContent.Import can be slow, so we cache the resolved path that is used
795
+ // as the map key by parsedPackages and UpToDateArchives.
796
+ // This makes subsequent lookups faster during compilation when all we have
797
+ // is the unresolved import path and source directory.
794
798
importPaths map [string ]map [string ]string
795
799
796
800
// parsePackage is a map of parsed packages that have been built and augmented.
@@ -958,9 +962,9 @@ func (s *Session) BuildProject(pkg *PackageData) (*compiler.Archive, error) {
958
962
var parsed * ParsedPackage
959
963
var err error
960
964
if pkg .IsTest {
961
- parsed , err = s .buildTestPackage (pkg )
965
+ parsed , err = s .loadTestPackage (pkg )
962
966
} else {
963
- parsed , err = s .buildPackages (pkg )
967
+ parsed , err = s .loadPackages (pkg )
964
968
}
965
969
if err != nil {
966
970
return nil , err
@@ -975,12 +979,12 @@ func (s *Session) BuildProject(pkg *PackageData) (*compiler.Archive, error) {
975
979
return s .compilePackages (parsed )
976
980
}
977
981
978
- func (s * Session ) buildTestPackage (pkg * PackageData ) (* ParsedPackage , error ) {
979
- _ , err := s .buildPackages (pkg .TestPackage ())
982
+ func (s * Session ) loadTestPackage (pkg * PackageData ) (* ParsedPackage , error ) {
983
+ _ , err := s .loadPackages (pkg .TestPackage ())
980
984
if err != nil {
981
985
return nil , err
982
986
}
983
- _ , err = s .buildPackages (pkg .XTestPackage ())
987
+ _ , err = s .loadPackages (pkg .XTestPackage ())
984
988
if err != nil {
985
989
return nil , err
986
990
}
@@ -1004,7 +1008,7 @@ func (s *Session) buildTestPackage(pkg *PackageData) (*ParsedPackage, error) {
1004
1008
1005
1009
// Import dependencies for the testmain package.
1006
1010
for _ , importedPkgPath := range parsed .Imports () {
1007
- _ , _ , err := s .buildImportPathWithSrcDir (importedPkgPath , pkg .Dir )
1011
+ _ , _ , err := s .loadImportPathWithSrcDir (importedPkgPath , pkg .Dir )
1008
1012
if err != nil {
1009
1013
return nil , err
1010
1014
}
@@ -1013,11 +1017,11 @@ func (s *Session) buildTestPackage(pkg *PackageData) (*ParsedPackage, error) {
1013
1017
return parsed , nil
1014
1018
}
1015
1019
1016
- // buildImportPathWithSrcDir builds the parsed package specified by the import path.
1020
+ // loadImportPathWithSrcDir gets the parsed package specified by the import path.
1017
1021
//
1018
- // Relative import paths are interpreted relative to the passed srcDir. If
1019
- // srcDir is empty, current working directory is assumed.
1020
- func (s * Session ) buildImportPathWithSrcDir (path , srcDir string ) (* PackageData , * ParsedPackage , error ) {
1022
+ // Relative import paths are interpreted relative to the passed srcDir.
1023
+ // If srcDir is empty, current working directory is assumed.
1024
+ func (s * Session ) loadImportPathWithSrcDir (path , srcDir string ) (* PackageData , * ParsedPackage , error ) {
1021
1025
pkg , err := s .xctx .Import (path , srcDir , 0 )
1022
1026
if s .Watcher != nil && pkg != nil { // add watch even on error
1023
1027
s .Watcher .Add (pkg .Dir )
@@ -1026,7 +1030,7 @@ func (s *Session) buildImportPathWithSrcDir(path, srcDir string) (*PackageData,
1026
1030
return nil , nil , err
1027
1031
}
1028
1032
1029
- parsed , err := s .buildPackages (pkg )
1033
+ parsed , err := s .loadPackages (pkg )
1030
1034
if err != nil {
1031
1035
return nil , nil , err
1032
1036
}
@@ -1035,8 +1039,8 @@ func (s *Session) buildImportPathWithSrcDir(path, srcDir string) (*PackageData,
1035
1039
return pkg , parsed , nil
1036
1040
}
1037
1041
1038
- // cacheImportPath stores the import path for the build package so we can look
1039
- // it up later without getting the whole build package.
1042
+ // cacheImportPath stores the resolved import path for the build package
1043
+ // so we can look it up later without getting the whole build package.
1040
1044
// The given path and source directly are the ones passed into
1041
1045
// XContext.Import to the get the build package originally.
1042
1046
func (s * Session ) cacheImportPath (path , srcDir , importPath string ) {
@@ -1073,7 +1077,7 @@ var getExeModTime = func() func() time.Time {
1073
1077
}
1074
1078
}()
1075
1079
1076
- func (s * Session ) buildPackages (pkg * PackageData ) (* ParsedPackage , error ) {
1080
+ func (s * Session ) loadPackages (pkg * PackageData ) (* ParsedPackage , error ) {
1077
1081
if parsed , ok := s .parsedPackages [pkg .ImportPath ]; ok {
1078
1082
return parsed , nil
1079
1083
}
@@ -1086,7 +1090,7 @@ func (s *Session) buildPackages(pkg *PackageData) (*ParsedPackage, error) {
1086
1090
if importedPkgPath == "unsafe" {
1087
1091
continue
1088
1092
}
1089
- importedPkg , _ , err := s .buildImportPathWithSrcDir (importedPkgPath , pkg .Dir )
1093
+ importedPkg , _ , err := s .loadImportPathWithSrcDir (importedPkgPath , pkg .Dir )
1090
1094
if err != nil {
1091
1095
return nil , err
1092
1096
}
@@ -1126,7 +1130,7 @@ func (s *Session) buildPackages(pkg *PackageData) (*ParsedPackage, error) {
1126
1130
// Import dependencies from the augmented files,
1127
1131
// whilst skipping any that have been already imported.
1128
1132
for _ , importedPkgPath := range parsed .Imports (pkg .Imports ... ) {
1129
- _ , _ , err := s .buildImportPathWithSrcDir (importedPkgPath , pkg .Dir )
1133
+ _ , _ , err := s .loadImportPathWithSrcDir (importedPkgPath , pkg .Dir )
1130
1134
if err != nil {
1131
1135
return nil , err
1132
1136
}
0 commit comments