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 <[email protected]>
---
 rust/hw/timer/hpet/src/device.rs | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/rust/hw/timer/hpet/src/device.rs b/rust/hw/timer/hpet/src/device.rs
index 738ebb374fc9..bd673a1d0110 100644
--- a/rust/hw/timer/hpet/src/device.rs
+++ b/rust/hw/timer/hpet/src/device.rs
@@ -885,9 +885,11 @@ fn pre_save(&self) -> Result<(), migration::Infallible> {
     }
 
     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;
@@ -896,7 +898,7 @@ fn post_load(&self, _version_id: u8) -> Result<(), 
migration::Infallible> {
         // 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(())
-- 
2.34.1


Reply via email to