diff --git a/src/util/address.rs b/src/util/address.rs index f5ad843ee8..123f3d41d4 100644 --- a/src/util/address.rs +++ b/src/util/address.rs @@ -503,7 +503,14 @@ impl ObjectReference { NonZeroUsize::new(addr.0).map(ObjectReference) } - /// Like `from_raw_address`, but assume `addr` is not zero. + /// Like `from_raw_address`, but assume `addr` is not zero. This can be used to elide a check + /// against zero for performance-critical code. + /// + /// # Safety + /// + /// This method assumes `addr` is not zero. It should only be used in cases where we know at + /// compile time that the input cannot be zero. For example, if we compute the address by + /// adding a positive offset to a non-zero address, we know the result must not be zero. pub unsafe fn from_raw_address_unchecked(addr: Address) -> ObjectReference { debug_assert!(!addr.is_zero()); ObjectReference(NonZeroUsize::new_unchecked(addr.0)) diff --git a/src/util/metadata/vo_bit/mod.rs b/src/util/metadata/vo_bit/mod.rs index efa247b9b2..7f1648de8d 100644 --- a/src/util/metadata/vo_bit/mod.rs +++ b/src/util/metadata/vo_bit/mod.rs @@ -100,9 +100,7 @@ pub fn is_vo_bit_set(object: ObjectReference) -> bool { /// Check if an address can be turned directly into an object reference using the VO bit. /// If so, return `Some(object)`. Otherwise return `None`. pub fn is_vo_bit_set_for_addr(address: Address) -> Option { - let Some(potential_object) = ObjectReference::from_raw_address(address) else { - return None; - }; + let potential_object = ObjectReference::from_raw_address(address)?; let addr = potential_object.to_address::(); @@ -126,9 +124,7 @@ pub fn is_vo_bit_set_for_addr(address: Address) -> Option(address: Address) -> Option { - let Some(potential_object) = ObjectReference::from_raw_address(address) else { - return None; - }; + let potential_object = ObjectReference::from_raw_address(address)?; let addr = potential_object.to_address::();