Skip to content

Commit

Permalink
replace some std::map with std::unordered_map
Browse files Browse the repository at this point in the history
  • Loading branch information
cnbatch committed Oct 10, 2023
1 parent 8389b43 commit f319f8a
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/networks/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1110,7 +1110,7 @@ void client_mode::process_disconnect(uint32_t conv)
std::shared_ptr<kcp_mappings> kcp_mappings_ptr = kcp_channel_iter->second;
std::shared_ptr<KCP::KCP> kcp_ptr = kcp_mappings_ptr->egress_kcp;

if (std::scoped_lock locker_expiring_kcp{ mutex_expiring_kcp }; expiring_kcp.find(kcp_ptr) == expiring_kcp.end())
if (std::scoped_lock locker_expiring_kcp{ mutex_expiring_kcp }; expiring_kcp.find(kcp_mappings_ptr) == expiring_kcp.end())
expiring_kcp.insert({ kcp_mappings_ptr, packet::right_now() });

if (std::scoped_lock locker_kcp_keepalive{mutex_kcp_keepalive}; kcp_keepalive.find(kcp_ptr) != kcp_keepalive.end())
Expand Down
14 changes: 7 additions & 7 deletions src/networks/client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,25 @@ class client_mode
std::unique_ptr<udp_server> udp_access_point;

std::shared_mutex mutex_handshakes;
std::map<kcp_mappings*, std::shared_ptr<kcp_mappings>> handshakes;
std::unordered_map<kcp_mappings*, std::shared_ptr<kcp_mappings>> handshakes;

std::shared_mutex mutex_udp_local_session_map_to_kcp;
std::map<udp::endpoint, std::shared_ptr<kcp_mappings>> udp_local_session_map_to_kcp;

std::mutex mutex_udp_address_map_to_handshake;
std::map<udp::endpoint, std::shared_ptr<kcp_mappings>> udp_address_map_to_handshake;
std::mutex mutex_udp_seesion_caches;
std::map<std::shared_ptr<kcp_mappings>, std::vector<std::vector<uint8_t>>, std::owner_less<>> udp_seesion_caches;
std::unordered_map<std::shared_ptr<kcp_mappings>, std::vector<std::vector<uint8_t>>> udp_seesion_caches;

std::shared_mutex mutex_kcp_channels;
std::map<uint32_t, std::shared_ptr<kcp_mappings>> kcp_channels;
std::unordered_map<uint32_t, std::shared_ptr<kcp_mappings>> kcp_channels;

std::mutex mutex_expiring_kcp;
std::map<std::shared_ptr<kcp_mappings>, int64_t, std::owner_less<>> expiring_kcp;
std::unordered_map<std::shared_ptr<kcp_mappings>, int64_t> expiring_kcp;
std::mutex mutex_expiring_handshakes;
std::map<std::shared_ptr<kcp_mappings>, int64_t, std::owner_less<>> expiring_handshakes;
std::unordered_map<std::shared_ptr<kcp_mappings>, int64_t> expiring_handshakes;
std::mutex mutex_expiring_forwarders;
std::map<std::shared_ptr<forwarder>, int64_t, std::owner_less<>> expiring_forwarders;
std::unordered_map<std::shared_ptr<forwarder>, int64_t> expiring_forwarders;

std::shared_mutex mutex_target_address;
std::unique_ptr<asio::ip::address> target_address;
Expand All @@ -42,7 +42,7 @@ class client_mode
std::map<std::weak_ptr<KCP::KCP>, std::atomic<int64_t>, std::owner_less<>> kcp_keepalive;

std::shared_mutex mutex_id_map_to_mux_records;
std::map<uint64_t, std::shared_ptr<mux_records>> id_map_to_mux_records; // (KCP conv << 32) + connection uid
std::unordered_map<uint64_t, std::shared_ptr<mux_records>> id_map_to_mux_records; // (KCP conv << 32) + connection uid
std::shared_mutex mutex_udp_map_to_mux_records;
std::map<udp::endpoint, std::weak_ptr<mux_records>> udp_map_to_mux_records;

