From bc5b24e3adcaa455136dbb2f20bb77d3442c7ef4 Mon Sep 17 00:00:00 2001 From: YuSanka Date: Wed, 3 May 2023 12:18:19 +0200 Subject: [PATCH] ConfigWizard: Fix performance Call update_materials() only when it's reasonable + Improve update_materials(). Code refactoring to fill all variable during one loop trough filaments + TaskTimer: Extended output --- src/slic3r/GUI/ConfigWizard.cpp | 176 +++++++----------------- src/slic3r/GUI/ConfigWizard_private.hpp | 13 +- src/slic3r/GUI/GUI_Utils.cpp | 6 +- 3 files changed, 58 insertions(+), 137 deletions(-) diff --git a/src/slic3r/GUI/ConfigWizard.cpp b/src/slic3r/GUI/ConfigWizard.cpp index 1f194966c0..741ab7be00 100644 --- a/src/slic3r/GUI/ConfigWizard.cpp +++ b/src/slic3r/GUI/ConfigWizard.cpp @@ -786,6 +786,16 @@ PageMaterials::PageMaterials(ConfigWizard *parent, Materials *materials, wxStrin reload_presets(); set_compatible_printers_html_window(std::vector(), false); } + +void PageMaterials::check_and_update_presets(bool force_reload_presets /*= false*/) +{ + if (presets_loaded) + return; + wizard_p()->update_materials(materials->technology); + if (force_reload_presets) + reload_presets(); +} + void PageMaterials::on_paint() { } @@ -1299,10 +1309,7 @@ void PageMaterials::clear() void PageMaterials::on_activate() { - if (! presets_loaded) { - wizard_p()->update_materials(materials->technology); - reload_presets(); - } + check_and_update_presets(true); first_paint = true; } @@ -2507,7 +2514,7 @@ void ConfigWizard::priv::load_vendors() } } appconfig_new.set_section(section_name, section_new); - }; + } } void ConfigWizard::priv::add_page(ConfigWizardPage *page) @@ -2589,78 +2596,37 @@ void ConfigWizard::priv::set_run_reason(RunReason run_reason) void ConfigWizard::priv::update_materials(Technology technology) { + auto add_material = [](Materials& materials, PresetAliases& aliases, const Preset& preset, const Preset* printer = nullptr) { + if (!materials.containts(&preset)) { + materials.push(&preset); + if (!preset.alias.empty()) + aliases[preset.alias].emplace(&preset); + } + if (printer) { + materials.add_printer(printer); + materials.compatibility_counter[preset.alias]++; + } + }; + if ((any_fff_selected || custom_printer_in_bundle || custom_printer_selected) && (technology & T_FFF)) { filaments.clear(); aliases_fff.clear(); - // Iterate filaments in all bundles - for (const auto &pair : bundles) { - for (const auto &filament : pair.second.preset_bundle->filaments) { - // Check if filament is already added - if (filaments.containts(&filament)) - continue; + + for (const auto &[name, bundle] : bundles) { + for (const auto &filament : bundle.preset_bundle->filaments) { // Iterate printers in all bundles - for (const auto &printer : pair.second.preset_bundle->printers) { + for (const auto &printer : bundle.preset_bundle->printers) { if (!printer.is_visible || printer.printer_technology() != ptFFF) continue; // Filter out inapplicable printers - if (is_compatible_with_printer(PresetWithVendorProfile(filament, filament.vendor), PresetWithVendorProfile(printer, printer.vendor))) { - if (!filaments.containts(&filament)) { - filaments.push(&filament); - if (!filament.alias.empty()) - aliases_fff[filament.alias].insert(filament.name); - } - filaments.add_printer(&printer); - } + if (is_compatible_with_printer(PresetWithVendorProfile(filament, filament.vendor), PresetWithVendorProfile(printer, printer.vendor))) + add_material(filaments, aliases_fff, filament, &printer); } // template filament bundle has no printers - filament would be never added - if(pair.second.vendor_profile&& pair.second.vendor_profile->templates_profile && pair.second.preset_bundle->printers.begin() == pair.second.preset_bundle->printers.end()) - { - if (!filaments.containts(&filament)) { - filaments.push(&filament); - if (!filament.alias.empty()) - aliases_fff[filament.alias].insert(filament.name); - } - } + if(bundle.vendor_profile && bundle.vendor_profile->templates_profile && bundle.preset_bundle->printers.begin() == bundle.preset_bundle->printers.end()) + add_material(filaments, aliases_fff, filament); } } - // count compatible printers - for (const auto& preset : filaments.presets) { - // skip template filaments - if (preset->vendor && preset->vendor->templates_profile) - continue; - - const auto filter = [preset](const std::pair element) { - return preset->alias == element.first; - }; - if (std::find_if(filaments.compatibility_counter.begin(), filaments.compatibility_counter.end(), filter) != filaments.compatibility_counter.end()) { - continue; - } - // find all aliases (except templates) - std::vector idx_with_same_alias; - for (size_t i = 0; i < filaments.presets.size(); ++i) { - if (preset->alias == filaments.presets[i]->alias && ((filaments.presets[i]->vendor && !filaments.presets[i]->vendor->templates_profile) || !filaments.presets[i]->vendor)) - idx_with_same_alias.push_back(i); - } - // check compatibility with each printer - size_t counter = 0; - for (const auto& printer : filaments.printers) { - if (!(*printer).is_visible || (*printer).printer_technology() != ptFFF) - continue; - bool compatible = false; - // Test other materials with same alias - for (size_t i = 0; i < idx_with_same_alias.size() && !compatible; ++i) { - const Preset& prst = *(filaments.presets[idx_with_same_alias[i]]); - const Preset& prntr = *printer; - if (is_compatible_with_printer(PresetWithVendorProfile(prst, prst.vendor), PresetWithVendorProfile(prntr, prntr.vendor))) { - compatible = true; - break; - } - } - if (compatible) - counter++; - } - filaments.compatibility_counter.emplace_back(preset->alias, counter); - } } if (any_sla_selected && (technology & T_SLA)) { @@ -2668,62 +2634,20 @@ void ConfigWizard::priv::update_materials(Technology technology) aliases_sla.clear(); // Iterate SLA materials in all bundles - for (const auto &pair : bundles) { - for (const auto &material : pair.second.preset_bundle->sla_materials) { - // Check if material is already added - if (sla_materials.containts(&material)) - continue; + for (const auto& [name, bundle] : bundles) { + for (const auto &material : bundle.preset_bundle->sla_materials) { // Iterate printers in all bundles // For now, we only allow the profiles to be compatible with another profiles inside the same bundle. - for (const auto& printer : pair.second.preset_bundle->printers) { + for (const auto& printer : bundle.preset_bundle->printers) { if(!printer.is_visible || printer.printer_technology() != ptSLA) continue; // Filter out inapplicable printers - if (is_compatible_with_printer(PresetWithVendorProfile(material, nullptr), PresetWithVendorProfile(printer, nullptr))) { + if (is_compatible_with_printer(PresetWithVendorProfile(material, nullptr), PresetWithVendorProfile(printer, nullptr))) // Check if material is already added - if(!sla_materials.containts(&material)) { - sla_materials.push(&material); - if (!material.alias.empty()) - aliases_sla[material.alias].insert(material.name); - } - sla_materials.add_printer(&printer); - } + add_material(sla_materials, aliases_sla, material, &printer); } } } - // count compatible printers - for (const auto& preset : sla_materials.presets) { - - const auto filter = [preset](const std::pair element) { - return preset->alias == element.first; - }; - if (std::find_if(sla_materials.compatibility_counter.begin(), sla_materials.compatibility_counter.end(), filter) != sla_materials.compatibility_counter.end()) { - continue; - } - std::vector idx_with_same_alias; - for (size_t i = 0; i < sla_materials.presets.size(); ++i) { - if(preset->alias == sla_materials.presets[i]->alias) - idx_with_same_alias.push_back(i); - } - size_t counter = 0; - for (const auto& printer : sla_materials.printers) { - if (!(*printer).is_visible || (*printer).printer_technology() != ptSLA) - continue; - bool compatible = false; - // Test otrher materials with same alias - for (size_t i = 0; i < idx_with_same_alias.size() && !compatible; ++i) { - const Preset& prst = *(sla_materials.presets[idx_with_same_alias[i]]); - const Preset& prntr = *printer; - if (is_compatible_with_printer(PresetWithVendorProfile(prst, prst.vendor), PresetWithVendorProfile(prntr, prntr.vendor))) { - compatible = true; - break; - } - } - if (compatible) - counter++; - } - sla_materials.compatibility_counter.emplace_back(preset->alias, counter); - } } } @@ -2857,21 +2781,15 @@ bool ConfigWizard::priv::on_bnt_finish() index->go_to(page_downloader); return false; } - /* When Filaments or Sla Materials pages are activated, - * materials for this pages are automaticaly updated and presets are reloaded. - * - * But, if _Finish_ button was clicked without activation of those pages - * (for example, just some printers were added/deleted), - * than last changes wouldn't be updated for filaments/materials. - * SO, do that before close of Wizard - */ - update_materials(T_ANY); - if (any_fff_selected) - page_filaments->reload_presets(); - if (any_sla_selected) - page_sla_materials->reload_presets(); - // theres no need to check that filament is selected if we have only custom printer + /* If some printers were added/deleted, but related MaterialPage wasn't activated, + * than last changes wouldn't be updated for filaments/materials. + * SO, do that before check_and_install_missing_materials() + */ + page_filaments->check_and_update_presets(); + page_sla_materials->check_and_update_presets(); + + // there's no need to check that filament is selected if we have only custom printer if (custom_printer_selected && !any_fff_selected && !any_sla_selected) return true; // check, that there is selected at least one filament/material return check_and_install_missing_materials(T_ANY); @@ -3357,8 +3275,8 @@ void ConfigWizard::priv::update_presets_in_config(const std::string& section, co // add or delete presets had a same alias auto it = aliases.find(alias_key); if (it != aliases.end()) - for (const std::string& name : it->second) - update(section, name); + for (const Preset* preset : it->second) + update(section, preset->name); } bool ConfigWizard::priv::check_fff_selected() diff --git a/src/slic3r/GUI/ConfigWizard_private.hpp b/src/slic3r/GUI/ConfigWizard_private.hpp index ea4701cd67..ce43fd5e71 100644 --- a/src/slic3r/GUI/ConfigWizard_private.hpp +++ b/src/slic3r/GUI/ConfigWizard_private.hpp @@ -296,6 +296,7 @@ struct PageMaterials: ConfigWizardPage PageMaterials(ConfigWizard *parent, Materials *materials, wxString title, wxString shortname, wxString list1name); + void check_and_update_presets(bool force_reload_presets = false); void reload_presets(); void update_lists(int sel_type, int sel_vendor, int last_selected_printer = -1); void on_material_highlighted(int sel_material); @@ -321,8 +322,8 @@ struct Materials Technology technology; // use vector for the presets to purpose of save of presets sorting in the bundle std::vector presets; - // String is alias of material, size_t number of compatible counters - std::vector> compatibility_counter; + // String is alias of material, size_t number of compatible printers counters + std::map compatibility_counter; std::set types; std::set printers; @@ -579,7 +580,7 @@ wxDEFINE_EVENT(EVT_INDEX_PAGE, wxCommandEvent); // ConfigWizard private data -typedef std::map> PresetAliases; +typedef std::map> PresetAliases; struct ConfigWizard::priv { @@ -592,14 +593,14 @@ struct ConfigWizard::priv // PrinterPickers state. Materials filaments; // Holds available filament presets and their types & vendors Materials sla_materials; // Ditto for SLA materials - PresetAliases aliases_fff; // Map of aliase to preset names - PresetAliases aliases_sla; // Map of aliase to preset names + PresetAliases aliases_fff; // Map of alias to material presets + PresetAliases aliases_sla; // Map of alias to material presets std::unique_ptr custom_config; // Backing for custom printer definition bool any_fff_selected; // Used to decide whether to display Filaments page bool any_sla_selected; // Used to decide whether to display SLA Materials page bool custom_printer_selected { false }; // New custom printer is requested bool custom_printer_in_bundle { false }; // Older custom printer already exists when wizard starts - // Set to true if there are none FFF printers on the main FFF page. If true, only SLA printers are shown (not even custum printers) + // Set to true if there are none FFF printers on the main FFF page. If true, only SLA printers are shown (not even custom printers) bool only_sla_mode { false }; bool template_profile_selected { false }; // This bool has one purpose - to tell that template profile should be installed if its not (because it cannot be added to appconfig) diff --git a/src/slic3r/GUI/GUI_Utils.cpp b/src/slic3r/GUI/GUI_Utils.cpp index f409756f01..e9318f65e4 100644 --- a/src/slic3r/GUI/GUI_Utils.cpp +++ b/src/slic3r/GUI/GUI_Utils.cpp @@ -4,6 +4,7 @@ ///|/ #include "GUI_Utils.hpp" #include "GUI_App.hpp" +#include "format.hpp" #include #include @@ -304,8 +305,9 @@ TaskTimer::~TaskTimer() { std::chrono::milliseconds stop_timer = std::chrono::duration_cast( std::chrono::system_clock::now().time_since_epoch()); - auto process_duration = std::chrono::milliseconds(stop_timer - start_timer).count(); - std::string out = (boost::format("\n!!! %1% duration = %2% ms \n\n") % task_name % process_duration).str(); + const auto process_duration_ms = std::chrono::milliseconds(stop_timer - start_timer).count(); + const auto process_duration_s = long long(0.001 * process_duration_ms); + std::string out = format("\n! \"%1%\" duration = %2% s (%3% ms) \n", task_name, process_duration_s, process_duration_ms); printf("%s", out.c_str()); #ifdef __WXMSW__ std::wstring stemp = std::wstring(out.begin(), out.end());