From: Zhao Liu Date: Thu, 13 Nov 2025 05:19:29 +0000 (+0800) Subject: rust/hpet: Borrow HPETState.regs once in HPETState::post_load() X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e3b5a1190a184586f66d8dc27949b212f3abbab1;p=thirdparty%2Fqemu.git rust/hpet: Borrow HPETState.regs once in HPETState::post_load() Timers in post_load() access the same HPETState, which is the "self" HPETState. So there's no need to access HPETState from child HPETTimer again and again. Instead, just cache and borrow HPETState.regs at the beginning, and this could save some CPU cycles and reduce borrow() calls. It's safe, because post_load() is called with BQL protection, so that there's no other chance to modify the regs. Signed-off-by: Zhao Liu Link: https://lore.kernel.org/r/20251113051937.4017675-15-zhao1.liu@intel.com Signed-off-by: Paolo Bonzini --- diff --git a/rust/hw/timer/hpet/src/device.rs b/rust/hw/timer/hpet/src/device.rs index da938f356e..abbaebc405 100644 --- a/rust/hw/timer/hpet/src/device.rs +++ b/rust/hw/timer/hpet/src/device.rs @@ -882,9 +882,11 @@ impl HPETState { } fn post_load(&self, _version_id: u8) -> Result<(), migration::Infallible> { + let regs = self.regs.borrow(); + for timer in self.timers.iter().take(self.num_timers) { let mut t = timer.borrow_mut(); - let cnt = t.get_state().regs.borrow().counter; + let cnt = regs.counter; t.cmp64 = t.calculate_cmp64(cnt, t.regs.cmp); t.last = CLOCK_VIRTUAL.get_ns() - NANOSECONDS_PER_SECOND; @@ -893,7 +895,7 @@ impl HPETState { // Recalculate the offset between the main counter and guest time if !self.hpet_offset_saved { self.hpet_offset - .set(ticks_to_ns(self.regs.borrow().counter) - CLOCK_VIRTUAL.get_ns()); + .set(ticks_to_ns(regs.counter) - CLOCK_VIRTUAL.get_ns()); } Ok(())