Skip to content

Change std::map to std::unordered_map Where Relevant #472

@Ohisemega

Description

@Ohisemega

std::map's underlying container is a Red-Black tree, while std::unordered_map is a hash-map.
std::map is necessary when we care about the ordering of elements within the container - it is a self-balancing data structure. When its key is an std::string, it uses strncmp() 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 takes O(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 time O(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::map to 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 to std::unordered_map. The two containers use a similar API, so nothing should change in the call sites.

Metadata

Metadata

Assignees

No one assigned

    Labels

    LOW-PRIORITYTag issues with low relevance or necessity for the momentenhancementNext 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.performanceThis label is for all tasks related to improving, measuring and analysing Odin-data's performance

    Type

    Projects

    Status

    🤷‍♂️ Needs Triage

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions