-
Notifications
You must be signed in to change notification settings - Fork 13
Change std::map to std::unordered_map Where Relevant #472
Copy link
Copy link
Open
Labels
LOW-PRIORITYTag issues with low relevance or necessity for the momentTag issues with low relevance or necessity for the momentenhancementNext stage development or improvement of an existing feature or utility.Next stage development or improvement of an existing feature or utility.nice-to-haveGood feature/modification, but it doesn't impact the current state of the project's development.Good feature/modification, but it doesn't impact the current state of the project's development.performanceThis label is for all tasks related to improving, measuring and analysing Odin-data's performanceThis label is for all tasks related to improving, measuring and analysing Odin-data's performance
Milestone
Metadata
Metadata
Assignees
Labels
LOW-PRIORITYTag issues with low relevance or necessity for the momentTag issues with low relevance or necessity for the momentenhancementNext stage development or improvement of an existing feature or utility.Next stage development or improvement of an existing feature or utility.nice-to-haveGood feature/modification, but it doesn't impact the current state of the project's development.Good feature/modification, but it doesn't impact the current state of the project's development.performanceThis label is for all tasks related to improving, measuring and analysing Odin-data's performanceThis label is for all tasks related to improving, measuring and analysing Odin-data's performance
Type
Projects
Status
🤷♂️ Needs Triage
std::map's underlying container is a Red-Black tree, whilestd::unordered_mapis a hash-map.std::mapis necessary when we care about the ordering of elements within the container - it is a self-balancing data structure. When its key is anstd::string, it usesstrncmp()to balance its nodes.strncmp()is not a constant-time operation; it is based on the length - L, of the compared strings -O(L), while the node-balancing takesO(log[n]), in total(O(L)*(O(log[n]))).On the other hand,
std::unordered_map- as its name suggests is unordered, but gives us faster indexing, with an average constant timeO(1). Hashing the string keys will still take O(L), but access is simply an array index (assuming collisions don't occur).Carefully observe the instances of
std::mapto know if the goal was accessing an associated element (a function associated with a string) as opposed to maintaining an order. If the ordering is never utilised, convert tostd::unordered_map. The two containers use a similar API, so nothing should change in the call sites.