Skip to content

Commit 5c24bfa

Browse files
committed
auto merge of #11057 : alexcrichton/rust/no-at-in-ebml, r=pcwalton
Now that the metadata is an owned value with a lifetime of a borrowed byte slice, it's possible to have future optimizations where the metadata doesn't need to be copied around (very expensive operation).
2 parents d069d58 + 73fceca commit 5c24bfa

File tree

12 files changed

+182
-156
lines changed

12 files changed

+182
-156
lines changed

src/libextra/ebml.rs

Lines changed: 51 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ use std::str;
1818

1919
// Common data structures
2020
#[deriving(Clone)]
21-
pub struct Doc {
22-
data: @~[u8],
21+
pub struct Doc<'a> {
22+
data: &'a [u8],
2323
start: uint,
2424
end: uint,
2525
}
2626

27-
impl Doc {
28-
pub fn get(&self, tag: uint) -> Doc {
27+
impl<'doc> Doc<'doc> {
28+
pub fn get<'a>(&'a self, tag: uint) -> Doc<'a> {
2929
reader::get_doc(*self, tag)
3030
}
3131

@@ -38,9 +38,9 @@ impl Doc {
3838
}
3939
}
4040

41-
pub struct TaggedDoc {
41+
pub struct TaggedDoc<'a> {
4242
priv tag: uint,
43-
doc: Doc,
43+
doc: Doc<'a>,
4444
}
4545

4646
pub enum EbmlEncoderTag {
@@ -167,25 +167,25 @@ pub mod reader {
167167
vuint_at_slow(data, start)
168168
}
169169

170-
pub fn Doc(data: @~[u8]) -> Doc {
170+
pub fn Doc<'a>(data: &'a [u8]) -> Doc<'a> {
171171
Doc { data: data, start: 0u, end: data.len() }
172172
}
173173

174-
pub fn doc_at(data: @~[u8], start: uint) -> TaggedDoc {
175-
let elt_tag = vuint_at(*data, start);
176-
let elt_size = vuint_at(*data, elt_tag.next);
174+
pub fn doc_at<'a>(data: &'a [u8], start: uint) -> TaggedDoc<'a> {
175+
let elt_tag = vuint_at(data, start);
176+
let elt_size = vuint_at(data, elt_tag.next);
177177
let end = elt_size.next + elt_size.val;
178178
TaggedDoc {
179179
tag: elt_tag.val,
180180
doc: Doc { data: data, start: elt_size.next, end: end }
181181
}
182182
}
183183

