@@ -8,6 +8,7 @@ use std::fs;
88
99use std:: path:: { Path , PathBuf } ;
1010use std:: ops:: Deref ;
11+ use std:: cell:: RefCell ;
1112
1213// A path which we know to be canonical in the filesystem
1314#[ derive( Debug , Clone , Hash , Eq , PartialEq , Ord , PartialOrd ) ]
@@ -52,12 +53,13 @@ impl CanonPath {
5253}
5354
5455/// This is how we resolve relative paths to in-workspace full paths
55- #[ derive( Eq , PartialEq , Debug , Clone ) ]
56+ #[ derive( Debug , Clone ) ]
5657pub struct PathResolver {
5758 // Root is provided by context, who will pass this struct to threads for
5859 // import resolution
5960 roots : Vec < PathBuf > ,
6061 include_paths : HashMap < CanonPath , Vec < PathBuf > > ,
62+ cache : RefCell < HashMap < ( PathBuf , Option < CanonPath > ) , Option < CanonPath > > > ,
6163}
6264
6365impl From < Option < PathBuf > > for PathResolver {
@@ -69,6 +71,7 @@ impl From<Option<PathBuf>> for PathResolver {
6971 PathResolver {
7072 roots,
7173 include_paths : HashMap :: default ( ) ,
74+ cache : RefCell :: default ( ) ,
7275 }
7376 }
7477}
@@ -117,6 +120,16 @@ impl PathResolver {
117120 path : & Path ,
118121 context : Option < & CanonPath > )
119122 -> Option < CanonPath > {
123+ self . cache . borrow_mut ( ) . entry ( ( path. to_path_buf ( ) , context. cloned ( ) ) )
124+ . or_insert_with (
125+ ||self . resolve_with_maybe_context_impl ( path, context) )
126+ . clone ( )
127+ }
128+
129+ fn resolve_with_maybe_context_impl ( & self ,
130+ path : & Path ,
131+ context : Option < & CanonPath > )
132+ -> Option < CanonPath > {
120133 // Given some relative info, find a canonical file path
121134 // NOTE: Right now the relative info is a pathbuf, but this might
122135 // change later
0 commit comments