Skip to content

Conversation

@chaowyc
Copy link

@chaowyc chaowyc commented Nov 12, 2025

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:

  • Interface compatibility verified with unit test
  • Performance benchmark confirms expected improvements
  • No API changes required (drop-in replacement)

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

  • feat: New feature (non-breaking change which adds functionality)
  • fix: Bug fix (non-breaking change which fixes an issue)
  • docs: Documentation only changes
  • style: Changes that do not affect the meaning of the code (white-space, formatting, missing semicolons, etc.)
  • refactor: Code change that neither fixes a bug nor adds a feature
  • perf: Code change that improves performance
  • test: Adding missing tests or correcting existing tests
  • build: Changes that affect the build system or external dependencies
  • ci: Changes to CI configuration files and scripts
  • chore: Other changes that don't modify src or test files
  • revert: Reverts a previous commit
  • BREAKING CHANGE: Introduces a breaking change (can be combined with any type above)

Checklist

  • Code follows the style guidelines of this project
  • Code has been self-reviewed
  • Code has been commented, particularly in hard-to-understand areas
  • Code docstring/documentation-blocks for new or existing methods/components have been added or updated
  • Unit tests have been added or updated for any new or modified functionality

AI Usage

  • None: No AI tools were used in creating this PR
  • Light: AI provided minor assistance (formatting, simple suggestions)
  • Moderate: AI helped with code generation or debugging specific parts
  • Heavy: AI generated most or all of the code changes

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
@sonarqubecloud
Copy link

@codecov
Copy link

codecov bot commented Nov 12, 2025

Bundle Report

Bundle size has no change ✅

@codecov
Copy link

codecov bot commented Nov 12, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 14.22%. Comparing base (1d6d916) to head (8934dd1).
⚠️ Report is 2 commits behind head on master.
✅ All tests successful. No failed tests found.

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     
Flag Coverage Δ
FreeBSD-14.3-aarch64 ?
FreeBSD-14.3-amd64 13.55% <ø> (+<0.01%) ⬆️
Linux-AppImage 11.55% <ø> (-0.02%) ⬇️
Windows-AMD64 13.34% <ø> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
src/stream.cpp 2.84% <ø> (ø)
src/utility.h 61.35% <ø> (+2.00%) ⬆️

... and 74 files with indirect coverage changes

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.

1 participant