Optimize codegen for SecondaryMap indexing (#2940)

Moves the slow path which resizes the vector out-of-line. The actual
indexing is also done in the out-of-line path which avoids the need for
a second bounds check in the fast path after a potential resize.
This commit is contained in:
Amanieu d'Antras
2021-05-27 17:09:15 +01:00
committed by GitHub
parent 8127346b4d
commit 76664fc73e

View File

@@ -129,6 +129,13 @@ where
pub fn resize(&mut self, n: usize) {
self.elems.resize(n, self.default.clone());
}
/// Slow path for `index_mut` which resizes the vector.
#[cold]
fn resize_for_index_mut(&mut self, i: usize) -> &mut V {
self.elems.resize(i + 1, self.default.clone());
&mut self.elems[i]
}
}
impl<K, V> Default for SecondaryMap<K, V>
@@ -169,7 +176,7 @@ where
fn index_mut(&mut self, k: K) -> &mut V {
let i = k.index();
if i >= self.elems.len() {
self.elems.resize(i + 1, self.default.clone());
return self.resize_for_index_mut(i);
}
&mut self.elems[i]
}