From 6adca76b5a82d9b910afe9e94ddc807cd95f3408 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Mikrut?= Date: Wed, 7 Feb 2024 09:06:37 +0100 Subject: [PATCH] SD --- czkawka_core/Cargo.toml | 2 +- czkawka_core/src/common.rs | 19 ++++++++++--------- czkawka_gui/src/saving_loading.rs | 6 +++--- krokiet/src/set_initial_gui_info.rs | 4 ++-- krokiet/src/settings.rs | 4 ++-- 5 files changed, 18 insertions(+), 17 deletions(-) diff --git a/czkawka_core/Cargo.toml b/czkawka_core/Cargo.toml index b442436..7b95707 100644 --- a/czkawka_core/Cargo.toml +++ b/czkawka_core/Cargo.toml @@ -42,7 +42,7 @@ blake3 = "1.5" crc32fast = "1.3" xxhash-rust = { version = "0.8", features = ["xxh3"] } -tempfile = "3.9" +tempfile = "3.10" # Video Duplicates vid_dup_finder_lib = "0.1" diff --git a/czkawka_core/src/common.rs b/czkawka_core/src/common.rs index b0515ba..098be44 100644 --- a/czkawka_core/src/common.rs +++ b/czkawka_core/src/common.rs @@ -39,6 +39,7 @@ use crate::duplicate::make_hard_link; use crate::CZKAWKA_VERSION; static NUMBER_OF_THREADS: state::InitCell = state::InitCell::new(); +static ALL_AVAILABLE_THREADS: state::InitCell = state::InitCell::new(); pub const DEFAULT_THREAD_SIZE: usize = 8 * 1024 * 1024; // 8 MB pub const DEFAULT_WORKER_THREAD_SIZE: usize = 4 * 1024 * 1024; // 4 MB @@ -47,7 +48,7 @@ pub fn get_number_of_threads() -> usize { if *data >= 1 { *data } else { - get_default_number_of_threads() + get_all_available_threads() } } @@ -66,15 +67,19 @@ pub fn setup_logger(disabled_printing: bool) { handsome_logger::TermLogger::init(config, TerminalMode::Mixed, ColorChoice::Always).unwrap(); } -pub fn get_available_threads() -> usize { - thread::available_parallelism().map(std::num::NonZeroUsize::get).unwrap_or(1) +pub fn get_all_available_threads() -> usize { + *ALL_AVAILABLE_THREADS.get_or_init(|| { + let available_threads = thread::available_parallelism().map(std::num::NonZeroUsize::get).unwrap_or(1); + ALL_AVAILABLE_THREADS.set(available_threads); + available_threads + }) } pub fn print_version_mode() { let rust_version = env!("RUST_VERSION_INTERNAL"); let debug_release = if cfg!(debug_assertions) { "debug" } else { "release" }; - let processors = get_available_threads(); + let processors = get_all_available_threads(); let info = os_info::get(); info!( @@ -94,11 +99,7 @@ pub fn print_version_mode() { } pub fn set_default_number_of_threads() { - set_number_of_threads(get_default_number_of_threads()); -} - -pub fn get_default_number_of_threads() -> usize { - thread::available_parallelism().map(std::num::NonZeroUsize::get).unwrap_or(1) + set_number_of_threads(get_all_available_threads()); } pub fn set_number_of_threads(thread_number: usize) { diff --git a/czkawka_gui/src/saving_loading.rs b/czkawka_gui/src/saving_loading.rs index c9a958a..1a68723 100644 --- a/czkawka_gui/src/saving_loading.rs +++ b/czkawka_gui/src/saving_loading.rs @@ -9,7 +9,7 @@ use directories_next::ProjectDirs; use gtk4::prelude::*; use gtk4::{ComboBoxText, ScrolledWindow, TextView, TreeView}; -use czkawka_core::common::get_default_number_of_threads; +use czkawka_core::common::get_all_available_threads; use czkawka_core::common_dir_traversal::CheckingMethod; use czkawka_core::common_items::DEFAULT_EXCLUDED_ITEMS; use czkawka_core::similar_images::SIMILAR_VALUES; @@ -833,8 +833,8 @@ pub fn load_configuration( main_notebook.scale_similarity_similar_images.connect_change_value(scale_step_function); main_notebook.scale_similarity_similar_images.set_value(similar_images_similarity as f64); - settings.scale_settings_number_of_threads.set_range(0_f64, get_default_number_of_threads() as f64); - settings.scale_settings_number_of_threads.set_fill_level(get_default_number_of_threads() as f64); + settings.scale_settings_number_of_threads.set_range(0_f64, get_all_available_threads() as f64); + settings.scale_settings_number_of_threads.set_fill_level(get_all_available_threads() as f64); settings.scale_settings_number_of_threads.connect_change_value(scale_step_function); settings.scale_settings_number_of_threads.set_value(thread_number as f64); } else { diff --git a/krokiet/src/set_initial_gui_info.rs b/krokiet/src/set_initial_gui_info.rs index b69cfa5..5543fe9 100644 --- a/krokiet/src/set_initial_gui_info.rs +++ b/krokiet/src/set_initial_gui_info.rs @@ -1,6 +1,6 @@ use slint::{ComponentHandle, SharedString, VecModel}; -use czkawka_core::common::get_available_threads; +use czkawka_core::common::get_all_available_threads; use crate::settings::{ALLOWED_HASH_SIZE_VALUES, ALLOWED_HASH_TYPE_VALUES, ALLOWED_RESIZE_ALGORITHM_VALUES}; use crate::{GuiState, MainWindow, Settings}; @@ -8,7 +8,7 @@ use crate::{GuiState, MainWindow, Settings}; // Some info needs to be send to gui at the start like available thread number in OS. // pub fn set_initial_gui_infos(app: &MainWindow) { - let threads = get_available_threads(); + let threads = get_all_available_threads(); let settings = app.global::(); app.global::().set_maximum_threads(threads as f32); diff --git a/krokiet/src/settings.rs b/krokiet/src/settings.rs index b22d9f6..7beb513 100644 --- a/krokiet/src/settings.rs +++ b/krokiet/src/settings.rs @@ -9,7 +9,7 @@ use log::{debug, error, info, warn}; use serde::{Deserialize, Serialize}; use slint::{ComponentHandle, Model, ModelRc}; -use czkawka_core::common::{get_available_threads, set_number_of_threads}; +use czkawka_core::common::{get_all_available_threads, set_number_of_threads}; use czkawka_core::common_items::{DEFAULT_EXCLUDED_DIRECTORIES, DEFAULT_EXCLUDED_ITEMS}; use crate::common::{create_excluded_directories_model_from_pathbuf, create_included_directories_model_from_pathbuf, create_vec_model_from_vec_string}; @@ -240,7 +240,7 @@ pub fn load_settings_from_file(app: &MainWindow) { } } base_settings.default_preset = max(min(base_settings.default_preset, 9), 0); - custom_settings.thread_number = max(min(custom_settings.thread_number, get_available_threads() as i32), 0); + custom_settings.thread_number = max(min(custom_settings.thread_number, get_all_available_threads() as i32), 0); // Ended validating set_settings_to_gui(app, &custom_settings);