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 15 commits into
base: main
Choose a base branch
from
23 changes: 21 additions & 2 deletions src/server/network/message/outputmessage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
#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 +61,22 @@ 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);

if (!rawPtr) {
g_logger().error("Failed to allocate OutputMessage");
}

try {
rawPtr->resetMessage();
} catch (...) {
outputMessageAllocator.deallocate(rawPtr, 1);
throw;
}

return { rawPtr, [](OutputMessage* ptr) {
if (ptr != nullptr) {
outputMessageAllocator.deallocate(ptr, 1);
}
} };
}
5 changes: 5 additions & 0 deletions src/server/network/message/outputmessage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ class OutputMessage : public NetworkMessage {
OutputMessage(const OutputMessage &) = delete;
OutputMessage &operator=(const OutputMessage &) = delete;

void resetMessage() {
reset();
outputBufferStart = INITIAL_BUFFER_POSITION;
}

uint8_t* getOutputBuffer() {
return buffer.data() + outputBufferStart;
}
Expand Down
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
Loading