Browse Source

Migrate maps to unordered maps

pull/7315/head
Gleb Mazovetskiy 2 years ago
parent
commit
730c26a856
  1. 5
      Source/dvlnet/base.h
  2. 7
      Source/dvlnet/base_protocol.h
  3. 4
      Source/dvlnet/cdwrap.h
  4. 12
      Source/dvlnet/protocol_zt.cpp
  5. 15
      Source/dvlnet/protocol_zt.h
  6. 5
      Source/qol/stash.h
  7. 4
      Source/utils/language.cpp

5
Source/dvlnet/base.h

@ -3,10 +3,11 @@
#include <array>
#include <cstdint>
#include <deque>
#include <map>
#include <memory>
#include <string>
#include <ankerl/unordered_dense.h>
#include "dvlnet/abstract_net.h"
#include "dvlnet/packet.h"
#include "multi.h"
@ -41,7 +42,7 @@ public:
~base() override = default;
protected:
std::map<event_type, SEVTHANDLER> registered_handlers;
ankerl::unordered_dense::map<event_type, SEVTHANDLER> registered_handlers;
buffer_t game_init_info;
struct message_t {

7
Source/dvlnet/base_protocol.h

@ -1,13 +1,15 @@
#pragma once
#include <map>
#include <memory>
#include <set>
#include <string>
#include <ankerl/unordered_dense.h>
#include "dvlnet/base.h"
#include "dvlnet/packet.h"
#include "player.h"
#include "utils/algorithm/container.hpp"
#include "utils/log.hpp"
namespace devilution::net {
@ -50,7 +52,7 @@ private:
std::vector<std::string> playerNames;
endpoint_t peer;
};
std::map</*name*/ std::string, GameListValue> game_list;
ankerl::unordered_dense::map</*name*/ std::string, GameListValue> game_list;
std::array<Peer, MAX_PLRS> peers;
bool isGameHost_;
@ -535,6 +537,7 @@ std::vector<GameInfo> base_protocol<P>::get_gamelist()
const auto &[gameData, players, _] = gameInfo;
ret.push_back(GameInfo { name, gameData, players });
}
c_sort(ret, [](const GameInfo &a, const GameInfo &b) { return a.name < b.name; });
return ret;
}

4
Source/dvlnet/cdwrap.h

@ -2,12 +2,12 @@
#include <cstdint>
#include <exception>
#include <map>
#include <memory>
#include <optional>
#include <string>
#include <vector>
#include <ankerl/unordered_dense.h>
#include <function_ref.hpp>
#include "dvlnet/abstract_net.h"
@ -18,7 +18,7 @@ namespace devilution::net {
class cdwrap : public abstract_net {
private:
std::unique_ptr<abstract_net> dvlnet_wrap;
std::map<event_type, SEVTHANDLER> registered_handlers;
ankerl::unordered_dense::map<event_type, SEVTHANDLER> registered_handlers;
buffer_t game_init_info;
std::optional<std::string> game_pw;
tl::function_ref<std::unique_ptr<abstract_net>()> make_net_fn_;

12
Source/dvlnet/protocol_zt.cpp

@ -277,14 +277,15 @@ bool protocol_zt::get_disconnected(endpoint &peer)
void protocol_zt::disconnect(const endpoint &peer)
{
if (peer_list.count(peer) != 0) {
if (peer_list[peer].fd != -1) {
if (lwip_close(peer_list[peer].fd) < 0) {
const auto it = peer_list.find(peer);
if (it != peer_list.end()) {
if (it->second.fd != -1) {
if (lwip_close(it->second.fd) < 0) {
Log("lwip_close: {}", strerror(errno));
SDL_SetError("lwip_close: %s", strerror(errno));
}
}
peer_list.erase(peer);
peer_list.erase(it);
}
}
@ -328,7 +329,8 @@ uint64_t protocol_zt::current_ms()
bool protocol_zt::is_peer_connected(endpoint &peer)
{
return peer_list.count(peer) != 0 && peer_list[peer].fd != -1;
const auto it = peer_list.find(peer);
return it != peer_list.end() && it->second.fd != -1;
}
bool protocol_zt::is_peer_relayed(const endpoint &peer) const

15
Source/dvlnet/protocol_zt.h

@ -6,10 +6,10 @@
#include <cstdint>
#include <deque>
#include <exception>
#include <map>
#include <set>
#include <string>
#include <ankerl/unordered_dense.h>
#include "dvlnet/frame_queue.h"
#include "dvlnet/packet.h"
@ -63,6 +63,15 @@ public:
void from_string(const std::string &str);
};
struct EndpointHash {
using is_avalanching = void;
[[nodiscard]] uint64_t operator()(const endpoint &e) const noexcept
{
return ankerl::unordered_dense::hash<std::string_view> {}(
std::string_view { reinterpret_cast<const char *>(e.addr.data()), e.addr.size() });
}
};
protocol_zt();
~protocol_zt();
@ -90,7 +99,7 @@ private:
std::deque<std::pair<endpoint, buffer_t>> oob_recv_queue;
std::deque<endpoint> disconnect_queue;
std::map<endpoint, peer_state> peer_list;
ankerl::unordered_dense::map<endpoint, peer_state, EndpointHash> peer_list;
int fd_tcp = -1;
int fd_udp = -1;

5
Source/qol/stash.h

@ -6,9 +6,10 @@
#pragma once
#include <cstdint>
#include <map>
#include <vector>
#include <ankerl/unordered_dense.h>
#include "engine/point.hpp"
#include "items.h"
@ -21,7 +22,7 @@ public:
static constexpr StashCell EmptyCell = -1;
void RemoveStashItem(StashCell iv);
std::map<unsigned, StashGrid> stashGrids;
ankerl::unordered_dense::map<unsigned, StashGrid> stashGrids;
std::vector<Item> stashList;
int gold;
bool dirty = false;

4
Source/utils/language.cpp

@ -40,9 +40,9 @@ using TranslationRef = uint32_t;
struct StringHash {
using is_avalanching = void;
size_t operator()(const char *str) const noexcept
[[nodiscard]] uint64_t operator()(const char *str) const noexcept
{
return ankerl::unordered_dense::detail::wyhash::hash(str, std::string_view { str }.size());
return ankerl::unordered_dense::hash<std::string_view> {}(str);
}
};

Loading…
Cancel
Save