Skip to content

Commit 5dc03a8

Browse files
committed
Lazy-load filemaps from external crates.
1 parent c3d60ab commit 5dc03a8

File tree

3 files changed

+35
-22
lines changed

3 files changed

+35
-22
lines changed

src/librustc/metadata/creader.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use metadata::decoder;
2121
use metadata::loader;
2222
use metadata::loader::CratePaths;
2323

24+
use std::cell::RefCell;
2425
use std::path::PathBuf;
2526
use std::rc::Rc;
2627
use std::fs;
@@ -376,14 +377,13 @@ impl<'a> CrateReader<'a> {
376377
let loader::Library { dylib, rlib, metadata } = lib;
377378

378379
let cnum_map = self.resolve_crate_deps(root, metadata.as_slice(), span);
379-
let codemap_import_info = import_codemap(self.sess.codemap(), &metadata);
380380

381381
let cmeta = Rc::new( cstore::crate_metadata {
382382
name: name.to_string(),
383383
data: metadata,
384384
cnum_map: cnum_map,
385385
cnum: cnum,
386-
codemap_import_info: codemap_import_info,
386+
codemap_import_info: RefCell::new(vec![]),
387387
span: span,
388388
});
389389

@@ -616,9 +616,9 @@ impl<'a> CrateReader<'a> {
616616
/// file they represent, just information about length, line breaks, and
617617
/// multibyte characters. This information is enough to generate valid debuginfo
618618
/// for items inlined from other crates.
619-
fn import_codemap(local_codemap: &codemap::CodeMap,
620-
metadata: &MetadataBlob)
621-
-> Vec<cstore::ImportedFileMap> {
619+
pub fn import_codemap(local_codemap: &codemap::CodeMap,
620+
metadata: &MetadataBlob)
621+
-> Vec<cstore::ImportedFileMap> {
622622
let external_codemap = decoder::get_imported_filemaps(metadata.as_slice());
623623

624624
let imported_filemaps = external_codemap.into_iter().map(|filemap_to_import| {

src/librustc/metadata/cstore.rs

+17-4
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,11 @@ pub use self::LinkagePreference::*;
1818
pub use self::NativeLibraryKind::*;
1919

2020
use back::svh::Svh;
21-
use metadata::decoder;
22-
use metadata::loader;
21+
use metadata::{creader, decoder, loader};
2322
use session::search_paths::PathKind;
2423
use util::nodemap::{FnvHashMap, NodeMap};
2524

26-
use std::cell::RefCell;
25+
use std::cell::{RefCell, Ref};
2726
use std::rc::Rc;
2827
use std::path::PathBuf;
2928
use flate::Bytes;
@@ -58,7 +57,7 @@ pub struct crate_metadata {
5857
pub data: MetadataBlob,
5958
pub cnum_map: cnum_map,
6059
pub cnum: ast::CrateNum,
61-
pub codemap_import_info: Vec<ImportedFileMap>,
60+
pub codemap_import_info: RefCell<Vec<ImportedFileMap>>,
6261
pub span: codemap::Span,
6362
}
6463

@@ -240,6 +239,20 @@ impl crate_metadata {
240239
pub fn data<'a>(&'a self) -> &'a [u8] { self.data.as_slice() }
241240
pub fn name(&self) -> String { decoder::get_crate_name(self.data()) }
242241
pub fn hash(&self) -> Svh { decoder::get_crate_hash(self.data()) }
242+
pub fn imported_filemaps<'a>(&'a self, codemap: &codemap::CodeMap)
243+
-> Ref<'a, Vec<ImportedFileMap>> {
244+
let filemaps = self.codemap_import_info.borrow();
245+
if filemaps.is_empty() {
246+
drop(filemaps);
247+
let filemaps = creader::import_codemap(codemap, &self.data);
248+
249+
// This shouldn't borrow twice, but there is no way to downgrade RefMut to Ref.
250+
*self.codemap_import_info.borrow_mut() = filemaps;
251+
self.codemap_import_info.borrow()
252+
} else {
253+
filemaps
254+
}
255+
}
243256
}
244257

245258
impl MetadataBlob {

src/librustc/middle/astencode.rs

+13-13
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,6 @@ impl<'a, 'b, 'tcx> DecodeContext<'a, 'b, 'tcx> {
233233
/// codemap as a side-effect of creating the crate_metadata's
234234
/// `codemap_import_info`.
235235
pub fn tr_span(&self, span: Span) -> Span {
236-
let imported_filemaps = &self.cdata.codemap_import_info[..];
237-
238236
let span = if span.lo > span.hi {
239237
// Currently macro expansion sometimes produces invalid Span values
240238
// where lo > hi. In order not to crash the compiler when trying to
@@ -248,16 +246,18 @@ impl<'a, 'b, 'tcx> DecodeContext<'a, 'b, 'tcx> {
248246
span
249247
};
250248

251-
let filemap_index = {
249+
let imported_filemaps = self.cdata.imported_filemaps(self.tcx.sess.codemap());
250+
let filemap = {
252251
// Optimize for the case that most spans within a translated item
253252
// originate from the same filemap.
254253
let last_filemap_index = self.last_filemap_index.get();
254+
let last_filemap = &imported_filemaps[last_filemap_index];
255255

256-
if span.lo >= imported_filemaps[last_filemap_index].original_start_pos &&
257-
span.lo <= imported_filemaps[last_filemap_index].original_end_pos &&
258-
span.hi >= imported_filemaps[last_filemap_index].original_start_pos &&
259-
span.hi <= imported_filemaps[last_filemap_index].original_end_pos {
260-
last_filemap_index
256+
if span.lo >= last_filemap.original_start_pos &&
257+
span.lo <= last_filemap.original_end_pos &&
258+
span.hi >= last_filemap.original_start_pos &&
259+
span.hi <= last_filemap.original_end_pos {
260+
last_filemap
261261
} else {
262262
let mut a = 0;
263263
let mut b = imported_filemaps.len();
@@ -272,14 +272,14 @@ impl<'a, 'b, 'tcx> DecodeContext<'a, 'b, 'tcx> {
272272
}
273273

274274
self.last_filemap_index.set(a);
275-
a
275+
&imported_filemaps[a]
276276
}
277277
};
278278

279-
let lo = (span.lo - imported_filemaps[filemap_index].original_start_pos) +
280-
imported_filemaps[filemap_index].translated_filemap.start_pos;
281-
let hi = (span.hi - imported_filemaps[filemap_index].original_start_pos) +
282-
imported_filemaps[filemap_index].translated_filemap.start_pos;
279+
let lo = (span.lo - filemap.original_start_pos) +
280+
filemap.translated_filemap.start_pos;
281+
let hi = (span.hi - filemap.original_start_pos) +
282+
filemap.translated_filemap.start_pos;
283283

284284
codemap::mk_sp(lo, hi)
285285
}

0 commit comments

Comments
 (0)