vm_memory: make unchecked_add actually unchecked

GuestAddress::unchecked_add() previously used a normal + operator to add
the two values, which got converted into a checked addition even in
release mode due to our use of `overflow-checks = true`. Explicitly call
the wrapping_add() function instead to ensure we get the natural binary
integer behavior, which can be compiled into a simple add instruction
without any extra checks.

Additionally, mark the function as #[inline] - even though the function
body is quite small, in practice, I observed the compiler emitting a
call instead of simply inlining the single instruction (at least in
non-LTO release builds).

This probably has very little impact in practice, but it should slightly
improve codegen in some critical paths for virtio descriptor processing.

BUG=None
TEST=tools/dev_container tools/presubmit
TEST=cargo build --release and observe no call in SplitQueue::add_used

Change-Id: Idad5acccf56b4428f8db890576e37e470e61584d
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/4679849
Reviewed-by: Zihan Chen <zihanchen@google.com>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
This commit is contained in:
Daniel Verkamp 2023-07-11 10:48:28 -07:00 committed by crosvm LUCI
parent 800f2bec5f
commit b714d56d3e

View file

@ -57,8 +57,9 @@ impl GuestAddress {
/// Returns the result of the base address + the size.
/// Only use this when `offset` is guaranteed not to overflow.
#[inline]
pub fn unchecked_add(self, offset: u64) -> GuestAddress {
GuestAddress(self.0 + offset)
GuestAddress(self.0.wrapping_add(offset))
}
/// Returns the result of the subtraction of None if there is underflow.