perf(gui): optimize GUI add_dirs allocations and cloning (#191)

*  Optimize `add_dirs` method by removing redundant allocations

Replaced the `iter().unique().cloned().collect()` pipeline with a simple
contains check before pushing to `app_state.dirs`. This prevents entire
vector allocations and item clones per user folder pick. Furthermore,
saving the configuration and cloning `dirs` is now only performed if the
app state actually changed (e.g. if the user picked a new folder).

Cleaned up the unused `itertools::Itertools` import.

Co-authored-by: arabianq <55220741+arabianq@users.noreply.github.com>

*  Optimize GUI `add_dirs` allocations and cloning

Replaced the `iter().unique().cloned().collect()` pipeline with a simple
contains check before pushing to `app_state.dirs`. This prevents entire
vector allocations and item clones per user folder pick. Furthermore,
saving the configuration and cloning `dirs` is now only performed if the
app state actually changed (e.g. if the user picked a new folder).

Cleaned up the unused `itertools::Itertools` import. Cargo.lock was
not touched.

Co-authored-by: arabianq <55220741+arabianq@users.noreply.github.com>

---------

Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
This commit is contained in:
Tarasov Aleksandr
2026-09-09 11:30:56 +03:00
committed by GitHub
co-authored by google-labs-jules[bot]
parent 3d1bc24c21
commit dd13eee0df
+6 -2
View File
@@ -5,7 +5,6 @@ mod views;
use anyhow::{Result, anyhow}; use anyhow::{Result, anyhow};
use eframe::{NativeOptions, icon_data::from_png_bytes, run_native}; use eframe::{NativeOptions, icon_data::from_png_bytes, run_native};
use egui::{Context, FontData, FontDefinitions, FontFamily, FontTweak, Vec2, ViewportBuilder}; use egui::{Context, FontData, FontDefinitions, FontFamily, FontTweak, Vec2, ViewportBuilder};
use itertools::Itertools;
use pwsp_lib::{ use pwsp_lib::{
types::{ types::{
audio_player::PlayerState, audio_player::PlayerState,
@@ -110,14 +109,19 @@ impl SoundpadGui {
pub fn add_dirs(&mut self) { pub fn add_dirs(&mut self) {
let file_dialog = FileDialog::new(); let file_dialog = FileDialog::new();
if let Some(paths) = file_dialog.pick_folders() { if let Some(paths) = file_dialog.pick_folders() {
let mut changed = false;
for path in paths { for path in paths {
if !self.app_state.dirs.contains(&path) {
self.app_state.dirs.push(path); self.app_state.dirs.push(path);
changed = true;
} }
self.app_state.dirs = self.app_state.dirs.iter().unique().cloned().collect(); }
if changed {
self.config.dirs = self.app_state.dirs.clone(); self.config.dirs = self.app_state.dirs.clone();
self.config.save_to_file().ok(); self.config.save_to_file().ok();
} }
} }
}
pub fn open_dir(&mut self, path: &PathBuf, ctx: Option<Context>, force_rescan: bool) { pub fn open_dir(&mut self, path: &PathBuf, ctx: Option<Context>, force_rescan: bool) {
self.app_state.current_dir = Some(path.clone()); self.app_state.current_dir = Some(path.clone());