@@ -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,14 @@ 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+ #[ allow( clippy:: type_complexity) ]
63+ cache : RefCell < HashMap < ( PathBuf , Option < CanonPath > ) , Option < CanonPath > > > ,
6164}
6265
6366impl From < Option < PathBuf > > for PathResolver {
@@ -69,6 +72,7 @@ impl From<Option<PathBuf>> for PathResolver {
6972 PathResolver {
7073 roots,
7174 include_paths : HashMap :: default ( ) ,
75+ cache : RefCell :: default ( ) ,
7276 }
7377 }
7478}
@@ -117,6 +121,16 @@ impl PathResolver {
117121 path : & Path ,
118122 context : Option < & CanonPath > )
119123 -> Option < CanonPath > {
124+ self . cache . borrow_mut ( ) . entry ( ( path. to_path_buf ( ) , context. cloned ( ) ) )
125+ . or_insert_with (
126+ ||self . resolve_with_maybe_context_impl ( path, context) )
127+ . clone ( )
128+ }
129+
130+ fn resolve_with_maybe_context_impl ( & self ,
131+ path : & Path ,
132+ context : Option < & CanonPath > )
133+ -> Option < CanonPath > {
120134 // Given some relative info, find a canonical file path
121135 // NOTE: Right now the relative info is a pathbuf, but this might
122136 // change later
0 commit comments