From 151f43f1abdc94fd5160f7a2a9170f2cdc577e98 Mon Sep 17 00:00:00 2001 From: Tarasov Aleksandr <55220741+arabianq@users.noreply.github.com> Date: Sun, 8 Mar 2026 00:32:42 +0300 Subject: [PATCH] Optimize value lookup in GetFullStateCommand loop (#30) Moves the check `if let Some(current_input_name) = &audio_player.input_device_name` outside the loop over `input_devices` in `GetFullStateCommand::execute`. By creating two separate loops (one for when an input device name is selected, and one for when it is not), we eliminate the overhead of evaluating the `Option` on every single iteration of the `input_devices` array. This effectively unswitches the loop and avoids repeatedly accessing the `audio_player` struct field. Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com> --- src/types/commands.rs | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/src/types/commands.rs b/src/types/commands.rs index ecb4dbf..8911385 100644 --- a/src/types/commands.rs +++ b/src/types/commands.rs @@ -392,19 +392,26 @@ impl Executable for GetFullStateCommand { let mut current_input_nick = String::new(); let audio_player = get_audio_player().await.lock().await; - let current_input_name = audio_player.input_device_name.as_deref(); - for device in input_devices { - if device.name == "pwsp-virtual-mic" { - continue; - } + if let Some(current_input_name) = &audio_player.input_device_name { + for device in input_devices { + if device.name == "pwsp-virtual-mic" { + continue; + } - if let Some(name) = current_input_name { - if device.name == name { + if device.name == *current_input_name { current_input_nick = format!("{} - {}", device.name, device.nick); } - } - all_inputs.insert(device.name, device.nick); + all_inputs.insert(device.name, device.nick); + } + } else { + for device in input_devices { + if device.name == "pwsp-virtual-mic" { + continue; + } + + all_inputs.insert(device.name, device.nick); + } } let full_state = FullState {