Skip to content

Commit

Permalink
Revert "Replace robin_hood map and set with STD alternative and add s…
Browse files Browse the repository at this point in the history
…afeVal() function for map/vector access with fallback"

This reverts commit 6c87ab6.
  • Loading branch information
aristocratos committed Dec 25, 2023
1 parent 6c87ab6 commit e15e0b7
Show file tree
Hide file tree
Showing 19 changed files with 214 additions and 261 deletions.
8 changes: 2 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,6 @@ CLANG_WORKS = false
GCC_WORKS = false
MIN_CLANG_VERSION = 16

ifeq ($(DEBUG),true)
override ADDFLAGS += -DBTOP_DEBUG
endif

#? Supported is Clang 16.0.0 and later
ifeq ($(CXX_IS_CLANG),true)
ifeq ($(shell $(CXX) --version | grep Apple >/dev/null 2>&1; echo $$?),0)
Expand Down Expand Up @@ -283,13 +279,13 @@ directories:
clean:
@printf "\033[1;91mRemoving: \033[1;97mbuilt objects...\033[0m\n"
@rm -rf $(BUILDDIR)
@test -e lib/rocm_smi_lib/build && cmake --build lib/rocm_smi_lib/build --target clean &> /dev/null || true
@cmake --build lib/rocm_smi_lib/build --target clean &> /dev/null || true

#? Clean Objects and Binaries
distclean: clean
@printf "\033[1;91mRemoving: \033[1;97mbuilt binaries...\033[0m\n"
@rm -rf $(TARGETDIR)
@test -e lib/rocm_smi_lib/build && rm -rf lib/rocm_smi_lib/build || true
@rm -rf lib/rocm_smi_lib/build

install:
@printf "\033[1;92mInstalling binary to: \033[1;97m$(DESTDIR)$(PREFIX)/bin/btop\n"
Expand Down
3 changes: 1 addition & 2 deletions src/btop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ tab-size = 4
#include <tuple>
#include <regex>
#include <chrono>
#include <utility>
#ifdef __APPLE__
#include <CoreFoundation/CoreFoundation.h>
#include <mach-o/dyld.h>
Expand Down Expand Up @@ -417,7 +416,7 @@ namespace Runner {
};

string debug_bg;
std::unordered_map<string, array<uint64_t, 2>> debug_times;
unordered_flat_map<string, array<uint64_t, 2>> debug_times;