Expand Down
1 change: 1 addition & 0 deletions src/networks/connections.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <array>
#include <atomic>
#include <unordered_set>
#include <unordered_map>
#include <iostream>
#include <iomanip>
#include <sstream>
Expand Down
8 changes: 4 additions & 4 deletions src/networks/relay.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,21 @@ class relay_mode
std::array<uint8_t, 16> external_ipv6_address;
const std::array<uint8_t, 16> zero_value_array;

std::map<asio::ip::port_type, std::unique_ptr<udp_server>> udp_servers;
std::unordered_map<asio::ip::port_type, std::unique_ptr<udp_server>> udp_servers;

std::shared_mutex mutex_id_map_to_both_sides;
std::map<uint32_t, std::shared_ptr<kcp_mappings>> id_map_to_both_sides;
std::unordered_map<uint32_t, std::shared_ptr<kcp_mappings>> id_map_to_both_sides;

std::shared_mutex mutex_handshake_ingress_map_to_channels;
std::map<udp::endpoint, std::shared_ptr<kcp_mappings>> handshake_ingress_map_to_channels;

std::mutex mutex_expiring_kcp;
std::map<std::shared_ptr<kcp_mappings>, int64_t, std::owner_less<>> expiring_kcp;
std::unordered_map<std::shared_ptr<kcp_mappings>, int64_t> expiring_kcp;
std::mutex mutex_expiring_handshakes;
std::map<std::weak_ptr<kcp_mappings>, int64_t, std::owner_less<>> expiring_handshakes;

std::mutex mutex_expiring_forwarders;
std::map<std::shared_ptr<forwarder>, int64_t, std::owner_less<>> expiring_forwarders;
std::unordered_map<std::shared_ptr<forwarder>, int64_t> expiring_forwarders;

std::shared_mutex mutex_kcp_keepalive_ingress;
std::map<std::weak_ptr<KCP::KCP>, std::atomic<int64_t>, std::owner_less<>> kcp_keepalive_ingress;
Expand Down
10 changes: 5 additions & 5 deletions src/networks/server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,26 @@ class server_mode
std::array<uint8_t, 16> external_ipv6_address;
const std::array<uint8_t, 16> zero_value_array;

std::map<asio::ip::port_type, std::unique_ptr<udp_server>> udp_servers;
std::unordered_map<asio::ip::port_type, std::unique_ptr<udp_server>> udp_servers;

std::shared_mutex mutex_handshake_channels;
std::map<udp::endpoint, std::shared_ptr<kcp_mappings>> handshake_channels;
std::shared_mutex mutex_kcp_channels;
std::map<uint32_t, std::shared_ptr<kcp_mappings>> kcp_channels;
std::unordered_map<uint32_t, std::shared_ptr<kcp_mappings>> kcp_channels;

std::mutex mutex_expiring_kcp;
std::map<std::shared_ptr<kcp_mappings>, int64_t, std::owner_less<>> expiring_kcp;
std::unordered_map<std::shared_ptr<kcp_mappings>, int64_t> expiring_kcp;
std::mutex mutex_expiring_handshakes;
std::map<std::weak_ptr<kcp_mappings>, int64_t, std::owner_less<>> expiring_handshakes;

std::shared_mutex mutex_kcp_keepalive;
std::map<std::weak_ptr<KCP::KCP>, std::atomic<int64_t>, std::owner_less<>> kcp_keepalive;

std::shared_mutex mutex_id_map_to_mux_records;
std::map<uint64_t, std::shared_ptr<mux_records>> id_map_to_mux_records; // (KCP conv << 32) + connection uid
std::unordered_map<uint64_t, std::shared_ptr<mux_records>> id_map_to_mux_records; // (KCP conv << 32) + connection uid

std::shared_mutex mutex_expiring_mux_records;
std::map<uint64_t, std::shared_ptr<mux_records>> expiring_mux_records; // (KCP conv << 32) + connection uid
std::unordered_map<uint64_t, std::shared_ptr<mux_records>> expiring_mux_records; // (KCP conv << 32) + connection uid

std::shared_mutex mutex_mux_tcp_cache;
std::map<std::weak_ptr<KCP::KCP>, std::deque<mux_data_cache>, std::owner_less<>> mux_tcp_cache;
Expand Down

0 comments on commit f319f8a

Please sign in to comment.