From a3387eb6589d5310e1f64cbce5b316999da481ef Mon Sep 17 00:00:00 2001 From: Ariel Ben-Yehuda Date: Tue, 1 Aug 2017 14:17:11 +0300 Subject: [PATCH] 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. --- src/libsyntax/codemap.rs | 5 +++-- src/libsyntax_pos/lib.rs | 5 ++++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/libsyntax/codemap.rs b/src/libsyntax/codemap.rs index b3d9cf9da36c7..bfdcae7641dd5 100644 --- a/src/libsyntax/codemap.rs +++ b/src/libsyntax/codemap.rs @@ -561,8 +561,9 @@ impl CodeMapper for CodeMap { sp } fn ensure_filemap_source_present(&self, file_map: Rc) -> bool { - let src = self.file_loader.read_file(Path::new(&file_map.name)).ok(); - return file_map.add_external_src(src) + file_map.add_external_src( + || self.file_loader.read_file(Path::new(&file_map.name)).ok() + ) } } diff --git a/src/libsyntax_pos/lib.rs b/src/libsyntax_pos/lib.rs index a7c247689cce8..fc9bd7d762805 100644 --- a/src/libsyntax_pos/lib.rs +++ b/src/libsyntax_pos/lib.rs @@ -604,8 +604,11 @@ impl FileMap { /// If the hash of the input doesn't match or no input is supplied via None, /// it is interpreted as an error and the corresponding enum variant is set. /// The return value signifies whether some kind of source is present. - pub fn add_external_src(&self, src: Option) -> bool { + pub fn add_external_src(&self, get_src: F) -> bool + where F: FnOnce() -> Option + { if *self.external_src.borrow() == ExternalSource::AbsentOk { + let src = get_src(); let mut external_src = self.external_src.borrow_mut(); if let Some(src) = src { let mut hasher: StableHasher = StableHasher::new();