Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: npc/monster storage with vector indexing #3209

Merged
merged 2 commits into from
Jan 4, 2025

Conversation

dudantas
Copy link
Member

@dudantas dudantas commented Jan 4, 2025

Description

This PR modifies the internal storage of NPCs and monsters in the Game class from std::unordered_map to a combination of std::vector and std::unordered_map indexes for improved lookup efficiency and better memory locality.

Resolves this:
image

Motivation

Previously, every time a search was performed for an NPC or a monster, the unordered map was iterated, and even though it provides average O(1) complexity for lookups, the actual cost becomes significant when handling a large dataset (e.g., 80,000 monsters). This cost arises from the overhead of hashing and poor memory locality, as unordered maps store elements in a hash table.

Switching to a vector with index-based lookup improves the following:

  1. Memory locality: Vectors store elements contiguously in memory, improving cache efficiency when accessing elements sequentially or repeatedly.
  2. Lookup efficiency: Using an unordered map as an index to the vector allows leveraging direct index-based access (constant time) to the actual data, combining the best of both structures.

Behaviour

Actual

  • Lookups for monsters or NPCs require multiple hash operations and traversals, resulting in higher overhead for large datasets.

Expected

  • Lookups are performed efficiently by first finding the index from the unordered map and then directly accessing the data in the vector.

Type of change

  • Performance improvement (non-breaking change which improves efficiency)

How Has This Been Tested

  • Performed stress tests with datasets containing 80,000+ NPCs and monsters to compare the lookup time and resource utilization.
  • Verified that all existing functionality works without modifications.

Tests performed:

  • Lookups for existing and non-existing monsters/NPCs.
  • Addition and removal of monsters/NPCs.
  • Ensured consistency between the unordered map index and the vector.

Checklist

  • My code follows the style guidelines of this project.
  • I have performed a self-review of my own code.
  • I checked the PR checks reports.
  • I have commented my code, particularly in hard-to-understand areas.
  • My changes generate no new warnings.
  • I have added tests that prove my fix is effective or that my feature works.

@dudantas dudantas force-pushed the dudantas/perf-monsters-and-npcs-search branch from aedfdfd to 52a83ce Compare January 4, 2025 03:15
This modifies the internal storage of NPCs and monsters in the `Game` class from `std::unordered_map` to a combination of `std::vector` and `std::unordered_map` indexes for improved lookup efficiency and better memory locality.
@dudantas dudantas force-pushed the dudantas/perf-monsters-and-npcs-search branch from 52a83ce to d739b5d Compare January 4, 2025 03:28
@majestyotbr majestyotbr merged commit e7c1591 into main Jan 4, 2025
31 of 32 checks passed
@majestyotbr majestyotbr deleted the dudantas/perf-monsters-and-npcs-search branch January 4, 2025 23:58
Copy link

sonarqubecloud bot commented Jan 4, 2025

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants