@@ -2512,14 +2512,21 @@ trait RcInnerPtr {
2512
2512
fn inc_strong ( & self ) {
2513
2513
let strong = self . strong ( ) ;
2514
2514
2515
+ // We insert an `assume` here to hint LLVM at an otherwise
2516
+ // missed optimization.
2517
+ // SAFETY: The reference count will never be zero when this is
2518
+ // called.
2519
+ unsafe { core:: intrinsics:: assume ( strong != 0 ) ; }
2520
+
2521
+ let strong = strong. wrapping_add ( 1 ) ;
2522
+ self . strong_ref ( ) . set ( strong) ;
2523
+
2515
2524
// We want to abort on overflow instead of dropping the value.
2516
- // The reference count will never be zero when this is called;
2517
- // nevertheless, we insert an abort here to hint LLVM at
2518
- // an otherwise missed optimization.
2519
- if strong == 0 || strong == usize:: MAX {
2525
+ // Checking after the store instead of before allows for
2526
+ // slightly better code generation.
2527
+ if core:: intrinsics:: unlikely ( strong == 0 ) {
2520
2528
abort ( ) ;
2521
2529
}
2522
- self . strong_ref ( ) . set ( strong + 1 ) ;
2523
2530
}
2524
2531
2525
2532
#[ inline]
@@ -2536,14 +2543,21 @@ trait RcInnerPtr {
2536
2543
fn inc_weak ( & self ) {
2537
2544
let weak = self . weak ( ) ;
2538
2545
2546
+ // We insert an `assume` here to hint LLVM at an otherwise
2547
+ // missed optimization.
2548
+ // SAFETY: The reference count will never be zero when this is
2549
+ // called.
2550
+ unsafe { core:: intrinsics:: assume ( weak != 0 ) ; }
2551
+
2552
+ let weak = weak. wrapping_add ( 1 ) ;
2553
+ self . weak_ref ( ) . set ( weak) ;
2554
+
2539
2555
// We want to abort on overflow instead of dropping the value.
2540
- // The reference count will never be zero when this is called;
2541
- // nevertheless, we insert an abort here to hint LLVM at
2542
- // an otherwise missed optimization.
2543
- if weak == 0 || weak == usize:: MAX {
2556
+ // Checking after the store instead of before allows for
2557
+ // slightly better code generation.
2558
+ if core:: intrinsics:: unlikely ( weak == 0 ) {
2544
2559
abort ( ) ;
2545
2560
}
2546
- self . weak_ref ( ) . set ( weak + 1 ) ;
2547
2561
}
2548
2562
2549
2563
#[ inline]
0 commit comments