Skip to content

Commit f0b3c02

Browse files
committed
Optimize RefCell read borrowing
Instead of doing two comparisons we can do only one with a bit of cleverness. LLVM currently can't do this optimization itself on x86-64.
1 parent a9c7feb commit f0b3c02

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

Diff for: src/libcore/cell.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1101,13 +1101,13 @@ struct BorrowRef<'b> {
11011101
impl<'b> BorrowRef<'b> {
11021102
#[inline]
11031103
fn new(borrow: &'b Cell<BorrowFlag>) -> Option<BorrowRef<'b>> {
1104-
let b = borrow.get();
1105-
if is_writing(b) || b == isize::max_value() {
1104+
let b = borrow.get().wrapping_add(1);
1105+
if !is_reading(b) {
11061106
// If there's currently a writing borrow, or if incrementing the
11071107
// refcount would overflow into a writing borrow.
11081108
None
11091109
} else {
1110-
borrow.set(b + 1);
1110+
borrow.set(b);
11111111
Some(BorrowRef { borrow })
11121112
}
11131113
}

0 commit comments

Comments
 (0)