class MyNumPunct : public std::numpunct<char>
{
Expand Down
13 changes: 6 additions & 7 deletions src/btop_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ tab-size = 4
#include <fstream>
#include <ranges>
#include <string_view>
#include <utility>

#include <fmt/core.h>

Expand Down Expand Up @@ -216,7 +215,7 @@ namespace Config {
#endif
};

std::unordered_map<std::string_view, string> strings = {
unordered_flat_map<std::string_view, string> strings = {
{"color_theme", "Default"},
{"shown_boxes", "cpu mem net proc"},
{"graph_symbol", "braille"},
Expand Down Expand Up @@ -252,9 +251,9 @@ namespace Config {
{"show_gpu_info", "Auto"}
#endif
};
std::unordered_map<std::string_view, string> stringsTmp;
unordered_flat_map<std::string_view, string> stringsTmp;

std::unordered_map<std::string_view, bool> bools = {
unordered_flat_map<std::string_view, bool> bools = {
{"theme_background", true},
{"truecolor", true},
{"rounded_corners", true},
Expand Down Expand Up @@ -305,9 +304,9 @@ namespace Config {
{"gpu_mirror_graph", true},
#endif
};
std::unordered_map<std::string_view, bool> boolsTmp;
unordered_flat_map<std::string_view, bool> boolsTmp;

std::unordered_map<std::string_view, int> ints = {
unordered_flat_map<std::string_view, int> ints = {
{"update_ms", 2000},
{"net_download", 100},
{"net_upload", 100},
Expand All @@ -318,7 +317,7 @@ namespace Config {
{"proc_selected", 0},
{"proc_last_selected", 0},
};
std::unordered_map<std::string_view, int> intsTmp;
unordered_flat_map<std::string_view, int> intsTmp;

bool _locked(const std::string_view name) {
atomic_wait(writelock, true);
Expand Down
15 changes: 8 additions & 7 deletions src/btop_config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,24 @@ tab-size = 4
#include <vector>
#include <filesystem>

#include <unordered_map>
#include <robin_hood.h>

using std::string;
using std::vector;
using robin_hood::unordered_flat_map;

//* Functions and variables for reading and writing the btop config file
namespace Config {

extern std::filesystem::path conf_dir;
extern std::filesystem::path conf_file;

extern std::unordered_map<std::string_view, string> strings;
extern std::unordered_map<std::string_view, string> stringsTmp;
extern std::unordered_map<std::string_view, bool> bools;
extern std::unordered_map<std::string_view, bool> boolsTmp;
extern std::unordered_map<std::string_view, int> ints;
extern std::unordered_map<std::string_view, int> intsTmp;
extern unordered_flat_map<std::string_view, string> strings;
extern unordered_flat_map<std::string_view, string> stringsTmp;
extern unordered_flat_map<std::string_view, bool> bools;
extern unordered_flat_map<std::string_view, bool> boolsTmp;
extern unordered_flat_map<std::string_view, int> ints;
extern unordered_flat_map<std::string_view, int> intsTmp;

const vector<string> valid_graph_symbols = { "braille", "block", "tty" };
const vector<string> valid_graph_symbols_def = { "default", "braille", "block", "tty" };
Expand Down
172 changes: 87 additions & 85 deletions src/btop_draw.cpp

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions src/btop_draw.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ tab-size = 4
#include <string>
#include <vector>
#include <array>
#include <unordered_map>
#include <robin_hood.h>
#include <deque>

using robin_hood::unordered_flat_map;
using std::array;
using std::deque;
using std::string;
Expand Down Expand Up @@ -107,7 +108,7 @@ namespace Draw {
long long offset;
long long last = 0, max_value = 0;
bool current = true, tty_mode = false;
std::unordered_map<bool, vector<string>> graphs = { {true, {}}, {false, {}}};
unordered_flat_map<bool, vector<string>> graphs = { {true, {}}, {false, {}}};

//* Create two representations of the graph to switch between to represent two values for each braille character
void _create(const deque<long long>& data, int data_offset);
Expand All @@ -134,6 +135,6 @@ namespace Draw {

namespace Proc {
extern Draw::TextEdit filter;
extern std::unordered_map<size_t, Draw::Graph> p_graphs;
extern std::unordered_map<size_t, int> p_counters;
extern unordered_flat_map<size_t, Draw::Graph> p_graphs;
extern unordered_flat_map<size_t, int> p_counters;
}
4 changes: 2 additions & 2 deletions src/btop_input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ namespace rng = std::ranges;
namespace Input {

//* Map for translating key codes to readable values
const std::unordered_map<string, string> Key_escapes = {
const unordered_flat_map<string, string> Key_escapes = {
{"\033", "escape"},
{"\n", "enter"},
{" ", "space"},
Expand Down Expand Up @@ -92,7 +92,7 @@ namespace Input {
std::atomic<bool> interrupt (false);
std::atomic<bool> polling (false);
array<int, 2> mouse_pos;
std::unordered_map<string, Mouse_loc> mouse_mappings;
unordered_flat_map<string, Mouse_loc> mouse_mappings;

deque<string> history(50, "");
string old_filter;
Expand Down
5 changes: 3 additions & 2 deletions src/btop_input.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ tab-size = 4
#include <string>
#include <atomic>
#include <array>
#include <unordered_map>
#include <robin_hood.h>
#include <deque>

using robin_hood::unordered_flat_map;
using std::array;
using std::atomic;
using std::deque;
Expand All @@ -43,7 +44,7 @@ namespace Input {
};

//? line, col, height, width
extern std::unordered_map<string, Mouse_loc> mouse_mappings;
extern unordered_flat_map<string, Mouse_loc> mouse_mappings;

extern atomic<bool> interrupt;
extern atomic<bool> polling;
Expand Down
7 changes: 4 additions & 3 deletions src/btop_menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ tab-size = 4
*/

#include <deque>
#include <unordered_map>
#include <robin_hood.h>
#include <array>
#include <signal.h>
#include <errno.h>
Expand All @@ -31,6 +31,7 @@ tab-size = 4
#include "btop_draw.hpp"
#include "btop_shared.hpp"

using robin_hood::unordered_flat_map;
using std::array;
using std::ceil;
using std::max;
Expand Down Expand Up @@ -122,7 +123,7 @@ namespace Menu {
#endif
};

std::unordered_map<string, Input::Mouse_loc> mouse_mappings;
unordered_flat_map<string, Input::Mouse_loc> mouse_mappings;

const array<array<string, 3>, 3> menu_normal = {
array<string, 3>{
Expand Down Expand Up @@ -1164,7 +1165,7 @@ namespace Menu {
static Draw::TextEdit editor;
static string warnings;
static bitset<8> selPred;
static const std::unordered_map<string, std::reference_wrapper<const vector<string>>> optionsList = {
static const unordered_flat_map<string, std::reference_wrapper<const vector<string>>> optionsList = {
{"color_theme", std::cref(Theme::themes)},
{"log_level", std::cref(Logger::log_levels)},
{"temp_scale", std::cref(Config::temp_scales)},
Expand Down
2 changes: 1 addition & 1 deletion src/btop_menu.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ namespace Menu {
extern bool redraw;

//? line, col, height, width
extern std::unordered_map<string, Input::Mouse_loc> mouse_mappings;
extern unordered_flat_map<string, Input::Mouse_loc> mouse_mappings;

//* Creates a message box centered on screen
//? Height of box is determined by size of content vector
Expand Down
2 changes: 1 addition & 1 deletion src/btop_shared.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ using namespace Tools;
namespace Gpu {
vector<string> gpu_names;
vector<int> gpu_b_height_offsets;
std::unordered_map<string, deque<long long>> shared_gpu_percent = {
unordered_flat_map<string, deque<long long>> shared_gpu_percent = {
{"gpu-average", {}},
{"gpu-vram-total", {}},
{"gpu-pwr-total", {}},
Expand Down
29 changes: 15 additions & 14 deletions src/btop_shared.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ tab-size = 4
#include <tuple>
#include <vector>
#include <ifaddrs.h>
#include <unordered_map>
#include <robin_hood.h>
#include <unistd.h>

using robin_hood::unordered_flat_map;
using std::array;
using std::atomic;
using std::deque;
Expand Down Expand Up @@ -97,7 +98,7 @@ namespace Gpu {
extern vector<int> gpu_b_height_offsets;
extern long long gpu_pwr_total_max;

extern std::unordered_map<string, deque<long long>> shared_gpu_percent; // averages, power/vram total
extern unordered_flat_map<string, deque<long long>> shared_gpu_percent; // averages, power/vram total

const array mem_names { "used"s, "free"s };

Expand All @@ -123,7 +124,7 @@ namespace Gpu {

//* Per-device container for GPU info
struct gpu_info {
std::unordered_map<string, deque<long long>> gpu_percent = {
unordered_flat_map<string, deque<long long>> gpu_percent = {
{"gpu-totals", {}},
{"gpu-vram-totals", {}},
{"gpu-pwr-totals", {}},
Expand Down Expand Up @@ -180,7 +181,7 @@ namespace Cpu {
extern tuple<int, long, string> current_bat;

struct cpu_info {
std::unordered_map<string, deque<long long>> cpu_percent = {
unordered_flat_map<string, deque<long long>> cpu_percent = {
{"total", {}},
{"user", {}},
{"nice", {}},
Expand All @@ -206,8 +207,8 @@ namespace Cpu {
string draw(const cpu_info& cpu, const vector<Gpu::gpu_info>& gpu, bool force_redraw = false, bool data_same = false);

//* Parse /proc/cpu info for mapping of core ids
auto get_core_mapping() -> std::unordered_map<int, int>;
extern std::unordered_map<int, int> core_mapping;
auto get_core_mapping() -> unordered_flat_map<int, int>;
extern unordered_flat_map<int, int> core_mapping;

auto get_cpuHz() -> string;

Expand Down Expand Up @@ -241,13 +242,13 @@ namespace Mem {
};

struct mem_info {
std::unordered_map<string, uint64_t> stats =
unordered_flat_map<string, uint64_t> stats =
{{"used", 0}, {"available", 0}, {"cached", 0}, {"free", 0},
{"swap_total", 0}, {"swap_used", 0}, {"swap_free", 0}};
std::unordered_map<string, deque<long long>> percent =
unordered_flat_map<string, deque<long long>> percent =
{{"used", {}}, {"available", {}}, {"cached", {}}, {"free", {}},
{"swap_total", {}}, {"swap_used", {}}, {"swap_free", {}}};
std::unordered_map<string, disk_info> disks;
unordered_flat_map<string, disk_info> disks;
vector<string> disks_order;
};

Expand All @@ -269,7 +270,7 @@ namespace Net {
extern string selected_iface;
extern vector<string> interfaces;
extern bool rescale;
extern std::unordered_map<string, uint64_t> graph_max;
extern unordered_flat_map<string, uint64_t> graph_max;

struct net_stat {
uint64_t speed{}; // defaults to 0
Expand All @@ -281,14 +282,14 @@ namespace Net {
};

struct net_info {
std::unordered_map<string, deque<long long>> bandwidth = { {"download", {}}, {"upload", {}} };
std::unordered_map<string, net_stat> stat = { {"download", {}}, {"upload", {}} };
unordered_flat_map<string, deque<long long>> bandwidth = { {"download", {}}, {"upload", {}} };
unordered_flat_map<string, net_stat> stat = { {"download", {}}, {"upload", {}} };
string ipv4{}; // defaults to ""
string ipv6{}; // defaults to ""
bool connected{}; // defaults to false
};

extern std::unordered_map<string, net_info> current_net;
extern unordered_flat_map<string, net_info> current_net;

//* Collect net upload/download stats
auto collect(bool no_update=false) -> net_info&;
Expand Down Expand Up @@ -321,7 +322,7 @@ namespace Proc {
};

//? Translation from process state char to explanative string
const std::unordered_map<char, string> proc_states = {
const unordered_flat_map<char, string> proc_states = {
{'R', "Running"},
{'S', "Sleeping"},
{'D', "Waiting"},
Expand Down
14 changes: 7 additions & 7 deletions src/btop_theme.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ namespace Theme {
fs::path theme_dir;
fs::path user_theme_dir;
vector<string> themes;
std::unordered_map<string, string> colors;
std::unordered_map<string, array<int, 3>> rgbs;
std::unordered_map<string, array<string, 101>> gradients;
unordered_flat_map<string, string> colors;
unordered_flat_map<string, array<int, 3>> rgbs;
unordered_flat_map<string, array<string, 101>> gradients;

const std::unordered_map<string, string> Default_theme = {
const unordered_flat_map<string, string> Default_theme = {
{ "main_bg", "#00" },
{ "main_fg", "#cc" },
{ "title", "#ee" },
Expand Down Expand Up @@ -91,7 +91,7 @@ namespace Theme {
{ "process_end", "#d45454" }
};

const std::unordered_map<string, string> TTY_theme = {
const unordered_flat_map<string, string> TTY_theme = {
{ "main_bg", "\x1b[0;40m" },
{ "main_fg", "\x1b[37m" },
{ "title", "\x1b[97m" },
Expand Down Expand Up @@ -224,7 +224,7 @@ namespace Theme {
}

//* Generate colors and rgb decimal vectors for the theme
void generateColors(const std::unordered_map<string, string>& source) {
void generateColors(const unordered_flat_map<string, string>& source) {
vector<string> t_rgb;
string depth;
bool t_to_256 = Config::getB("lowcolor");
Expand Down Expand Up @@ -372,7 +372,7 @@ namespace Theme {

//* Load a .theme file from disk
auto loadFile(const string& filename) {
std::unordered_map<string, string> theme_out;
unordered_flat_map<string, string> theme_out;
const fs::path filepath = filename;
if (not fs::exists(filepath))
return Default_theme;
Expand Down
Loading

0 comments on commit e15e0b7

Please sign in to comment.