Skip to content

Commit c9d14a8

Browse files
committed
syntax: avoid loading the same source-file multiple times
We already had a cache for file contents, but we read the source-file before testing the cache, causing obvious slowness, so this just avoids loading the source-file when the cache already has the contents.
1 parent 91aff57 commit c9d14a8

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

src/libsyntax/codemap.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -561,8 +561,9 @@ impl CodeMapper for CodeMap {
561561
sp
562562
}
563563
fn ensure_filemap_source_present(&self, file_map: Rc<FileMap>) -> bool {
564-
let src = self.file_loader.read_file(Path::new(&file_map.name)).ok();
565-
return file_map.add_external_src(src)
564+
file_map.add_external_src(
565+
|| self.file_loader.read_file(Path::new(&file_map.name)).ok()
566+
)
566567
}
567568
}
568569

src/libsyntax_pos/lib.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -618,8 +618,11 @@ impl FileMap {
618618
/// If the hash of the input doesn't match or no input is supplied via None,
619619
/// it is interpreted as an error and the corresponding enum variant is set.
620620
/// The return value signifies whether some kind of source is present.
621-
pub fn add_external_src(&self, src: Option<String>) -> bool {
621+
pub fn add_external_src<F>(&self, get_src: F) -> bool
622+
where F: FnOnce() -> Option<String>
623+
{
622624
if *self.external_src.borrow() == ExternalSource::AbsentOk {
625+
let src = get_src();
623626
let mut external_src = self.external_src.borrow_mut();
624627
if let Some(src) = src {
625628
let mut hasher: StableHasher<u128> = StableHasher::new();

0 commit comments

Comments
 (0)