Skip to content

Commit 1553ea2

Browse files
committed
Changed Rc::inc_{weak,strong} to better hint optimization to LLVM
1 parent 3edb355 commit 1553ea2

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

src/liballoc/rc.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1359,7 +1359,14 @@ trait RcBoxPtr<T: ?Sized> {
13591359

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

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

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

13801394
#[inline]

0 commit comments

Comments
 (0)