Switch from std::map<> to std::unordered_map<> where possible

Didn't switch env_var_t map because it seems to be mostly iterated in
order, but that decision may be revisited at a later date.
This commit is contained in:
Mahmoud Al-Qudsi 2017-08-19 11:55:06 -05:00
parent 0dce9a2114
commit 61b4900a70
12 changed files with 45 additions and 28 deletions

View file

@ -12,11 +12,11 @@
#include <functional> #include <functional>
#include <iostream> #include <iostream>
#include <iterator> #include <iterator>
#include <map>
#include <memory> #include <memory>
#include <sstream> #include <sstream>
#include <string> #include <string>
#include <type_traits> #include <type_traits>
#include <unordered_map>
#include <utility> #include <utility>
#include <vector> #include <vector>
@ -65,8 +65,8 @@ class argparse_cmd_opts_t {
wcstring name = L"argparse"; wcstring name = L"argparse";
wcstring_list_t raw_exclusive_flags; wcstring_list_t raw_exclusive_flags;
wcstring_list_t argv; wcstring_list_t argv;
std::map<wchar_t, option_spec_t *> options; std::unordered_map<wchar_t, option_spec_t *> options;
std::map<wcstring, wchar_t> long_to_short_flag; std::unordered_map<wcstring, wchar_t> long_to_short_flag;
std::vector<std::vector<wchar_t>> exclusive_flag_sets; std::vector<std::vector<wchar_t>> exclusive_flag_sets;
~argparse_cmd_opts_t() { ~argparse_cmd_opts_t() {

View file

@ -17,9 +17,9 @@
#include <algorithm> #include <algorithm>
#include <cwctype> #include <cwctype>
#include <iterator> #include <iterator>
#include <map>
#include <memory> #include <memory>
#include <string> #include <string>
#include <unordered_map>
#include <utility> #include <utility>
#include <vector> #include <vector>
@ -387,7 +387,7 @@ static const struct woption long_options[] = {{L"all", no_argument, NULL, 'a'},
{L"style", required_argument, NULL, 1}, {L"style", required_argument, NULL, 1},
{NULL, 0, NULL, 0}}; {NULL, 0, NULL, 0}};
static std::map<char, decltype(*handle_flag_N)> flag_to_function = { static std::unordered_map<char, decltype(*handle_flag_N)> flag_to_function = {
{'N', handle_flag_N}, {'a', handle_flag_a}, {'c', handle_flag_c}, {'e', handle_flag_e}, {'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}, {'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}, {'n', handle_flag_n}, {'q', handle_flag_q}, {'r', handle_flag_r}, {'s', handle_flag_s},

View file

@ -832,3 +832,19 @@ enum {
#endif #endif
#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<const wcstring>
{
std::size_t operator()(const wcstring& w) const
{
std::hash<wcstring> hasher;
return hasher((wcstring) w);
}
};
}
#endif

View file

@ -16,11 +16,11 @@
#include <functional> #include <functional>
#include <iterator> #include <iterator>
#include <list> #include <list>
#include <map>
#include <memory> #include <memory>
#include <set> #include <set>
#include <string> #include <string>
#include <type_traits> #include <type_traits>
#include <unordered_map>
#include <unordered_set> #include <unordered_set>
#include <utility> #include <utility>
@ -292,7 +292,7 @@ class completer_t {
/// Table of completions conditions that have already been tested and the corresponding test /// Table of completions conditions that have already been tested and the corresponding test
/// results. /// results.
typedef std::map<wcstring, bool> condition_cache_t; typedef std::unordered_map<wcstring, bool> condition_cache_t;
condition_cache_t condition_cache; condition_cache_t condition_cache;
enum complete_type_t { COMPLETE_DEFAULT, COMPLETE_AUTOSUGGEST }; 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 "); wcstring lookup_cmd(L"__fish_describe_command ");
lookup_cmd.append(escape_string(cmd_start, 1)); lookup_cmd.append(escape_string(cmd_start, 1));
std::map<wcstring, wcstring> lookup; std::unordered_map<wcstring, wcstring> lookup;
// First locate a list of possible descriptions using a single call to apropos or a direct // 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 // 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; const wcstring &el = completion.completion;
if (el.empty()) continue; if (el.empty()) continue;
std::map<wcstring, wcstring>::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; 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. /// Completion "wrapper" support. The map goes from wrapping-command to wrapped-command-list.
static std::mutex wrapper_lock; static std::mutex wrapper_lock;
typedef std::map<wcstring, wcstring_list_t> wrapper_map_t; typedef std::unordered_map<wcstring, wcstring_list_t> wrapper_map_t;
static wrapper_map_t &wrap_map() { static wrapper_map_t &wrap_map() {
ASSERT_IS_LOCKED(wrapper_lock); ASSERT_IS_LOCKED(wrapper_lock);
// A pointer is a little more efficient than an object as a static because we can elide the // A pointer is a little more efficient than an object as a static because we can elide the

View file

@ -30,9 +30,9 @@
#endif #endif
#include <algorithm> #include <algorithm>
#include <map>
#include <set> #include <set>
#include <type_traits> #include <type_traits>
#include <unordered_map>
#include <utility> #include <utility>
#include <vector> #include <vector>
@ -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. /// found in `TERMINFO_DIRS` we don't to call `handle_curses()` before we've imported the latter.
static bool env_initialized = false; static bool env_initialized = false;
typedef std::map<const wcstring, void (*)(const wcstring &, const wcstring &)> var_dispatch_table_t; typedef std::unordered_map<const wcstring, void (*)(const wcstring &, const wcstring &)> var_dispatch_table_t;
var_dispatch_table_t var_dispatch_table; var_dispatch_table_t var_dispatch_table;
/// List of all locale environment variable names that might trigger (re)initializing the locale /// 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()) { if (this->is_current()) {
return env_get(key); return env_get(key);
} }
std::map<wcstring, env_var_t>::const_iterator iter = vars.find(key); auto iter = vars.find(key);
return iter == vars.end() ? missing_var : env_var_t(iter->second); return iter == vars.end() ? missing_var : env_var_t(iter->second);
} }

View file

@ -27,9 +27,9 @@
#include <algorithm> #include <algorithm>
#include <functional> #include <functional>
#include <map>
#include <memory> // IWYU pragma: keep #include <memory> // IWYU pragma: keep
#include <type_traits> #include <type_traits>
#include <unordered_map>
#include <utility> #include <utility>
#include <vector> #include <vector>
@ -1566,7 +1566,7 @@ bool fish_xdm_login_hack_hack_hack_hack(std::vector<std::string> *cmds, int argc
return result; return result;
} }
std::map<const wcstring, const wcstring> abbreviations; std::unordered_map<const wcstring, const wcstring> abbreviations;
void update_abbr_cache(const wchar_t *op, const wcstring &varname) { void update_abbr_cache(const wchar_t *op, const wcstring &varname) {
wcstring abbr; wcstring abbr;
if (!unescape_string(varname.substr(wcslen(L"_fish_abbr_")), &abbr, 0, STRING_STYLE_VAR)) { if (!unescape_string(varname.substr(wcslen(L"_fish_abbr_")), &abbr, 0, STRING_STYLE_VAR)) {

View file

@ -14,6 +14,7 @@
#include <memory> #include <memory>
#include <set> #include <set>
#include <string> #include <string>
#include <unordered_map>
#include <utility> #include <utility>
#include "autoload.h" #include "autoload.h"
@ -28,7 +29,7 @@
#include "wutil.h" // IWYU pragma: keep #include "wutil.h" // IWYU pragma: keep
/// Table containing all functions. /// Table containing all functions.
typedef std::map<wcstring, function_info_t> function_map_t; typedef std::unordered_map<wcstring, function_info_t> function_map_t;
static function_map_t loaded_functions; static function_map_t loaded_functions;
/// Functions that shouldn't be autoloaded (anymore). /// Functions that shouldn't be autoloaded (anymore).

View file

@ -9,11 +9,11 @@
#include <wchar.h> #include <wchar.h>
#include <algorithm> #include <algorithm>
#include <map>
#include <memory> #include <memory>
#include <set> #include <set>
#include <string> #include <string>
#include <type_traits> #include <type_traits>
#include <unordered_map>
#include <utility> #include <utility>
#include "builtin.h" #include "builtin.h"
@ -67,7 +67,7 @@ static const wchar_t *const highlight_var[] = {L"fish_color_normal",
/// Returns: /// Returns:
/// false: the filesystem is not case insensitive /// false: the filesystem is not case insensitive
/// true: the file system is case insensitive /// true: the file system is case insensitive
typedef std::map<wcstring, bool> case_sensitivity_cache_t; typedef std::unordered_map<wcstring, bool> case_sensitivity_cache_t;
bool fs_is_case_insensitive(const wcstring &path, int fd, bool fs_is_case_insensitive(const wcstring &path, int fd,
case_sensitivity_cache_t &case_sensitivity_cache) { case_sensitivity_cache_t &case_sensitivity_cache) {
bool result = false; bool result = false;

View file

@ -4,7 +4,7 @@
#include <wchar.h> #include <wchar.h>
#include <map> #include <unordered_map>
#include "common.h" #include "common.h"
@ -22,8 +22,6 @@
template <class DERIVED, class CONTENTS> template <class DERIVED, class CONTENTS>
class lru_cache_t { class lru_cache_t {
struct lru_node_t; struct lru_node_t;
typedef typename std::map<wcstring, lru_node_t>::iterator node_iter_t;
struct lru_link_t { struct lru_link_t {
// Our doubly linked list // Our doubly linked list
// The base class is used for the mouth only // 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)) {} explicit lru_node_t(const CONTENTS &v) : value(std::move(v)) {}
}; };
typedef typename std::unordered_map<wcstring, lru_node_t>::iterator node_iter_t;
// Max node count. This may be (transiently) exceeded by add_node_without_eviction, which is // Max node count. This may be (transiently) exceeded by add_node_without_eviction, which is
// used from background threads. // used from background threads.
const size_t max_node_count; const size_t max_node_count;
@ -54,7 +54,7 @@ class lru_cache_t {
// All of our nodes // All of our nodes
// Note that our linked list contains pointers to these nodes in the map // Note that our linked list contains pointers to these nodes in the map
// We are dependent on the iterator-noninvalidation guarantees of std::map // We are dependent on the iterator-noninvalidation guarantees of std::map
std::map<wcstring, lru_node_t> node_map; std::unordered_map<wcstring, lru_node_t> node_map;
// Head of the linked list // Head of the linked list
// The list is circular! // The list is circular!

View file

@ -6,8 +6,8 @@
#include <wctype.h> #include <wctype.h>
#include <algorithm> #include <algorithm>
#include <map>
#include <numeric> #include <numeric>
#include <unordered_map>
#include <vector> #include <vector>
#include "common.h" #include "common.h"
@ -267,7 +267,7 @@ static void mangle_1_completion_description(wcstring *str) {
static void join_completions(comp_info_list_t *comps) { 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. // A map from description to index in the completion list of the element with that description.
// The indexes are stored +1. // The indexes are stored +1.
std::map<wcstring, size_t> desc_table; std::unordered_map<wcstring, size_t> desc_table;
// Note that we mutate the completion list as we go, so the size changes. // Note that we mutate the completion list as we go, so the size changes.
for (size_t i = 0; i < comps->size(); i++) { for (size_t i = 0; i < comps->size(); i++) {

View file

@ -16,9 +16,9 @@
#include <algorithm> #include <algorithm>
#include <cstddef> #include <cstddef>
#include <map>
#include <memory> #include <memory>
#include <set> #include <set>
#include <unordered_map>
#include <vector> #include <vector>
#include "common.h" #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 // sequence lengths we've actually cached to avoid checking for matches of lengths we know are
// not in our cache. // not in our cache.
std::vector<size_t> lengths; std::vector<size_t> lengths;
std::map<size_t, size_t> lengths_match_count; std::unordered_map<size_t, size_t> lengths_match_count;
size_t cache_hits; size_t cache_hits;
public: public:

View file

@ -17,8 +17,8 @@
#include <wchar.h> #include <wchar.h>
#include <wctype.h> #include <wctype.h>
#include <map>
#include <string> #include <string>
#include <unordered_map>
#include "common.h" #include "common.h"
#include "fallback.h" // IWYU pragma: keep #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 #endif
/// Map used as cache by wgettext. /// Map used as cache by wgettext.
static owning_lock<std::map<wcstring, wcstring>> wgettext_map; static owning_lock<std::unordered_map<wcstring, wcstring>> wgettext_map;
bool wreaddir_resolving(DIR *dir, const wcstring &dir_path, wcstring &out_name, bool *out_is_dir) { bool wreaddir_resolving(DIR *dir, const wcstring &dir_path, wcstring &out_name, bool *out_is_dir) {
struct dirent d; struct dirent d;