184-
pub fn maybe_get_doc(d: Doc, tg: uint) -> Option<Doc> {
184+
pub fn maybe_get_doc<'a>(d: Doc<'a>, tg: uint) -> Option<Doc<'a>> {
185185
let mut pos = d.start;
186186
while pos < d.end {
187-
let elt_tag = vuint_at(*d.data, pos);
188-
let elt_size = vuint_at(*d.data, elt_tag.next);
187+
let elt_tag = vuint_at(d.data, pos);
188+
let elt_size = vuint_at(d.data, elt_tag.next);
189189
pos = elt_size.next + elt_size.val;
190190
if elt_tag.val == tg {
191191
return Some(Doc { data: d.data, start: elt_size.next,
@@ -195,7 +195,7 @@ pub mod reader {
195195
None
196196
}
197197

198-
pub fn get_doc(d: Doc, tg: uint) -> Doc {
198+
pub fn get_doc<'a>(d: Doc<'a>, tg: uint) -> Doc<'a> {
199199
match maybe_get_doc(d, tg) {
200200
Some(d) => d,
201201
None => {
@@ -205,11 +205,11 @@ pub mod reader {
205205
}
206206
}
207207

208-
pub fn docs(d: Doc, it: |uint, Doc| -> bool) -> bool {
208+
pub fn docs<'a>(d: Doc<'a>, it: |uint, Doc<'a>| -> bool) -> bool {
209209
let mut pos = d.start;
210210
while pos < d.end {
211-
let elt_tag = vuint_at(*d.data, pos);
212-
let elt_size = vuint_at(*d.data, elt_tag.next);
211+
let elt_tag = vuint_at(d.data, pos);
212+
let elt_size = vuint_at(d.data, elt_tag.next);
213213
pos = elt_size.next + elt_size.val;
214214
let doc = Doc { data: d.data, start: elt_size.next, end: pos };
215215
if !it(elt_tag.val, doc) {
@@ -219,11 +219,11 @@ pub mod reader {
219219
return true;
220220
}
221221

222-
pub fn tagged_docs(d: Doc, tg: uint, it: |Doc| -> bool) -> bool {
222+
pub fn tagged_docs<'a>(d: Doc<'a>, tg: uint, it: |Doc<'a>| -> bool) -> bool {
223223
let mut pos = d.start;
224224
while pos < d.end {
225-
let elt_tag = vuint_at(*d.data, pos);
226-
let elt_size = vuint_at(*d.data, elt_tag.next);
225+
let elt_tag = vuint_at(d.data, pos);
226+
let elt_size = vuint_at(d.data, elt_tag.next);
227227
pos = elt_size.next + elt_size.val;
228228
if elt_tag.val == tg {
229229
let doc = Doc { data: d.data, start: elt_size.next,
@@ -236,49 +236,49 @@ pub mod reader {
236236
return true;
237237
}
238238

239-
pub fn with_doc_data<T>(d: Doc, f: |x: &[u8]| -> T) -> T {
239+
pub fn with_doc_data<'a, T>(d: Doc<'a>, f: |x: &'a [u8]| -> T) -> T {
240240
f(d.data.slice(d.start, d.end))
241241
}
242242

243243

244244
pub fn doc_as_u8(d: Doc) -> u8 {
245245
assert_eq!(d.end, d.start + 1u);
246-
(*d.data)[d.start]
246+
d.data[d.start]
247247
}
248248

249249
pub fn doc_as_u16(d: Doc) -> u16 {
250250
assert_eq!(d.end, d.start + 2u);
251-
u64_from_be_bytes(*d.data, d.start, 2u) as u16
251+
u64_from_be_bytes(d.data, d.start, 2u) as u16
252252
}
253253

254254
pub fn doc_as_u32(d: Doc) -> u32 {
255255
assert_eq!(d.end, d.start + 4u);
256-
u64_from_be_bytes(*d.data, d.start, 4u) as u32
256+
u64_from_be_bytes(d.data, d.start, 4u) as u32
257257
}
258258

259259
pub fn doc_as_u64(d: Doc) -> u64 {
260260
assert_eq!(d.end, d.start + 8u);
261-
u64_from_be_bytes(*d.data, d.start, 8u)
261+
u64_from_be_bytes(d.data, d.start, 8u)
262262
}
263263

264264
pub fn doc_as_i8(d: Doc) -> i8 { doc_as_u8(d) as i8 }
265265
pub fn doc_as_i16(d: Doc) -> i16 { doc_as_u16(d) as i16 }
266266
pub fn doc_as_i32(d: Doc) -> i32 { doc_as_u32(d) as i32 }
267267
pub fn doc_as_i64(d: Doc) -> i64 { doc_as_u64(d) as i64 }
268268

269-
pub struct Decoder {
270-
priv parent: Doc,
269+
pub struct Decoder<'a> {
270+
priv parent: Doc<'a>,
271271
priv pos: uint,
272272
}
273273

274-
pub fn Decoder(d: Doc) -> Decoder {
274+
pub fn Decoder<'a>(d: Doc<'a>) -> Decoder<'a> {
275275
Decoder {
276276
parent: d,
277277
pos: d.start
278278
}
279279
}
280280

281-
impl Decoder {
281+
impl<'doc> Decoder<'doc> {
282282
fn _check_label(&mut self, lbl: &str) {
283283
if self.pos < self.parent.end {
284284
let TaggedDoc { tag: r_tag, doc: r_doc } =
@@ -294,7 +294,7 @@ pub mod reader {
294294
}
295295
}
296296

297-
fn next_doc(&mut self, exp_tag: EbmlEncoderTag) -> Doc {
297+
fn next_doc(&mut self, exp_tag: EbmlEncoderTag) -> Doc<'doc> {
298298
debug!(". next_doc(exp_tag={:?})", exp_tag);
299299
if self.pos >= self.parent.end {
300300
fail!("no more documents in current node!");
@@ -321,7 +321,7 @@ pub mod reader {
321321
}
322322

323323
fn push_doc<T>(&mut self, exp_tag: EbmlEncoderTag,
324-
f: |&mut Decoder| -> T) -> T {
324+
f: |&mut Decoder<'doc>| -> T) -> T {
325325
let d = self.next_doc(exp_tag);
326326
let old_parent = self.parent;
327327
let old_pos = self.pos;
@@ -338,10 +338,8 @@ pub mod reader {
338338
debug!("_next_uint exp_tag={:?} result={}", exp_tag, r);
339339
r as uint
340340
}
341-
}
342341

343-
impl Decoder {
344-
pub fn read_opaque<R>(&mut self, op: |&mut Decoder, Doc| -> R) -> R {
342+
pub fn read_opaque<R>(&mut self, op: |&mut Decoder<'doc>, Doc| -> R) -> R {
345343
let doc = self.next_doc(EsOpaque);
346344

347345
let (old_parent, old_pos) = (self.parent, self.pos);
@@ -356,7 +354,7 @@ pub mod reader {
356354
}
357355
}
358356

359-
impl serialize::Decoder for Decoder {
357+
impl<'doc> serialize::Decoder for Decoder<'doc> {
360358
fn read_nil(&mut self) -> () { () }
361359

362360
fn read_u64(&mut self) -> u64 { doc_as_u64(self.next_doc(EsU64)) }
@@ -412,7 +410,7 @@ pub mod reader {
412410
}
413411

414412
// Compound types:
415-
fn read_enum<T>(&mut self, name: &str, f: |&mut Decoder| -> T) -> T {
413+
fn read_enum<T>(&mut self, name: &str, f: |&mut Decoder<'doc>| -> T) -> T {
416414
debug!("read_enum({})", name);
417415
self._check_label(name);
418416

@@ -431,7 +429,7 @@ pub mod reader {
431429

432430
fn read_enum_variant<T>(&mut self,
433431
_: &[&str],
434-
f: |&mut Decoder, uint| -> T)
432+
f: |&mut Decoder<'doc>, uint| -> T)
435433
-> T {
436434
debug!("read_enum_variant()");
437435
let idx = self._next_uint(EsEnumVid);
@@ -452,14 +450,14 @@ pub mod reader {
452450

453451
fn read_enum_variant_arg<T>(&mut self,
454452
idx: uint,
455-
f: |&mut Decoder| -> T) -> T {
453+
f: |&mut Decoder<'doc>| -> T) -> T {
456454
debug!("read_enum_variant_arg(idx={})", idx);
457455
f(self)
458456
}
459457

460458
fn read_enum_struct_variant<T>(&mut self,
461459
_: &[&str],
462-
f: |&mut Decoder, uint| -> T)
460+
f: |&mut Decoder<'doc>, uint| -> T)
463461
-> T {
464462
debug!("read_enum_struct_variant()");
465463
let idx = self._next_uint(EsEnumVid);
@@ -481,7 +479,7 @@ pub mod reader {
481479
fn read_enum_struct_variant_field<T>(&mut self,
482480
name: &str,
483481
idx: uint,
484-
f: |&mut Decoder| -> T)
482+
f: |&mut Decoder<'doc>| -> T)
485483
-> T {
486484
debug!("read_enum_struct_variant_arg(name={}, idx={})", name, idx);
487485
f(self)
@@ -490,7 +488,7 @@ pub mod reader {
490488
fn read_struct<T>(&mut self,
491489
name: &str,
492490
_: uint,
493-
f: |&mut Decoder| -> T)
491+
f: |&mut Decoder<'doc>| -> T)
494492
-> T {
495493
debug!("read_struct(name={})", name);
496494
f(self)
@@ -499,41 +497,41 @@ pub mod reader {
499497
fn read_struct_field<T>(&mut self,
500498
name: &str,
501499
idx: uint,
502-
f: |&mut Decoder| -> T)
500+
f: |&mut Decoder<'doc>| -> T)
503501
-> T {
504502
debug!("read_struct_field(name={}, idx={})", name, idx);
505503
self._check_label(name);
506504
f(self)
507505
}
508506

509-
fn read_tuple<T>(&mut self, f: |&mut Decoder, uint| -> T) -> T {
507+
fn read_tuple<T>(&mut self, f: |&mut Decoder<'doc>, uint| -> T) -> T {
510508
debug!("read_tuple()");
511509
self.read_seq(f)
512510
}
513511

514-
fn read_tuple_arg<T>(&mut self, idx: uint, f: |&mut Decoder| -> T)
512+
fn read_tuple_arg<T>(&mut self, idx: uint, f: |&mut Decoder<'doc>| -> T)
515513
-> T {
516514
debug!("read_tuple_arg(idx={})", idx);
517515
self.read_seq_elt(idx, f)
518516
}
519517

520518
fn read_tuple_struct<T>(&mut self,
521519
name: &str,
522-
f: |&mut Decoder, uint| -> T)
520+
f: |&mut Decoder<'doc>, uint| -> T)
523521
-> T {
524522
debug!("read_tuple_struct(name={})", name);
525523
self.read_tuple(f)
526524
}
527525

528526
fn read_tuple_struct_arg<T>(&mut self,
529527
idx: uint,
530-
f: |&mut Decoder| -> T)
528+
f: |&mut Decoder<'doc>| -> T)
531529
-> T {
532530
debug!("read_tuple_struct_arg(idx={})", idx);
533531
self.read_tuple_arg(idx, f)
534532
}
535533

536-
fn read_option<T>(&mut self, f: |&mut Decoder, bool| -> T) -> T {
534+
fn read_option<T>(&mut self, f: |&mut Decoder<'doc>, bool| -> T) -> T {
537535
debug!("read_option()");
538536
self.read_enum("Option", |this| {
539537
this.read_enum_variant(["None", "Some"], |this, idx| {
@@ -546,7 +544,7 @@ pub mod reader {
546544
})
547545
}
548546

549-
fn read_seq<T>(&mut self, f: |&mut Decoder, uint| -> T) -> T {
547+
fn read_seq<T>(&mut self, f: |&mut Decoder<'doc>, uint| -> T) -> T {
550548
debug!("read_seq()");
551549
self.push_doc(EsVec, |d| {
552550
let len = d._next_uint(EsVecLen);
@@ -555,13 +553,13 @@ pub mod reader {
555553
})
556554
}
557555

558-
fn read_seq_elt<T>(&mut self, idx: uint, f: |&mut Decoder| -> T)
556+
fn read_seq_elt<T>(&mut self, idx: uint, f: |&mut Decoder<'doc>| -> T)
559557
-> T {
560558
debug!("read_seq_elt(idx={})", idx);
561559
self.push_doc(EsVecElt, f)
562560
}
563561

564-
fn read_map<T>(&mut self, f: |&mut Decoder, uint| -> T) -> T {
562+
fn read_map<T>(&mut self, f: |&mut Decoder<'doc>, uint| -> T) -> T {
565563
debug!("read_map()");
566564
self.push_doc(EsMap, |d| {
567565
let len = d._next_uint(EsMapLen);
@@ -570,13 +568,13 @@ pub mod reader {
570568
})
571569
}
572570

573-
fn read_map_elt_key<T>(&mut self, idx: uint, f: |&mut Decoder| -> T)
571+
fn read_map_elt_key<T>(&mut self, idx: uint, f: |&mut Decoder<'doc>| -> T)
574572
-> T {
575573
debug!("read_map_elt_key(idx={})", idx);
576574
self.push_doc(EsMapKey, f)
577575
}
578576

579-
fn read_map_elt_val<T>(&mut self, idx: uint, f: |&mut Decoder| -> T)
577+
fn read_map_elt_val<T>(&mut self, idx: uint, f: |&mut Decoder<'doc>| -> T)
580578
-> T {
581579
debug!("read_map_elt_val(idx={})", idx);
582580
self.push_doc(EsMapVal, f)
@@ -953,7 +951,7 @@ mod tests {
953951
let wr = @mut MemWriter::new();
954952
let mut ebml_w = writer::Encoder(wr);
955953
v.encode(&mut ebml_w);
956-
let ebml_doc = reader::Doc(@wr.inner_ref().to_owned());
954+
let ebml_doc = reader::Doc(*wr.inner_ref());
957955
let mut deser = reader::Decoder(ebml_doc);
958956
let v1 = serialize::Decodable::decode(&mut deser);
959957
debug!("v1 == {:?}", v1);

src/libextra/uuid.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -792,7 +792,7 @@ mod test {
792792
let u = Uuid::new_v4();
793793
let wr = @mut MemWriter::new();
794794
u.encode(&mut ebml::writer::Encoder(wr));
795-
let doc = ebml::reader::Doc(@wr.inner_ref().to_owned());
795+
let doc = ebml::reader::Doc(wr.inner_ref().as_slice());
796796
let u2 = Decodable::decode(&mut ebml::reader::Decoder(doc));
797797
assert_eq!(u, u2);
798798
}

src/librustc/metadata/creader.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -267,9 +267,9 @@ fn resolve_crate(e: @mut Env,
267267
dylib, rlib, metadata
268268
} = load_ctxt.load_library_crate();
269269

270-
let attrs = decoder::get_crate_attributes(metadata);
270+
let attrs = decoder::get_crate_attributes(metadata.as_slice());
271271
let pkgid = attr::find_pkgid(attrs).unwrap();
272-
let hash = decoder::get_crate_hash(metadata);
272+
let hash = decoder::get_crate_hash(metadata.as_slice());
273273

274274
// Claim this crate number and cache it
275275
let cnum = e.next_crate_num;
@@ -282,7 +282,7 @@ fn resolve_crate(e: @mut Env,
282282
e.next_crate_num += 1;
283283

284284
// Now resolve the crates referenced by this crate
285-
let cnum_map = resolve_crate_deps(e, metadata);
285+
let cnum_map = resolve_crate_deps(e, metadata.as_slice());
286286

287287
let cmeta = @cstore::crate_metadata {
288288
name: name,
@@ -307,7 +307,7 @@ fn resolve_crate(e: @mut Env,
307307
}
308308

309309
// Go through the crate metadata and load any crates that it references
310-
fn resolve_crate_deps(e: @mut Env, cdata: @~[u8]) -> cstore::cnum_map {
310+
fn resolve_crate_deps(e: @mut Env, cdata: &[u8]) -> cstore::cnum_map {
311311
debug!("resolving deps of external crate");
312312
// The map from crate numbers in the crate we're resolving to local crate
313313
// numbers

0 commit comments

Comments
 (0)