Skip to content

Commit b742199

Browse files
Ariel Ben-Yehudaarielb1
authored andcommitted
improve the tyencode abbrev format
3% win on libcore 528828 liballoc-bb943c5a.rlib 1425126 liballoc_jemalloc-bb943c5a.rlib 10090 liballoc_system-bb943c5a.rlib 144904 libarena-bb943c5a.rlib 3773896 libcollections-bb943c5a.rlib 17075242 libcore-bb943c5a.rlib 195770 libflate-bb943c5a.rlib 234702 libfmt_macros-bb943c5a.rlib 536342 libgetopts-bb943c5a.rlib 212028 libgraphviz-bb943c5a.rlib 397068 liblibc-bb943c5a.rlib 185038 liblog-bb943c5a.rlib 680782 librand-bb943c5a.rlib 577574 librbml-bb943c5a.rlib 1381992 librustc_back-bb943c5a.rlib 37554736 librustc-bb943c5a.rlib 12826 librustc_bitflags-bb943c5a.rlib 2257392 librustc_borrowck-bb943c5a.rlib 533858 librustc_data_structures-bb943c5a.rlib 9338878 librustc_driver-bb943c5a.rlib 8960016 librustc_front-bb943c5a.rlib 1594212 librustc_lint-bb943c5a.rlib 79159342 librustc_llvm-bb943c5a.rlib 4590656 librustc_mir-bb943c5a.rlib 3529292 librustc_platform_intrinsics-bb943c5a.rlib 590688 librustc_privacy-bb943c5a.rlib 3084134 librustc_resolve-bb943c5a.rlib 14032890 librustc_trans-bb943c5a.rlib 11833852 librustc_typeck-bb943c5a.rlib 1641496 librustc_unicode-bb943c5a.rlib 15611582 librustdoc-bb943c5a.rlib 2693764 libserialize-bb943c5a.rlib 8266920 libstd-bb943c5a.rlib 29573790 libsyntax-bb943c5a.rlib 895484 libterm-bb943c5a.rlib
1 parent ce02aa4 commit b742199

File tree

6 files changed

+25
-39
lines changed

6 files changed

+25
-39
lines changed

src/librbml/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -914,7 +914,7 @@ pub mod writer {
914914
}
915915
}
916916

