From bc39279e53cba425baa327ea7553f382055b3ae4 Mon Sep 17 00:00:00 2001 From: bad code <> Date: Sun, 22 Sep 2024 16:43:00 +0200 Subject: [PATCH 01/14] Exit loop only if element found Currently the loop is only executed at most once, the break probably should be inside the if block. --- src/linux/btop_collect.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/linux/btop_collect.cpp b/src/linux/btop_collect.cpp index 4b3ab954..ca56a659 100644 --- a/src/linux/btop_collect.cpp +++ b/src/linux/btop_collect.cpp @@ -2501,8 +2501,10 @@ namespace Net { selected_iface.clear(); //? Try to set to a connected interface for (const auto& iface : sorted_interfaces) { - if (net.at(iface).connected) selected_iface = iface; - break; + if (net.at(iface).connected) { + selected_iface = iface; + break; + } } //? If no interface is connected set to first available if (selected_iface.empty() and not sorted_interfaces.empty()) selected_iface = sorted_interfaces.at(0); From e8ad3da4f2b46a968f09f3fe2f328765a46b2b0f Mon Sep 17 00:00:00 2001 From: bad code <> Date: Sun, 22 Sep 2024 16:43:36 +0200 Subject: [PATCH 02/14] Reserve known vector size Allocate the required memory at once. --- src/btop_config.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/btop_config.cpp b/src/btop_config.cpp index b1ccfd11..e0e4bda6 100644 --- a/src/btop_config.cpp +++ b/src/btop_config.cpp @@ -689,7 +689,8 @@ namespace Config { std::ifstream cread(conf_file); if (cread.good()) { vector valid_names; - for (auto &n : descriptions) + valid_names.reserve(descriptions.size()); + for (const auto &n : descriptions) valid_names.push_back(n[0]); if (string v_string; cread.peek() != '#' or (getline(cread, v_string, '\n') and not s_contains(v_string, Global::Version))) write_new = true; From bb8b967279066cecaedba79edc5e2c7bb6006b1c Mon Sep 17 00:00:00 2001 From: bad code <> Date: Sun, 22 Sep 2024 16:43:56 +0200 Subject: [PATCH 03/14] Avoid copies in loop iterator --- src/btop_config.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/btop_config.cpp b/src/btop_config.cpp index e0e4bda6..d523ebc4 100644 --- a/src/btop_config.cpp +++ b/src/btop_config.cpp @@ -753,7 +753,7 @@ namespace Config { std::ofstream cwrite(conf_file, std::ios::trunc); if (cwrite.good()) { cwrite << "#? Config file for btop v. " << Global::Version << "\n"; - for (auto [name, description] : descriptions) { + for (const auto& [name, description] : descriptions) { cwrite << "\n" << (description.empty() ? "" : description + "\n") << name << " = "; if (strings.contains(name)) From f59d4dafe77fdd88ad6fca71437f7b0f363810c2 Mon Sep 17 00:00:00 2001 From: bad code <> Date: Sun, 22 Sep 2024 16:44:02 +0200 Subject: [PATCH 04/14] Avoid unnecessary copy of vector --- src/btop_menu.cpp | 2 +- src/btop_menu.hpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/btop_menu.cpp b/src/btop_menu.cpp index 488886a5..af7905c9 100644 --- a/src/btop_menu.cpp +++ b/src/btop_menu.cpp @@ -787,7 +787,7 @@ namespace Menu { }; msgBox::msgBox() {} - msgBox::msgBox(int width, int boxtype, vector content, string title) + msgBox::msgBox(int width, int boxtype, const vector& content, string title) : width(width), boxtype(boxtype) { auto tty_mode = Config::getB("tty_mode"); auto rounded = Config::getB("rounded_corners"); diff --git a/src/btop_menu.hpp b/src/btop_menu.hpp index 63974e41..3b028370 100644 --- a/src/btop_menu.hpp +++ b/src/btop_menu.hpp @@ -61,7 +61,7 @@ namespace Menu { Select }; msgBox(); - msgBox(int width, int boxtype, vector content, string title); + msgBox(int width, int boxtype, const vector& content, string title); //? Draw and return box as a string string operator()(); From 5d41d290ac8eba30483982c22ef779dcc6255140 Mon Sep 17 00:00:00 2001 From: bad code <> Date: Sun, 22 Sep 2024 16:44:05 +0200 Subject: [PATCH 05/14] Drop unnecessary string construction --- src/linux/btop_collect.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/linux/btop_collect.cpp b/src/linux/btop_collect.cpp index ca56a659..1f9e6333 100644 --- a/src/linux/btop_collect.cpp +++ b/src/linux/btop_collect.cpp @@ -430,7 +430,7 @@ namespace Cpu { got_coretemp = true; for (const auto & file : fs::directory_iterator(add_path)) { - if (string(file.path().filename()) == "device") { + if (file.path().filename() == "device") { for (const auto & dev_file : fs::directory_iterator(file.path())) { string dev_filename = dev_file.path().filename(); if (dev_filename.starts_with("temp") and dev_filename.ends_with("_input")) { From 9d8a49bad9aecfdc15b83a40527523f0ffd9ea9e Mon Sep 17 00:00:00 2001 From: bad code <> Date: Sun, 22 Sep 2024 16:44:17 +0200 Subject: [PATCH 06/14] Avoid repeated lookup Use find() instead of contains() followed by at(). --- src/btop_config.cpp | 12 ++++++------ src/btop_input.cpp | 4 ++-- src/btop_tools.hpp | 8 ++++---- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/btop_config.cpp b/src/btop_config.cpp index d523ebc4..8aaac604 100644 --- a/src/btop_config.cpp +++ b/src/btop_config.cpp @@ -581,12 +581,12 @@ namespace Config { } string getAsString(const std::string_view name) { - if (bools.contains(name)) - return (bools.at(name) ? "True" : "False"); - else if (ints.contains(name)) - return to_string(ints.at(name)); - else if (strings.contains(name)) - return strings.at(name); + if (auto it = bools.find(name); it != bools.end()) + return it->second ? "True" : "False"; + if (auto it = ints.find(name); it != ints.end()) + return to_string(it->second); + if (auto it = strings.find(name); it != strings.end()) + return it->second; return ""; } diff --git a/src/btop_input.cpp b/src/btop_input.cpp index da77313b..fc4a704e 100644 --- a/src/btop_input.cpp +++ b/src/btop_input.cpp @@ -177,8 +177,8 @@ namespace Input { } } - else if (Key_escapes.contains(key)) - key = Key_escapes.at(key); + else if (auto it = Key_escapes.find(key); it != Key_escapes.end()) + key = it->second; else if (ulen(key) > 1) key.clear(); diff --git a/src/btop_tools.hpp b/src/btop_tools.hpp index 07f7de33..79cf5e00 100644 --- a/src/btop_tools.hpp +++ b/src/btop_tools.hpp @@ -342,8 +342,8 @@ namespace Tools { template #ifdef BTOP_DEBUG const T& safeVal(const std::unordered_map& map, const K& key, const T& fallback = T{}, std::source_location loc = std::source_location::current()) { - if (map.contains(key)) { - return map.at(key); + if (auto it = map.find(key); it != map.end()) { + return it->second; } else { Logger::error(fmt::format("safeVal() called with invalid key: [{}] in file: {} on line: {}", key, loc.file_name(), loc.line())); return fallback; @@ -351,8 +351,8 @@ namespace Tools { }; #else const T& safeVal(const std::unordered_map& map, const K& key, const T& fallback = T{}) { - if (map.contains(key)) { - return map.at(key); + if (auto it = map.find(key); it != map.end()) { + return it->second; } else { Logger::error(fmt::format("safeVal() called with invalid key: [{}] (Compile btop with DEBUG=true for more extensive logging!)", key)); return fallback; From c1a65a36bb6e5be8d58a6f54217079f367555e75 Mon Sep 17 00:00:00 2001 From: bad code <> Date: Sun, 22 Sep 2024 16:44:12 +0200 Subject: [PATCH 07/14] Avoid repeated lookup Cache the value once. --- src/linux/btop_collect.cpp | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/linux/btop_collect.cpp b/src/linux/btop_collect.cpp index 1f9e6333..60aa74f9 100644 --- a/src/linux/btop_collect.cpp +++ b/src/linux/btop_collect.cpp @@ -2420,13 +2420,14 @@ namespace Net { //? Get total received and transmitted bytes + device address if no ip was found for (const auto& iface : interfaces) { - if (net.at(iface).ipv4.empty() and net.at(iface).ipv6.empty()) - net.at(iface).ipv4 = readfile("/sys/class/net/" + iface + "/address"); + auto& netif = net.at(iface); + if (netif.ipv4.empty() and netif.ipv6.empty()) + netif.ipv4 = readfile("/sys/class/net/" + iface + "/address"); for (const string dir : {"download", "upload"}) { const fs::path sys_file = "/sys/class/net/" + iface + "/statistics/" + (dir == "download" ? "rx_bytes" : "tx_bytes"); - auto& saved_stat = net.at(iface).stat.at(dir); - auto& bandwidth = net.at(iface).bandwidth.at(dir); + auto& saved_stat = netif.stat.at(dir); + auto& bandwidth = netif.bandwidth.at(dir); uint64_t val{}; try { val = (uint64_t)stoull(readfile(sys_file, "0")); } @@ -2454,7 +2455,7 @@ namespace Net { //? Set counters for auto scaling if (net_auto and selected_iface == iface) { - if (net_sync and saved_stat.speed < net.at(iface).stat.at(dir == "download" ? "upload" : "download").speed) continue; + if (net_sync and saved_stat.speed < netif.stat.at(dir == "download" ? "upload" : "download").speed) continue; if (saved_stat.speed > graph_max[dir]) { ++max_count[dir][0]; if (max_count[dir][1] > 0) --max_count[dir][1]; From 15ce5e1be4407d685f37808ef85e14da0ec02f10 Mon Sep 17 00:00:00 2001 From: bad code <> Date: Sun, 22 Sep 2024 16:44:19 +0200 Subject: [PATCH 08/14] Avoid repeated lookup erase() works fine if the key does not exist. --- src/btop_menu.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/btop_menu.cpp b/src/btop_menu.cpp index af7905c9..c6792cdf 100644 --- a/src/btop_menu.cpp +++ b/src/btop_menu.cpp @@ -867,8 +867,8 @@ namespace Menu { button_left.shrink_to_fit(); button_right.clear(); button_right.shrink_to_fit(); - if (mouse_mappings.contains("button1")) mouse_mappings.erase("button1"); - if (mouse_mappings.contains("button2")) mouse_mappings.erase("button2"); + mouse_mappings.erase("button1"); + mouse_mappings.erase("button2"); } enum menuReturnCodes { From 02a1971c12251caa2c374f63a9cd08af909b8962 Mon Sep 17 00:00:00 2001 From: bad code <> Date: Sun, 22 Sep 2024 16:44:21 +0200 Subject: [PATCH 09/14] Initialize vector in one step --- src/btop_menu.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/btop_menu.cpp b/src/btop_menu.cpp index c6792cdf..595b5595 100644 --- a/src/btop_menu.cpp +++ b/src/btop_menu.cpp @@ -988,10 +988,10 @@ namespace Menu { int sizeError(const string& key) { if (redraw) { - vector cont_vec; - cont_vec.push_back(Fx::b + Theme::g("used")[100] + "Error:" + Theme::c("main_fg") + Fx::ub); - cont_vec.push_back("Terminal size to small to" + Fx::reset); - cont_vec.push_back("display menu or box!" + Fx::reset); + vector cont_vec { + Fx::b + Theme::g("used")[100] + "Error:" + Theme::c("main_fg") + Fx::ub, + "Terminal size to small to" + Fx::reset, + "display menu or box!" + Fx::reset }; messageBox = Menu::msgBox{45, 0, cont_vec, "error"}; Global::overlay = messageBox(); From 0f1bb2ac186b548062d0ae96f55167ef01af6025 Mon Sep 17 00:00:00 2001 From: bad code <> Date: Sun, 22 Sep 2024 16:44:29 +0200 Subject: [PATCH 10/14] Avoid string copy --- src/btop_theme.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/btop_theme.hpp b/src/btop_theme.hpp index dcc00e44..10a37cef 100644 --- a/src/btop_theme.hpp +++ b/src/btop_theme.hpp @@ -61,9 +61,9 @@ namespace Theme { inline const string& c(const string& name) { return colors.at(name); } //* Return array of escape codes for color gradient - inline const array& g(string name) { return gradients.at(name); } + inline const array& g(const string& name) { return gradients.at(name); } //* Return array of red, green and blue in decimal for color - inline const std::array& dec(string name) { return rgbs.at(name); } + inline const std::array& dec(const string& name) { return rgbs.at(name); } } From 4fa1a8a0892dbe9c7a9251840904593eb41b5559 Mon Sep 17 00:00:00 2001 From: bad code <> Date: Sun, 22 Sep 2024 16:44:34 +0200 Subject: [PATCH 11/14] Reduce variable scope --- src/btop_theme.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/btop_theme.cpp b/src/btop_theme.cpp index b8203695..d552fdaa 100644 --- a/src/btop_theme.cpp +++ b/src/btop_theme.cpp @@ -372,13 +372,13 @@ namespace Theme { //* Load a .theme file from disk auto loadFile(const string& filename) { - std::unordered_map theme_out; const fs::path filepath = filename; if (not fs::exists(filepath)) return Default_theme; std::ifstream themefile(filepath); if (themefile.good()) { + std::unordered_map theme_out; Logger::debug("Loading theme file: " + filename); while (not themefile.bad()) { if (themefile.peek() == '#') { From 5cf57128ceae3894a044fd4fc64b7b92515b967b Mon Sep 17 00:00:00 2001 From: bad code <> Date: Sun, 22 Sep 2024 16:44:38 +0200 Subject: [PATCH 12/14] Drop unused struct members --- src/linux/btop_collect.cpp | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/linux/btop_collect.cpp b/src/linux/btop_collect.cpp index 60aa74f9..85e9b834 100644 --- a/src/linux/btop_collect.cpp +++ b/src/linux/btop_collect.cpp @@ -99,9 +99,7 @@ namespace Cpu { struct Sensor { fs::path path; - string label; int64_t temp{}; - int64_t high{}; int64_t crit{}; }; @@ -479,10 +477,9 @@ namespace Cpu { const string label = readfile(fs::path(basepath + "label"), "temp" + to_string(file_id)); const string sensor_name = pname + "/" + label; const int64_t temp = stol(readfile(fs::path(basepath + "input"), "0")) / 1000; - const int64_t high = stol(readfile(fs::path(basepath + "max"), "80000")) / 1000; const int64_t crit = stol(readfile(fs::path(basepath + "crit"), "95000")) / 1000; - found_sensors[sensor_name] = {fs::path(basepath + "input"), label, temp, high, crit}; + found_sensors[sensor_name] = {fs::path(basepath + "input"), temp, crit}; if (not got_cpu and (label.starts_with("Package id") or label.starts_with("Tdie"))) { got_cpu = true; @@ -515,7 +512,7 @@ namespace Cpu { if (high < 1) high = 80; if (crit < 1) crit = 95; - found_sensors[sensor_name] = {basepath / "temp", label, temp, high, crit}; + found_sensors[sensor_name] = {basepath / "temp", temp, crit}; } } From 9c35b3881adfa5a21b6650386c20a1e5a9055a89 Mon Sep 17 00:00:00 2001 From: bad code <> Date: Sun, 22 Sep 2024 16:44:42 +0200 Subject: [PATCH 13/14] Avoid string copies The sensor data is not modified for the lifetime of the vector and outlives it. --- src/linux/btop_collect.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/linux/btop_collect.cpp b/src/linux/btop_collect.cpp index 85e9b834..552e146e 100644 --- a/src/linux/btop_collect.cpp +++ b/src/linux/btop_collect.cpp @@ -556,8 +556,7 @@ namespace Cpu { if (current_cpu.temp.at(0).size() > 20) current_cpu.temp.at(0).pop_front(); if (Config::getB("show_coretemp") and not cpu_temp_only) { - vector done; - for (const auto& sensor : core_sensors) { + for (vector done; const auto& sensor : core_sensors) { if (v_contains(done, sensor)) continue; found_sensors.at(sensor).temp = stol(readfile(found_sensors.at(sensor).path, "0")) / 1000; done.push_back(sensor); From 1454472d990ea2b2dc41e895cb402b9e0319af40 Mon Sep 17 00:00:00 2001 From: bad code <> Date: Sun, 22 Sep 2024 16:44:25 +0200 Subject: [PATCH 14/14] Avoid copies in ltrim() and ltrim() --- src/btop_draw.cpp | 6 +++--- src/btop_theme.cpp | 4 ++-- src/btop_tools.cpp | 18 ++++++++---------- src/btop_tools.hpp | 8 +++++--- 4 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/btop_draw.cpp b/src/btop_draw.cpp index 00de3ded..b519a3f2 100644 --- a/src/btop_draw.cpp +++ b/src/btop_draw.cpp @@ -1866,10 +1866,10 @@ namespace Proc { width_left -= (ulen(p.name) + 1); } if (width_left > 7) { - const string& cmd = width_left > 40 ? rtrim(p.cmd) : p.short_cmd; + const string_view cmd = width_left > 40 ? rtrim(p.cmd) : p.short_cmd; if (not cmd.empty() and cmd != p.name) { - out += g_color + '(' + uresize(cmd, width_left - 3, p_wide_cmd[p.pid]) + ") "; - width_left -= (ulen(cmd, true) + 3); + out += g_color + '(' + uresize(string{cmd}, width_left - 3, p_wide_cmd[p.pid]) + ") "; + width_left -= (ulen(string{cmd}, true) + 3); } } out += string(max(0, width_left), ' ') + Mv::to(y+2+lc, x+2+tree_size); diff --git a/src/btop_theme.cpp b/src/btop_theme.cpp index d552fdaa..f5a141b9 100644 --- a/src/btop_theme.cpp +++ b/src/btop_theme.cpp @@ -304,7 +304,7 @@ namespace Theme { for (const auto& [name, source_arr] : rgbs) { if (not name.ends_with("_start")) continue; - const string color_name = rtrim(name, "_start"); + const string color_name { rtrim(name, "_start") }; //? input_colors[start,mid,end][red,green,blue] const array, 3> input_colors = { @@ -357,7 +357,7 @@ namespace Theme { for (const auto& c : colors) { if (not c.first.ends_with("_start")) continue; - const string base_name = rtrim(c.first, "_start"); + const string base_name { rtrim(c.first, "_start") }; string section = "_start"; int split = colors.at(base_name + "_mid").empty() ? 50 : 33; for (int i : iota(0, 101)) { diff --git a/src/btop_tools.cpp b/src/btop_tools.cpp index 263009d7..aabb69d7 100644 --- a/src/btop_tools.cpp +++ b/src/btop_tools.cpp @@ -296,20 +296,18 @@ namespace Tools { return out; } - string ltrim(const string& str, const string& t_str) { - std::string_view str_v{str}; - while (str_v.starts_with(t_str)) - str_v.remove_prefix(t_str.size()); + string_view ltrim(string_view str, const string_view t_str) { + while (str.starts_with(t_str)) + str.remove_prefix(t_str.size()); - return string{str_v}; + return str; } - string rtrim(const string& str, const string& t_str) { - std::string_view str_v{str}; - while (str_v.ends_with(t_str)) - str_v.remove_suffix(t_str.size()); + string_view rtrim(string_view str, const string_view t_str) { + while (str.ends_with(t_str)) + str.remove_suffix(t_str.size()); - return string{str_v}; + return str; } auto ssplit(const string& str, const char& delim) -> vector { diff --git a/src/btop_tools.hpp b/src/btop_tools.hpp index 79cf5e00..6949f32a 100644 --- a/src/btop_tools.hpp +++ b/src/btop_tools.hpp @@ -30,6 +30,7 @@ tab-size = 4 #include #include #include +#include #include #include #include @@ -54,6 +55,7 @@ using std::array; using std::atomic; using std::string; using std::to_string; +using std::string_view; using std::tuple; using std::vector; using namespace fmt::literals; @@ -292,13 +294,13 @@ namespace Tools { } //* Left-trim from and return new string - string ltrim(const string& str, const string& t_str = " "); + string_view ltrim(string_view str, string_view t_str = " "); //* Right-trim from and return new string - string rtrim(const string& str, const string& t_str = " "); + string_view rtrim(string_view str, string_view t_str = " "); //* Left/right-trim from and return new string - inline string trim(const string& str, const string& t_str = " ") { + inline string_view trim(string_view str, string_view t_str = " ") { return ltrim(rtrim(str, t_str), t_str); }