]> git.lizzy.rs Git - rust.git/commitdiff
Right-size the `VecDeque` in `coerce_unsized`.
authorNicholas Nethercote <nnethercote@mozilla.com>
Tue, 22 May 2018 06:51:28 +0000 (16:51 +1000)
committerNicholas Nethercote <nnethercote@mozilla.com>
Tue, 22 May 2018 09:55:48 +0000 (19:55 +1000)
The default capacity of a VecDeque is 8, which is excessive here. In a
"base incremental" check build of rustc-perf's tuple-stress benchmark,
this decreases total heap allocation by 26%. I couldn't see a clear
speedup, but it can't hurt.

src/librustc_typeck/check/coercion.rs

index 324af7b62704dfabd3c9802ea7b353855dd579b5..1905ffef8db184767ee3f2749422b6a501064bc2 100644 (file)
@@ -537,8 +537,9 @@ fn coerce_unsized(&self, source: Ty<'tcx>, target: Ty<'tcx>) -> CoerceResult<'tc
 
         let mut selcx = traits::SelectionContext::new(self);
 
-        // Use a FIFO queue for this custom fulfillment procedure.
-        let mut queue = VecDeque::new();
+        // Use a FIFO queue for this custom fulfillment procedure. (The maximum
+        // length is almost always 1.)
+        let mut queue = VecDeque::with_capacity(1);
 
         // Create an obligation for `Source: CoerceUnsized<Target>`.
         let cause = ObligationCause::misc(self.cause.span, self.body_id);