Skip to content

Commit aff24e1

Browse files
committed
rustc: Refactor Shv to use a canonical u64 internally
I have vague concerns about how this allocates on attempts to look at it's representation as a str, however it doesn't appear that this is a common case in tight loops.
1 parent d7eb7a6 commit aff24e1

File tree

5 files changed

+33
-23
lines changed

5 files changed

+33
-23
lines changed

src/librustc/metadata/encoder.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1998,12 +1998,12 @@ fn encode_crate_dep(rbml_w: &mut Encoder,
19981998
dep: decoder::CrateDep) {
19991999
rbml_w.start_tag(tag_crate_dep);
20002000
rbml_w.wr_tagged_str(tag_crate_dep_crate_name, &dep.name);
2001-
rbml_w.wr_tagged_str(tag_crate_dep_hash, dep.hash.as_str());
2001+
rbml_w.wr_tagged_str(tag_crate_dep_hash, &dep.hash.to_string());
20022002
rbml_w.end_tag();
20032003
}
20042004

20052005
fn encode_hash(rbml_w: &mut Encoder, hash: &Svh) {
2006-
rbml_w.wr_tagged_str(tag_crate_hash, hash.as_str());
2006+
rbml_w.wr_tagged_str(tag_crate_hash, &hash.to_string());
20072007
}
20082008

20092009
fn encode_crate_name(rbml_w: &mut Encoder, crate_name: &str) {

src/librustc/metadata/loader.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ impl<'a> Context<'a> {
601601
info!("Rejecting via hash: expected {} got {}", *myhash, hash);
602602
self.rejected_via_hash.push(CrateMismatch {
603603
path: libpath.to_path_buf(),
604-
got: myhash.as_str().to_string()
604+
got: myhash.to_string()
605605
});
606606
false
607607
} else {

src/librustc_back/svh.rs

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -51,19 +51,39 @@ use std::hash::{Hash, SipHasher, Hasher};
5151
use syntax::ast;
5252
use syntax::visit;
5353

54-
#[derive(Clone, PartialEq, Debug)]
54+
fn hex(b: u64) -> char {
55+
let b = (b & 0xf) as u8;
56+
let b = match b {
57+
0 ... 9 => '0' as u8 + b,
58+
_ => 'a' as u8 + b - 10,
59+
};
60+
b as char
61+
}
62+
63+
#[derive(Clone, PartialEq, Eq, Debug)]
5564
pub struct Svh {
56-
hash: String,
65+
raw: u64,
5766
}
5867

5968
impl Svh {
6069
pub fn new(hash: &str) -> Svh {
6170
assert!(hash.len() == 16);
62-
Svh { hash: hash.to_string() }
71+
// Ideally we'd just reverse the nibbles on LE machines during as_string, unfortunately
72+
// this would break the abi so I guess we're just doing this now.
73+
74+
let s = if cfg!(target_endian = "big") {
75+
hash.to_string()
76+
} else {
77+
hash.chars().rev().collect()
78+
};
79+
80+
Svh {
81+
raw: u64::from_str_radix(&s, 16).unwrap(),
82+
}
6383
}
6484

65-
pub fn as_str<'a>(&'a self) -> &'a str {
66-
&self.hash
85+
pub fn as_string(&self) -> String {
86+
(0..64).step_by(4).map(|i| hex(self.raw >> i)).collect()
6787
}
6888

6989
pub fn calculate(metadata: &Vec<String>, krate: &ast::Crate) -> Svh {
@@ -100,25 +120,15 @@ impl Svh {
100120
attr.node.value.hash(&mut state);
101121
}
102122

103-
let hash = state.finish();
104-
return Svh {
105-
hash: (0..64).step_by(4).map(|i| hex(hash >> i)).collect()
106-
};
107-
108-
fn hex(b: u64) -> char {
109-
let b = (b & 0xf) as u8;
110-
let b = match b {
111-
0 ... 9 => '0' as u8 + b,
112-
_ => 'a' as u8 + b - 10,
113-
};
114-
b as char
123+
Svh {
124+
raw: state.finish(),
115125
}
116126
}
117127
}
118128

119129
impl fmt::Display for Svh {
120130
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
121-
f.pad(self.as_str())
131+
f.pad(&self.as_string())
122132
}
123133
}
124134

src/librustc_trans/back/link.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ fn symbol_hash<'tcx>(tcx: &ty::ctxt<'tcx>,
204204
symbol_hasher.reset();
205205
symbol_hasher.input_str(&link_meta.crate_name);
206206
symbol_hasher.input_str("-");
207-
symbol_hasher.input_str(link_meta.crate_hash.as_str());
207+
symbol_hasher.input_str(&link_meta.crate_hash.to_string());
208208
for meta in tcx.sess.crate_metadata.borrow().iter() {
209209
symbol_hasher.input_str(&meta[..]);
210210
}

src/librustc_trans/trans/debuginfo/metadata.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ impl<'tcx> TypeMap<'tcx> {
336336
cx.sess().cstore.get_crate_hash(source_def_id.krate)
337337
};
338338

339-
output.push_str(crate_hash.as_str());
339+
output.push_str(&crate_hash.to_string());
340340
output.push_str("/");
341341
output.push_str(&format!("{:x}", def_id.node));
342342

0 commit comments

Comments
 (0)