]> git.ipfire.org Git - thirdparty/qemu.git/commitdiff
rust/hpet: Borrow HPETState.regs once in HPETState::post_load()
authorZhao Liu <zhao1.liu@intel.com>
Thu, 13 Nov 2025 05:19:29 +0000 (13:19 +0800)
committerPaolo Bonzini <pbonzini@redhat.com>
Sat, 27 Dec 2025 09:11:10 +0000 (10:11 +0100)
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 <zhao1.liu@intel.com>
Link: https://lore.kernel.org/r/20251113051937.4017675-15-zhao1.liu@intel.com
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
rust/hw/timer/hpet/src/device.rs

index da938f356e98ce64ffe7118f99b4afcef69ded14..abbaebc405e060e82313a1ddc988e5a0debc5f8c 100644 (file)
@@ -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(())