Skip to content

Commit

Permalink
Changed Rc::inc_{weak,strong} to better hint optimization to LLVM
Browse files Browse the repository at this point in the history
  • Loading branch information
dshynkev committed Aug 4, 2018
1 parent 3edb355 commit 1553ea2
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/liballoc/rc.rs
Expand Up @@ -1359,7 +1359,14 @@ trait RcBoxPtr<T: ?Sized> {

#[inline]
fn inc_strong(&self) {
self.inner().strong.set(self.strong().checked_add(1).unwrap_or_else(|| unsafe { abort() }));
// We want to abort on overflow instead of dropping the value.
// The reference count will never be zero when this is called;
// nevertheless, we insert an abort here to hint LLVM at
// an otherwise missied optimization.
if self.strong() == 0 || self.strong() == usize::max_value() {
unsafe { abort(); }
}
self.inner().strong.set(self.strong() + 1);
}

#[inline]
Expand All @@ -1374,7 +1381,14 @@ trait RcBoxPtr<T: ?Sized> {

#[inline]
fn inc_weak(&self) {
self.inner().weak.set(self.weak().checked_add(1).unwrap_or_else(|| unsafe { abort() }));
// We want to abort on overflow instead of dropping the value.
// The reference count will never be zero when this is called;
// nevertheless, we insert an abort here to hint LLVM at
// an otherwise missied optimization.
if self.weak() == 0 || self.weak() == usize::max_value() {
unsafe { abort(); }
}
self.inner().weak.set(self.weak() + 1);
}

#[inline]
Expand Down

0 comments on commit 1553ea2

Please sign in to comment.