Skip to content

Commit

Permalink
perf: change zone positions to phmap::flat_hash_set (opentibiabr#3210)
Browse files Browse the repository at this point in the history
This replaces `std::unordered_set` with `phmap::flat_hash_set` in the
`Zone` class to improve performance by reducing hash collisions and
optimizing memory usage.

Motivation:

The use of `std::unordered_set` for the `positions` attribute caused
performance issues due to hash collisions, especially when managing
large datasets or heavily clustered data. Collisions result in increased
lookup, insertion, and deletion times as the hash table degrades into a
linked list traversal for conflicting buckets.

Switching to `phmap::flat_hash_set` provides several benefits:
1. Reduced hash collisions: The hashing strategy used by
`phmap::flat_hash_set` significantly reduces the likelihood of
collisions, improving lookup and insertion performance.
2. Improved memory locality: Unlike `std::unordered_set`, which uses
separate allocations for each bucket, `flat_hash_set` uses a contiguous
memory layout, enhancing cache efficiency and reducing overhead.
3. Faster operations: Benchmarks show that `flat_hash_set`
outperforms `std::unordered_set` in scenarios with frequent insertions,
lookups, and deletions due to its optimized design.
  • Loading branch information
dudantas authored and vllworldbuilding committed Jan 10, 2025
1 parent 595a31c commit bd79926
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion src/game/zones/zone.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ class Zone {
Position removeDestination = Position();
std::string name;
std::string monsterVariant;
std::unordered_set<Position> positions;
phmap::flat_hash_set<Position> positions;
uint32_t id = 0; // ID 0 is used in zones created dynamically from lua. The map editor uses IDs starting from 1 (automatically generated).

weak::set<Item> itemsCache;
Expand Down

0 comments on commit bd79926

Please sign in to comment.