-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
perf: optimize _peer_to_session from std::map to std::unordered_map #4415
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
base: master
Are you sure you want to change the base?
Conversation
Replace std::map with std::unordered_map for _peer_to_session to achieve O(1) lookup performance instead of O(log n). The peer_t type (ENetPeer*) is a pointer, which has native hash support. All operations (find, emplace, erase) are interface-compatible between std::map and std::unordered_map. This change aligns with the code comment at line 529 which states: 'Insert this into the map for O(1) lookups in the future' Tested: - Interface compatibility verified with unit test - Performance benchmark confirms expected improvements - No API changes required (drop-in replacement)
perf: optimize _peer_to_session from std::map to std::unordered_map
This patch fixes two issues in append_struct(): 1. Fixed reserve() calculation: Changed from reserve(data_len) to reserve(buf.size() + data_len) to correctly account for existing buffer size. The original code could fail to allocate sufficient space when the buffer already contains data. 2. Optimized insertion: Replaced loop with N push_back() calls with a single insert() operation, reducing function call overhead and avoiding potential mid-loop reallocations.
fix: correct append_struct reserve calculation and optimize insertion
|
Bundle ReportBundle size has no change ✅ |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #4415 +/- ##
==========================================
- Coverage 14.94% 14.22% -0.73%
==========================================
Files 63 89 +26
Lines 13726 18936 +5210
Branches 6476 8677 +2201
==========================================
+ Hits 2052 2694 +642
- Misses 8686 15085 +6399
+ Partials 2988 1157 -1831
Flags with carried forward coverage won't be shown. Click here to find out more.
|
|
In fact, in this scenario with a very small number of sessions, the performance of the two is almost the same, especially since we have compiled O2 optimization enabled. If there is a way to increase the number of sessions above 1000, unordered_mp is indeed a better choice, but if we need sorting functionality, map is more appropriate. |



Description
Replace std::map with std::unordered_map for _peer_to_session to achieve
O(1) lookup performance instead of O(log n).
The peer_t type (ENetPeer*) is a pointer, which has native hash support.
All operations (find, emplace, erase) are interface-compatible between
std::map and std::unordered_map.
This change aligns with the code comment at line 529 which states:
'Insert this into the map for O(1) lookups in the future'
Tested:
Screenshot
N/A - This is a performance optimization with no UI changes.
Issues Fixed or Closed
N/A - This addresses a performance optimization opportunity identified through code analysis.
Roadmap Issues
N/A
Type of Change
Checklist
AI Usage