-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Change Rc::inc_{weak,strong}
to better hint optimization to LLVM
#53080
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
1553ea2
4e17fbd
85b92c1
0b83914
0dd10af
79a905e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hm, should we instead have unreachable_unchecked for the == 0 case? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Confusingly, doing this: - if self.strong() == 0 || self.strong() == usize::max_value() {
+ if self.strong() == 0 {
+ unsafe { unreachable_unchecked(); }
+ }
+ if self.strong() == usize::max_value() {
unsafe { abort(); } produces the same output as before any changes ( There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, hm, that might be because LLVM for whatever reason decides that subtraction can overflow or some such. Seems fine to leave it then. |
||
unsafe { abort(); } | ||
} | ||
self.inner().strong.set(self.strong() + 1); | ||
} | ||
|
||
#[inline] | ||
|
@@ -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] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo: “missied” (here and below).