917-
fn write_vuint<W: Write>(w: &mut W, n: usize) -> EncodeResult {
917+
pub fn write_vuint<W: Write>(w: &mut W, n: usize) -> EncodeResult {
918918
if n < 0x7f { return write_sized_vuint(w, n, 1); }
919919
if n < 0x4000 { return write_sized_vuint(w, n, 2); }
920920
if n < 0x200000 { return write_sized_vuint(w, n, 3); }

src/librustc/metadata/encoder.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2150,13 +2150,13 @@ fn encode_metadata_inner(wr: &mut Cursor<Vec<u8>>,
21502150
}
21512151

21522152
// Get the encoded string for a type
2153-
pub fn encoded_ty<'tcx>(tcx: &ty::ctxt<'tcx>, t: Ty<'tcx>) -> String {
2153+
pub fn encoded_ty<'tcx>(tcx: &ty::ctxt<'tcx>, t: Ty<'tcx>) -> Vec<u8> {
21542154
let mut wr = Cursor::new(Vec::new());
21552155
tyencode::enc_ty(&mut Encoder::new(&mut wr), &tyencode::ctxt {
21562156
diag: tcx.sess.diagnostic(),
21572157
ds: def_to_string,
21582158
tcx: tcx,
21592159
abbrevs: &RefCell::new(FnvHashMap())
21602160
}, t);
2161-
String::from_utf8(wr.into_inner()).unwrap()
2161+
wr.into_inner()
21622162
}

src/librustc/metadata/tydecode.rs

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,12 @@ impl<'a,'tcx> TyDecoder<'a,'tcx> {
125125
return &self.data[start_pos..end_pos];
126126
}
127127

128+
fn parse_vuint(&mut self) -> usize {
129+
let res = rbml::reader::vuint_at(self.data, self.pos).unwrap();
130+
self.pos = res.next;
131+
res.val
132+
}
133+
128134
fn parse_name(&mut self, last: char) -> ast::Name {
129135
fn is_last(b: char, c: char) -> bool { return c == b; }
130136
let bytes = self.scan(|a| is_last(last, a));
@@ -405,11 +411,8 @@ impl<'a,'tcx> TyDecoder<'a,'tcx> {
405411
// we return it (modulo closure types, see below). But if not, then we
406412
// jump to offset 123 and read the type from there.
407413

408-
let pos = self.parse_hex();
409-
assert_eq!(self.next(), ':');
410-
let len = self.parse_hex();
411-
assert_eq!(self.next(), '#');
412-
let key = ty::CReaderCacheKey {cnum: self.krate, pos: pos, len: len };
414+
let pos = self.parse_vuint();
415+
let key = ty::CReaderCacheKey { cnum: self.krate, pos: pos };
413416
match tcx.rcache.borrow().get(&key).cloned() {
414417
Some(tt) => {
415418
// If there is a closure buried in the type some where, then we
@@ -508,19 +511,6 @@ impl<'a,'tcx> TyDecoder<'a,'tcx> {
508511
subst::ParamSpace::from_uint(self.parse_uint())
509512
}
510513

511-
fn parse_hex(&mut self) -> usize {
512-
let mut n = 0;
513-
loop {
514-
let cur = self.peek();
515-
if (cur < '0' || cur > '9') && (cur < 'a' || cur > 'f') { return n; }
516-
self.pos = self.pos + 1;
517-
n *= 16;
518-
if '0' <= cur && cur <= '9' {
519-
n += (cur as usize) - ('0' as usize);
520-
} else { n += 10 + (cur as usize) - ('a' as usize); }
521-
};
522-
}
523-
524514
fn parse_abi_set(&mut self) -> abi::Abi {
525515
assert_eq!(self.next(), '[');
526516
let bytes = self.scan(|c| c == ']');

src/librustc/metadata/tyencode.rs

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#![allow(non_camel_case_types)]
1515

1616
use std::cell::RefCell;
17-
use std::str;
17+
use std::io::Cursor;
1818
use std::io::prelude::*;
1919

2020
use middle::def_id::DefId;
@@ -31,7 +31,7 @@ use syntax::abi::Abi;
3131
use syntax::ast;
3232
use syntax::diagnostic::SpanHandler;
3333

34-
use rbml::writer::Encoder;
34+
use rbml::writer::{self, Encoder};
3535

3636
macro_rules! mywrite { ($w:expr, $($arg:tt)*) => ({ write!($w.writer, $($arg)*); }) }
3737

@@ -48,14 +48,14 @@ pub struct ctxt<'a, 'tcx: 'a> {
4848
// Extra parameters are for converting to/from def_ids in the string rep.
4949
// Whatever format you choose should not contain pipe characters.
5050
pub struct ty_abbrev {
51-
s: String
51+
s: Vec<u8>
5252
}
5353

5454
pub type abbrev_map<'tcx> = RefCell<FnvHashMap<Ty<'tcx>, ty_abbrev>>;
5555

5656
pub fn enc_ty<'a, 'tcx>(w: &mut Encoder, cx: &ctxt<'a, 'tcx>, t: Ty<'tcx>) {
5757
match cx.abbrevs.borrow_mut().get(&t) {
58-
Some(a) => { w.writer.write_all(a.s.as_bytes()); return; }
58+
Some(a) => { w.writer.write_all(&a.s); return; }
5959
None => {}
6060
}
6161

@@ -167,23 +167,20 @@ pub fn enc_ty<'a, 'tcx>(w: &mut Encoder, cx: &ctxt<'a, 'tcx>, t: Ty<'tcx>) {
167167

168168
let end = w.mark_stable_position();
169169
let len = end - pos;
170-
fn estimate_sz(u: u64) -> u64 {
171-
let mut n = u;
172-
let mut len = 0;
173-
while n != 0 { len += 1; n = n >> 4; }
174-
return len;
175-
}
176-
let abbrev_len = 3 + estimate_sz(pos) + estimate_sz(len);
170+
171+
let buf: &mut [u8] = &mut [0; 16]; // vuint < 15 bytes
172+
let mut abbrev = Cursor::new(buf);
173+
abbrev.write_all(b"#");
174+
writer::write_vuint(&mut abbrev, pos as usize);
175+
177176
cx.abbrevs.borrow_mut().insert(t, ty_abbrev {
178-
s: if abbrev_len < len {
179-
format!("#{:x}:{:x}#", pos, len)
177+
s: if abbrev.position() < len {
178+
abbrev.get_ref()[..abbrev.position() as usize].to_owned()
180179
} else {
181180
// if the abbreviation is longer than the real type,
182181
// don't use #-notation. However, insert it here so
183182
// other won't have to `mark_stable_position`
184-
str::from_utf8(
185-
&w.writer.get_ref()[pos as usize..end as usize]
186-
).unwrap().to_owned()
183+
w.writer.get_ref()[pos as usize..end as usize].to_owned()
187184
}
188185
});
189186
}

src/librustc/middle/ty/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,6 @@ pub type MethodMap<'tcx> = FnvHashMap<MethodCall, MethodCallee<'tcx>>;
366366
pub struct CReaderCacheKey {
367367
pub cnum: CrateNum,
368368
pub pos: usize,
369-
pub len: usize
370369
}
371370

372371
/// A restriction that certain types must be the same size. The use of

src/librustc_trans/back/link.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ fn symbol_hash<'tcx>(tcx: &ty::ctxt<'tcx>,
214214
symbol_hasher.input_str(&meta[..]);
215215
}
216216
symbol_hasher.input_str("-");
217-
symbol_hasher.input_str(&encoder::encoded_ty(tcx, t));
217+
symbol_hasher.input(&encoder::encoded_ty(tcx, t));
218218
// Prefix with 'h' so that it never blends into adjacent digits
219219
let mut hash = String::from("h");
220220
hash.push_str(&truncated_hash_result(symbol_hasher));

0 commit comments

Comments
 (0)