diff --git a/src/builtin_argparse.cpp b/src/builtin_argparse.cpp index dea5e1825..8ac8fa515 100644 --- a/src/builtin_argparse.cpp +++ b/src/builtin_argparse.cpp @@ -12,11 +12,11 @@ #include #include #include -#include #include #include #include #include +#include #include #include @@ -65,8 +65,8 @@ class argparse_cmd_opts_t { wcstring name = L"argparse"; wcstring_list_t raw_exclusive_flags; wcstring_list_t argv; - std::map options; - std::map long_to_short_flag; + std::unordered_map options; + std::unordered_map long_to_short_flag; std::vector> exclusive_flag_sets; ~argparse_cmd_opts_t() { diff --git a/src/builtin_string.cpp b/src/builtin_string.cpp index 03dfa1f07..a1e199ac0 100644 --- a/src/builtin_string.cpp +++ b/src/builtin_string.cpp @@ -17,9 +17,9 @@ #include #include #include -#include #include #include +#include #include #include @@ -387,7 +387,7 @@ static const struct woption long_options[] = {{L"all", no_argument, NULL, 'a'}, {L"style", required_argument, NULL, 1}, {NULL, 0, NULL, 0}}; -static std::map flag_to_function = { +static std::unordered_map flag_to_function = { {'N', handle_flag_N}, {'a', handle_flag_a}, {'c', handle_flag_c}, {'e', handle_flag_e}, {'f', handle_flag_f}, {'i', handle_flag_i}, {'l', handle_flag_l}, {'m', handle_flag_m}, {'n', handle_flag_n}, {'q', handle_flag_q}, {'r', handle_flag_r}, {'s', handle_flag_s}, diff --git a/src/common.h b/src/common.h index 3ef074bd0..3846e32e2 100644 --- a/src/common.h +++ b/src/common.h @@ -832,3 +832,19 @@ enum { #endif #endif + +// Custom hash function used by unordered_map/unordered_set when key is const +#ifndef CONST_WCSTRING_HASH +#define CONST_WCSTRING_HASH 1 +namespace std { + template <> + struct hash + { + std::size_t operator()(const wcstring& w) const + { + std::hash hasher; + return hasher((wcstring) w); + } + }; +} +#endif diff --git a/src/complete.cpp b/src/complete.cpp index d570c4ce8..48c54b973 100644 --- a/src/complete.cpp +++ b/src/complete.cpp @@ -16,11 +16,11 @@ #include #include #include -#include #include #include #include #include +#include #include #include @@ -292,7 +292,7 @@ class completer_t { /// Table of completions conditions that have already been tested and the corresponding test /// results. - typedef std::map condition_cache_t; + typedef std::unordered_map condition_cache_t; condition_cache_t condition_cache; enum complete_type_t { COMPLETE_DEFAULT, COMPLETE_AUTOSUGGEST }; @@ -596,7 +596,7 @@ void completer_t::complete_cmd_desc(const wcstring &str) { wcstring lookup_cmd(L"__fish_describe_command "); lookup_cmd.append(escape_string(cmd_start, 1)); - std::map lookup; + std::unordered_map lookup; // First locate a list of possible descriptions using a single call to apropos or a direct // search if we know the location of the whatis database. This can take some time on slower @@ -634,7 +634,7 @@ void completer_t::complete_cmd_desc(const wcstring &str) { const wcstring &el = completion.completion; if (el.empty()) continue; - std::map::iterator new_desc_iter = lookup.find(el); + auto new_desc_iter = lookup.find(el); if (new_desc_iter != lookup.end()) completion.description = new_desc_iter->second; } } @@ -1553,7 +1553,7 @@ wcstring complete_print() { /// Completion "wrapper" support. The map goes from wrapping-command to wrapped-command-list. static std::mutex wrapper_lock; -typedef std::map wrapper_map_t; +typedef std::unordered_map wrapper_map_t; static wrapper_map_t &wrap_map() { ASSERT_IS_LOCKED(wrapper_lock); // A pointer is a little more efficient than an object as a static because we can elide the diff --git a/src/env.cpp b/src/env.cpp index 2b183d6d7..0030e3aa0 100644 --- a/src/env.cpp +++ b/src/env.cpp @@ -30,9 +30,9 @@ #endif #include -#include #include #include +#include #include #include @@ -85,7 +85,7 @@ bool term_has_xn = false; /// found in `TERMINFO_DIRS` we don't to call `handle_curses()` before we've imported the latter. static bool env_initialized = false; -typedef std::map var_dispatch_table_t; +typedef std::unordered_map var_dispatch_table_t; var_dispatch_table_t var_dispatch_table; /// List of all locale environment variable names that might trigger (re)initializing the locale @@ -1596,7 +1596,7 @@ env_var_t env_vars_snapshot_t::get(const wcstring &key) const { if (this->is_current()) { return env_get(key); } - std::map::const_iterator iter = vars.find(key); + auto iter = vars.find(key); return iter == vars.end() ? missing_var : env_var_t(iter->second); } diff --git a/src/expand.cpp b/src/expand.cpp index 50168222f..733d11422 100644 --- a/src/expand.cpp +++ b/src/expand.cpp @@ -27,9 +27,9 @@ #include #include -#include #include // IWYU pragma: keep #include +#include #include #include @@ -1566,7 +1566,7 @@ bool fish_xdm_login_hack_hack_hack_hack(std::vector *cmds, int argc return result; } -std::map abbreviations; +std::unordered_map abbreviations; void update_abbr_cache(const wchar_t *op, const wcstring &varname) { wcstring abbr; if (!unescape_string(varname.substr(wcslen(L"_fish_abbr_")), &abbr, 0, STRING_STYLE_VAR)) { diff --git a/src/function.cpp b/src/function.cpp index 04dd5dfb9..6571f3081 100644 --- a/src/function.cpp +++ b/src/function.cpp @@ -14,6 +14,7 @@ #include #include #include +#include #include #include "autoload.h" @@ -28,7 +29,7 @@ #include "wutil.h" // IWYU pragma: keep /// Table containing all functions. -typedef std::map function_map_t; +typedef std::unordered_map function_map_t; static function_map_t loaded_functions; /// Functions that shouldn't be autoloaded (anymore). diff --git a/src/highlight.cpp b/src/highlight.cpp index adefd969b..bbd3abac9 100644 --- a/src/highlight.cpp +++ b/src/highlight.cpp @@ -9,11 +9,11 @@ #include #include -#include #include #include #include #include +#include #include #include "builtin.h" @@ -67,7 +67,7 @@ static const wchar_t *const highlight_var[] = {L"fish_color_normal", /// Returns: /// false: the filesystem is not case insensitive /// true: the file system is case insensitive -typedef std::map case_sensitivity_cache_t; +typedef std::unordered_map case_sensitivity_cache_t; bool fs_is_case_insensitive(const wcstring &path, int fd, case_sensitivity_cache_t &case_sensitivity_cache) { bool result = false; diff --git a/src/lru.h b/src/lru.h index c5a9bb994..39b4a683d 100644 --- a/src/lru.h +++ b/src/lru.h @@ -4,7 +4,7 @@ #include -#include +#include #include "common.h" @@ -22,8 +22,6 @@ template class lru_cache_t { struct lru_node_t; - typedef typename std::map::iterator node_iter_t; - struct lru_link_t { // Our doubly linked list // The base class is used for the mouth only @@ -47,6 +45,8 @@ class lru_cache_t { explicit lru_node_t(const CONTENTS &v) : value(std::move(v)) {} }; + typedef typename std::unordered_map::iterator node_iter_t; + // Max node count. This may be (transiently) exceeded by add_node_without_eviction, which is // used from background threads. const size_t max_node_count; @@ -54,7 +54,7 @@ class lru_cache_t { // All of our nodes // Note that our linked list contains pointers to these nodes in the map // We are dependent on the iterator-noninvalidation guarantees of std::map - std::map node_map; + std::unordered_map node_map; // Head of the linked list // The list is circular! diff --git a/src/pager.cpp b/src/pager.cpp index 2dbc95d93..c53e928fe 100644 --- a/src/pager.cpp +++ b/src/pager.cpp @@ -6,8 +6,8 @@ #include #include -#include #include +#include #include #include "common.h" @@ -267,7 +267,7 @@ static void mangle_1_completion_description(wcstring *str) { static void join_completions(comp_info_list_t *comps) { // A map from description to index in the completion list of the element with that description. // The indexes are stored +1. - std::map desc_table; + std::unordered_map desc_table; // Note that we mutate the completion list as we go, so the size changes. for (size_t i = 0; i < comps->size(); i++) { diff --git a/src/screen.h b/src/screen.h index f7960a6a0..c06ffaf12 100644 --- a/src/screen.h +++ b/src/screen.h @@ -16,9 +16,9 @@ #include #include -#include #include #include +#include #include #include "common.h" @@ -214,7 +214,7 @@ class cached_esc_sequences_t { // sequence lengths we've actually cached to avoid checking for matches of lengths we know are // not in our cache. std::vector lengths; - std::map lengths_match_count; + std::unordered_map lengths_match_count; size_t cache_hits; public: diff --git a/src/wutil.cpp b/src/wutil.cpp index 928557c00..14b63875c 100644 --- a/src/wutil.cpp +++ b/src/wutil.cpp @@ -17,8 +17,8 @@ #include #include -#include #include +#include #include "common.h" #include "fallback.h" // IWYU pragma: keep @@ -38,7 +38,7 @@ const file_id_t kInvalidFileID = {(dev_t)-1LL, (ino_t)-1LL, (uint64_t)-1LL, -1, #endif /// Map used as cache by wgettext. -static owning_lock> wgettext_map; +static owning_lock> wgettext_map; bool wreaddir_resolving(DIR *dir, const wcstring &dir_path, wcstring &out_name, bool *out_is_dir) { struct dirent d;