From 838285e9253ffb9e10d34c36b93ceff813456562 Mon Sep 17 00:00:00 2001 From: bseiller Date: Sun, 23 May 2021 11:28:48 +0200 Subject: [PATCH 1/3] Improve performance of surveying => faster search - def.h: changed vectors for inorganics to contain uint8_t instead of bool which improves the performance when using std::fill and std::memset to batch-set the whole array - survey.cpp: using std::memset instead of direct assignment to reset the inorganic vectors, also using the actual size of each vector for the call - changelog.txt: add note concerning the changes --- docs/changelog.txt | 1 + plugins/embark-assistant/defs.h | 9 ++++++--- plugins/embark-assistant/survey.cpp | 11 +++++------ 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/docs/changelog.txt b/docs/changelog.txt index 16589f759..81cf90424 100644 --- a/docs/changelog.txt +++ b/docs/changelog.txt @@ -42,6 +42,7 @@ changelog.txt uses a syntax similar to RST, with a few special sequences: ## Misc Improvements - `tweak` hide-priority: changed so that priorities stay hidden (or visible) when exiting and re-entering the designations menu +- `embark-assistant`: slightly improved performance of surveying and improved code a little ## Lua - ``gui.Painter``: fixed error when calling ``viewport()`` method diff --git a/plugins/embark-assistant/defs.h b/plugins/embark-assistant/defs.h index 9634d7c48..07062f9ce 100644 --- a/plugins/embark-assistant/defs.h +++ b/plugins/embark-assistant/defs.h @@ -91,9 +91,12 @@ namespace embark_assist { int16_t river_elevation = 100; int8_t adamantine_level; // -1 = none, 0 .. 3 = cavern 1 .. magma sea. Currently not used beyond present/absent. int8_t magma_level; // -1 = none, 0 .. 3 = cavern 3 .. surface/volcano - std::vector metals; - std::vector economics; - std::vector minerals; + // using uint8_t instead of bool as vector gets optimized for a small memory footprint which leads to a significant overhead when iterating over all entries, + // also there seems to be no template specialization for std::fill in MSVS C++11 in regards to std::vector and std::memset does not work as expected (=> not at all that is) + // have a look here https://github.com/DFHack/dfhack/pull/1771#discussion_r579498636 for the related discussion and furter resources + std::vector metals; + std::vector economics; + std::vector minerals; }; typedef std::array, 16> mid_level_tiles; diff --git a/plugins/embark-assistant/survey.cpp b/plugins/embark-assistant/survey.cpp index 9eb2f28b6..f7da800b2 100644 --- a/plugins/embark-assistant/survey.cpp +++ b/plugins/embark-assistant/survey.cpp @@ -909,15 +909,14 @@ void inline copy_incursion_values(embark_assist::defs::mid_level_tile_incursion_ //================================================================================= void reset_mlt_inorganics(embark_assist::defs::mid_level_tiles &mlts) { - const uint16_t size = mlts[0][0].metals.size(); for (uint8_t i = 0; i < 16; i++) { for (uint8_t k = 0; k < 16; k++) { embark_assist::defs::mid_level_tile &mlt = mlts[i][k]; - for (uint16_t l = 0; l < size; l++) { - mlt.metals[l] = false; - mlt.economics[l] = false; - mlt.minerals[l] = false; - } + // std::memset is much faster than std::fill and also faster than direct assignment - also std::fill might be compiled to std::memset but is not guaranteed to happen + // have a look here for why: https://travisdowns.github.io/blog/2020/01/20/zero.html + std::memset(&mlt.metals[0], false, mlt.metals.size() * sizeof(mlt.metals[0])); + std::memset(&mlt.economics[0], false, mlt.economics.size() * sizeof(mlt.economics[0])); + std::memset(&mlt.minerals[0], false, mlt.minerals.size() * sizeof(mlt.minerals[0])); } } } From 2516f9927a39099d3f33b8c338058394aca821bb Mon Sep 17 00:00:00 2001 From: bseiller Date: Sun, 23 May 2021 14:12:16 +0200 Subject: [PATCH 2/3] removing trailing whitespace --- plugins/embark-assistant/defs.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/embark-assistant/defs.h b/plugins/embark-assistant/defs.h index 07062f9ce..4df27ee3d 100644 --- a/plugins/embark-assistant/defs.h +++ b/plugins/embark-assistant/defs.h @@ -91,7 +91,7 @@ namespace embark_assist { int16_t river_elevation = 100; int8_t adamantine_level; // -1 = none, 0 .. 3 = cavern 1 .. magma sea. Currently not used beyond present/absent. int8_t magma_level; // -1 = none, 0 .. 3 = cavern 3 .. surface/volcano - // using uint8_t instead of bool as vector gets optimized for a small memory footprint which leads to a significant overhead when iterating over all entries, + // using uint8_t instead of bool as vector gets optimized for a small memory footprint which leads to a significant overhead when iterating over all entries, // also there seems to be no template specialization for std::fill in MSVS C++11 in regards to std::vector and std::memset does not work as expected (=> not at all that is) // have a look here https://github.com/DFHack/dfhack/pull/1771#discussion_r579498636 for the related discussion and furter resources std::vector metals; From 5cd86743f1a4c98472d49cf5291c9c1e6f5dc668 Mon Sep 17 00:00:00 2001 From: bseiller Date: Tue, 25 May 2021 18:56:58 +0200 Subject: [PATCH 3/3] trying to make gcc happy - survey.cpp: adding include to allow gcc to find std::memset --- plugins/embark-assistant/survey.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/plugins/embark-assistant/survey.cpp b/plugins/embark-assistant/survey.cpp index f7da800b2..f0e14378a 100644 --- a/plugins/embark-assistant/survey.cpp +++ b/plugins/embark-assistant/survey.cpp @@ -1,5 +1,6 @@ #include #include +#include #include "Core.h" #include