Skip to content
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

perf: Enhance Memory Management with Lock-Free Allocator, Preallocation, and Optimized Thread-Local Caching #2825

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions src/server/network/message/outputmessage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
#include "game/scheduling/dispatcher.hpp"
#include "utils/lockfree.hpp"

constexpr uint16_t OUTPUTMESSAGE_FREE_LIST_CAPACITY = 2048;
constexpr size_t OUTPUTMESSAGE_FREE_LIST_CAPACITY = 2048;
constexpr std::chrono::milliseconds OUTPUTMESSAGE_AUTOSEND_DELAY { 10 };
static inline LockfreePoolingAllocator<OutputMessage, OUTPUTMESSAGE_FREE_LIST_CAPACITY> outputMessageAllocator;

OutputMessagePool &OutputMessagePool::getInstance() {
return inject<OutputMessagePool>();
Expand Down Expand Up @@ -59,5 +60,19 @@ void OutputMessagePool::removeProtocolFromAutosend(const Protocol_ptr &protocol)
}

OutputMessage_ptr OutputMessagePool::getOutputMessage() {
return std::allocate_shared<OutputMessage>(LockfreePoolingAllocator<OutputMessage, OUTPUTMESSAGE_FREE_LIST_CAPACITY>());
OutputMessage* rawPtr = outputMessageAllocator.allocate(1);

try {
new (rawPtr) OutputMessage();
} catch (...) {
outputMessageAllocator.deallocate(rawPtr, 1);
throw;
}

return { rawPtr, [](OutputMessage* ptr) {
if (ptr != nullptr) {
ptr->~OutputMessage();
outputMessageAllocator.deallocate(ptr, 1);
}
} };
}
13 changes: 10 additions & 3 deletions src/server/network/protocol/protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#include "game/scheduling/dispatcher.hpp"
#include "utils/tools.hpp"

#include <lib/logging/log_with_spd_log.hpp>

Protocol::Protocol(const Connection_ptr &initConnection) :
connectionPtr(initConnection) { }

Expand Down Expand Up @@ -120,8 +122,9 @@ void Protocol::send(OutputMessage_ptr msg) const {
}

void Protocol::disconnect() const {
if (const auto connection = getConnection()) {
connection->close();
auto conn = connectionPtr.lock();
if (conn) {
conn->close();
}
}

Expand Down Expand Up @@ -234,7 +237,11 @@ bool Protocol::isConnectionExpired() const {
}

Connection_ptr Protocol::getConnection() const {
return connectionPtr.lock();
auto conn = connectionPtr.lock();
if (!conn) {
return nullptr;
}
return conn;
}

uint32_t Protocol::getIP() const {
Expand Down
4 changes: 3 additions & 1 deletion src/server/network/protocol/protocol.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ class Protocol : public std::enable_shared_from_this<Protocol> {
ZStream() noexcept;

~ZStream() {
deflateEnd(stream.get());
if (stream) {
deflateEnd(stream.get());
}
}

std::unique_ptr<z_stream> stream;
Expand Down
Loading