From 5f97c1f9c19fe4c9d3d7b3821ea359c375510b1e Mon Sep 17 00:00:00 2001 From: Logan McNaughton <848146+loganmc10@users.noreply.github.com> Date: Thu, 12 Nov 2026 07:16:25 +0000 Subject: [PATCH] don't clone cheats each VI (#1383) --- src/cheats.rs | 14 +++++++++++--- src/device/vi.rs | 2 +- src/savestates.rs | 2 +- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/cheats.rs b/src/cheats.rs index 31cb3a6e..efb6810d 100644 --- a/src/cheats.rs +++ b/src/cheats.rs @@ -134,7 +134,16 @@ fn equal_half(device: &mut device::Device, cheat_line: &DecodedCheat) -> bool { current_value1 == (cheat_line.data >> 8) as u8 && current_value2 == cheat_line.data as u8 } -pub fn execute_cheats(device: &mut device::Device, cheats: Vec>) { +pub fn execute_cheats(device: &mut device::Device) { + // Temporarily move the list out so it can be borrowed while the device is mutated, + // instead of cloning it every VI. + let cheats = std::mem::take(&mut device.cheats.cheats); + run_cheats(device, &cheats); + device.cheats.cheats = cheats; + device.cheats.boot = false; +} + +fn run_cheats(device: &mut device::Device, cheats: &[Vec]) { for cheat in cheats.iter() { let mut valid = true; let mut cheat_iter = cheat.iter().peekable(); @@ -155,7 +164,7 @@ pub fn execute_cheats(device: &mut device::Device, cheats: Vec }; expanded_cheat.push(line); } - execute_cheats(device, [expanded_cheat].to_vec()); + run_cheats(device, &[expanded_cheat]); } valid = false; // skip the next line } @@ -200,5 +209,4 @@ pub fn execute_cheats(device: &mut device::Device, cheats: Vec } } } - device.cheats.boot = false; } diff --git a/src/device/vi.rs b/src/device/vi.rs index e3daefc5..9a972aee 100644 --- a/src/device/vi.rs +++ b/src/device/vi.rs @@ -165,7 +165,7 @@ pub fn vertical_interrupt_event(device: &mut device::Device) { device.vi.elapsed_time += device.vi.frame_time; if device.cheats.enabled { - cheats::execute_cheats(device, device.cheats.cheats.clone()); + cheats::execute_cheats(device); } retroachievements::do_frame(); diff --git a/src/savestates.rs b/src/savestates.rs index f78c64f1..8e893b7e 100644 --- a/src/savestates.rs +++ b/src/savestates.rs @@ -374,7 +374,7 @@ fn apply_state(device: &mut device::Device, state: &SavestateData, run_cheats: b } if run_cheats && device.cheats.enabled { - cheats::execute_cheats(device, device.cheats.cheats.clone()); + cheats::execute_cheats(device); } true