Skip to content

Commit 614e285

Browse files
authored
Rollup merge of rust-lang#46839 - michaelwoerister:faster-span-hashing-2, r=nikomatsakis
incr.comp.: Precompute small hash for filenames to save some work. For each span we hash the filename of the file it points to. Since filenames can be quite long, especially with absolute paths, this PR pre-computes a hash of the filename and we then only hash the hash. r? @nikomatsakis
2 parents dc00aa4 + 0258c6d commit 614e285

File tree

6 files changed

+36
-8
lines changed

6 files changed

+36
-8
lines changed

src/librustc/ich/hcx.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ impl<'gcx> HashStable<StableHashingContext<'gcx>> for Span {
341341
std_hash::Hash::hash(&TAG_VALID_SPAN, hasher);
342342
// We truncate the stable_id hash and line and col numbers. The chances
343343
// of causing a collision this way should be minimal.
344-
std_hash::Hash::hash(&file_lo.name, hasher);
344+
std_hash::Hash::hash(&(file_lo.name_hash as u64), hasher);
345345

346346
let col = (col_lo.0 as u64) & 0xFF;
347347
let line = ((line_lo as u64) & 0xFF_FF_FF) << 8;

src/librustc/ich/impls_syntax.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,8 @@ impl<'gcx> HashStable<StableHashingContext<'gcx>> for FileMap {
387387
hcx: &mut StableHashingContext<'gcx>,
388388
hasher: &mut StableHasher<W>) {
389389
let FileMap {
390-
ref name,
390+
name: _, // We hash the smaller name_hash instead of this
391+
name_hash,
391392
name_was_remapped,
392393
unmapped_path: _,
393394
crate_of_origin,
@@ -402,7 +403,7 @@ impl<'gcx> HashStable<StableHashingContext<'gcx>> for FileMap {
402403
ref non_narrow_chars,
403404
} = *self;
404405

405-
name.hash_stable(hcx, hasher);
406+
(name_hash as u64).hash_stable(hcx, hasher);
406407
name_was_remapped.hash_stable(hcx, hasher);
407408

408409
DefId {

src/librustc_metadata/decoder.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1129,6 +1129,7 @@ impl<'a, 'tcx> CrateMetadata {
11291129
lines,
11301130
multibyte_chars,
11311131
non_narrow_chars,
1132+
name_hash,
11321133
.. } = filemap_to_import;
11331134

11341135
let source_length = (end_pos - start_pos).to_usize();
@@ -1155,6 +1156,7 @@ impl<'a, 'tcx> CrateMetadata {
11551156
name_was_remapped,
11561157
self.cnum.as_u32(),
11571158
src_hash,
1159+
name_hash,
11581160
source_length,
11591161
lines,
11601162
multibyte_chars,

src/librustc_metadata/encoder.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@ use rustc::ty::codec::{self as ty_codec, TyEncoder};
2828
use rustc::session::config::{self, CrateTypeProcMacro};
2929
use rustc::util::nodemap::{FxHashMap, NodeSet};
3030

31+
use rustc_data_structures::stable_hasher::StableHasher;
3132
use rustc_serialize::{Encodable, Encoder, SpecializedEncoder, opaque};
3233

34+
use std::hash::Hash;
3335
use std::io::prelude::*;
3436
use std::io::Cursor;
3537
use std::path::Path;
@@ -290,6 +292,11 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
290292
} else {
291293
let mut adapted = (**filemap).clone();
292294
adapted.name = Path::new(&working_dir).join(name).into();
295+
adapted.name_hash = {
296+
let mut hasher: StableHasher<u128> = StableHasher::new();
297+
adapted.name.hash(&mut hasher);
298+
hasher.finish()
299+
};
293300
Rc::new(adapted)
294301
}
295302
},

src/libsyntax/codemap.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ impl CodeMap {
246246
name_was_remapped: bool,
247247
crate_of_origin: u32,
248248
src_hash: u128,
249+
name_hash: u128,
249250
source_len: usize,
250251
mut file_local_lines: Vec<BytePos>,
251252
mut file_local_multibyte_chars: Vec<MultiByteChar>,
@@ -282,6 +283,7 @@ impl CodeMap {
282283
lines: RefCell::new(file_local_lines),
283284
multibyte_chars: RefCell::new(file_local_multibyte_chars),
284285
non_narrow_chars: RefCell::new(file_local_non_narrow_chars),
286+
name_hash,
285287
});
286288

287289
files.push(filemap.clone());

src/libsyntax_pos/lib.rs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use std::borrow::Cow;
3030
use std::cell::{Cell, RefCell};
3131
use std::cmp::{self, Ordering};
3232
use std::fmt;
33-
use std::hash::Hasher;
33+
use std::hash::{Hasher, Hash};
3434
use std::ops::{Add, Sub};
3535
use std::path::PathBuf;
3636
use std::rc::Rc;
@@ -691,6 +691,8 @@ pub struct FileMap {
691691
pub multibyte_chars: RefCell<Vec<MultiByteChar>>,
692692
/// Width of characters that are not narrow in the source code
693693
pub non_narrow_chars: RefCell<Vec<NonNarrowChar>>,
694+
/// A hash of the filename, used for speeding up the incr. comp. hashing.
695+
pub name_hash: u128,
694696
}
695697

696698
impl Encodable for FileMap {
@@ -752,6 +754,9 @@ impl Encodable for FileMap {
752754
})?;
753755
s.emit_struct_field("non_narrow_chars", 8, |s| {
754756
(*self.non_narrow_chars.borrow()).encode(s)
757+
})?;
758+
s.emit_struct_field("name_hash", 9, |s| {
759+
self.name_hash.encode(s)
755760
})
756761
})
757762
}
@@ -801,6 +806,8 @@ impl Decodable for FileMap {
801806
d.read_struct_field("multibyte_chars", 7, |d| Decodable::decode(d))?;
802807
let non_narrow_chars: Vec<NonNarrowChar> =
803808
d.read_struct_field("non_narrow_chars", 8, |d| Decodable::decode(d))?;
809+
let name_hash: u128 =
810+
d.read_struct_field("name_hash", 9, |d| Decodable::decode(d))?;
804811
Ok(FileMap {
805812
name,
806813
name_was_remapped,
@@ -816,7 +823,8 @@ impl Decodable for FileMap {
816823
external_src: RefCell::new(ExternalSource::AbsentOk),
817824
lines: RefCell::new(lines),
818825
multibyte_chars: RefCell::new(multibyte_chars),
819-
non_narrow_chars: RefCell::new(non_narrow_chars)
826+
non_narrow_chars: RefCell::new(non_narrow_chars),
827+
name_hash,
820828
})
821829
})
822830
}
@@ -836,9 +844,16 @@ impl FileMap {
836844
start_pos: BytePos) -> FileMap {
837845
remove_bom(&mut src);
838846

839-
let mut hasher: StableHasher<u128> = StableHasher::new();
840-
hasher.write(src.as_bytes());
841-
let src_hash = hasher.finish();
847+
let src_hash = {
848+
let mut hasher: StableHasher<u128> = StableHasher::new();
849+
hasher.write(src.as_bytes());
850+
hasher.finish()
851+
};
852+
let name_hash = {
853+
let mut hasher: StableHasher<u128> = StableHasher::new();
854+
name.hash(&mut hasher);
855+
hasher.finish()
856+
};
842857
let end_pos = start_pos.to_usize() + src.len();
843858

844859
FileMap {
@@ -854,6 +869,7 @@ impl FileMap {
854869
lines: RefCell::new(Vec::new()),
855870
multibyte_chars: RefCell::new(Vec::new()),
856871
non_narrow_chars: RefCell::new(Vec::new()),
872+
name_hash,
857873
}
858874
}
859875

0 commit comments

Comments
 (0)