From 2fe010bb03c3dee255667577dbdcc80092fe65f5 Mon Sep 17 00:00:00 2001 From: ljedrz Date: Wed, 31 Oct 2018 13:24:48 +0100 Subject: [PATCH] Preallocate the vector containing predicates in decode_predicates --- src/librustc/ty/codec.rs | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/src/librustc/ty/codec.rs b/src/librustc/ty/codec.rs index 8738f57414823..52c212b30a50b 100644 --- a/src/librustc/ty/codec.rs +++ b/src/librustc/ty/codec.rs @@ -175,22 +175,26 @@ pub fn decode_predicates<'a, 'tcx, D>(decoder: &mut D) where D: TyDecoder<'a, 'tcx>, 'tcx: 'a, { + let parent = Decodable::decode(decoder)?; + let decoder_len = decoder.read_usize()?; + let mut predicates = Vec::with_capacity(decoder_len); + for _ in 0..decoder_len { + // Handle shorthands first, if we have an usize > 0x80. + let predicate = if decoder.positioned_at_shorthand() { + let pos = decoder.read_usize()?; + assert!(pos >= SHORTHAND_OFFSET); + let shorthand = pos - SHORTHAND_OFFSET; + + decoder.with_position(shorthand, ty::Predicate::decode) + } else { + ty::Predicate::decode(decoder) + }?; + predicates.push((predicate, Decodable::decode(decoder)?)) + } + Ok(ty::GenericPredicates { - parent: Decodable::decode(decoder)?, - predicates: (0..decoder.read_usize()?).map(|_| { - // Handle shorthands first, if we have an usize > 0x80. - let predicate = if decoder.positioned_at_shorthand() { - let pos = decoder.read_usize()?; - assert!(pos >= SHORTHAND_OFFSET); - let shorthand = pos - SHORTHAND_OFFSET; - - decoder.with_position(shorthand, ty::Predicate::decode) - } else { - ty::Predicate::decode(decoder) - }?; - Ok((predicate, Decodable::decode(decoder)?)) - }) - .collect::, _>>()?, + parent, + predicates }) }