Skip to content

Commit 1602a46

Browse files
committed
Auto merge of rust-lang#123930 - Mark-Simulacrum:vec-length-invariant, r=jhpratt
Tell LLVM Vec::len is invariant across growth This allows LLVM to avoid re-loading it from memory.
2 parents 3914420 + 755057b commit 1602a46

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

alloc/src/vec/mod.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -1991,15 +1991,17 @@ impl<T, A: Allocator> Vec<T, A> {
19911991
#[stable(feature = "rust1", since = "1.0.0")]
19921992
#[rustc_confusables("push_back", "put", "append")]
19931993
pub fn push(&mut self, value: T) {
1994+
// Inform codegen that the length does not change across grow_one().
1995+
let len = self.len;
19941996
// This will panic or abort if we would allocate > isize::MAX bytes
19951997
// or if the length increment would overflow for zero-sized types.
1996-
if self.len == self.buf.capacity() {
1998+
if len == self.buf.capacity() {
19971999
self.buf.grow_one();
19982000
}
19992001
unsafe {
2000-
let end = self.as_mut_ptr().add(self.len);
2002+
let end = self.as_mut_ptr().add(len);
20012003
ptr::write(end, value);
2002-
self.len += 1;
2004+
self.len = len + 1;
20032005
}
20042006
}
20052007

0 commit comments

Comments
 (0)