Skip to content

Commit 2fe010b

Browse files
committed
Preallocate the vector containing predicates in decode_predicates
1 parent 0db7abe commit 2fe010b

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

src/librustc/ty/codec.rs

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -175,22 +175,26 @@ pub fn decode_predicates<'a, 'tcx, D>(decoder: &mut D)
175175
where D: TyDecoder<'a, 'tcx>,
176176
'tcx: 'a,
177177
{
178+
let parent = Decodable::decode(decoder)?;
179+
let decoder_len = decoder.read_usize()?;
180+
let mut predicates = Vec::with_capacity(decoder_len);
181+
for _ in 0..decoder_len {
182+
// Handle shorthands first, if we have an usize > 0x80.
183+
let predicate = if decoder.positioned_at_shorthand() {
184+
let pos = decoder.read_usize()?;
185+
assert!(pos >= SHORTHAND_OFFSET);
186+
let shorthand = pos - SHORTHAND_OFFSET;
187+
188+
decoder.with_position(shorthand, ty::Predicate::decode)
189+
} else {
190+
ty::Predicate::decode(decoder)
191+
}?;
192+
predicates.push((predicate, Decodable::decode(decoder)?))
193+
}
194+
178195
Ok(ty::GenericPredicates {
179-
parent: Decodable::decode(decoder)?,
180-
predicates: (0..decoder.read_usize()?).map(|_| {
181-
// Handle shorthands first, if we have an usize > 0x80.
182-
let predicate = if decoder.positioned_at_shorthand() {
183-
let pos = decoder.read_usize()?;
184-
assert!(pos >= SHORTHAND_OFFSET);
185-
let shorthand = pos - SHORTHAND_OFFSET;
186-
187-
decoder.with_position(shorthand, ty::Predicate::decode)
188-
} else {
189-
ty::Predicate::decode(decoder)
190-
}?;
191-
Ok((predicate, Decodable::decode(decoder)?))
192-
})
193-
.collect::<Result<Vec<_>, _>>()?,
196+
parent,
197+
predicates
194198
})
195199
}
196200

0 commit comments

Comments
 (0)