From ca171d421d0bd3cb8a7d6c6cb648a5f82bf27fa6 Mon Sep 17 00:00:00 2001 From: Alex Hultman Date: Fri, 18 Oct 2024 15:48:20 +0200 Subject: [PATCH] Separate cork size per SSL --- src/App.h | 2 +- src/AsyncSocket.h | 8 ++++---- src/LoopData.h | 6 +++--- src/WebSocket.h | 2 +- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/App.h b/src/App.h index db916822a..0a0906027 100644 --- a/src/App.h +++ b/src/App.h @@ -152,7 +152,7 @@ struct TemplatedApp { * app has one conceptual Topic tree) */ bool publish(std::string_view topic, std::string_view message, OpCode opCode, bool compress = false) { /* Anything big bypasses corking efforts */ - if (message.length() >= LoopData::CORK_BUFFER_SIZE) { + if (message.length() >= LoopData::CORK_BUFFER_SIZE[SSL]) { return topicTree->publishBig(nullptr, topic, {message, opCode, compress}, [](Subscriber *s, TopicTreeBigMessage &message) { auto *ws = (WebSocket *) s->user; diff --git a/src/AsyncSocket.h b/src/AsyncSocket.h index bf7b50a7b..007c3c81a 100644 --- a/src/AsyncSocket.h +++ b/src/AsyncSocket.h @@ -157,7 +157,7 @@ struct AsyncSocket { LoopData *loopData = getLoopData(); BackPressure &backPressure = getAsyncSocketData()->buffer; size_t existingBackpressure = backPressure.length(); - if ((!existingBackpressure) && (isCorked() || canCork()) && (loopData->corkOffset + size < LoopData::CORK_BUFFER_SIZE)) { + if ((!existingBackpressure) && (isCorked() || canCork()) && (loopData->corkOffset + size < LoopData::CORK_BUFFER_SIZE[SSL])) { /* Cork automatically if we can */ if (isCorked()) { char *sendBuffer = loopData->corkBuffer + loopData->corkOffset; @@ -271,7 +271,7 @@ struct AsyncSocket { if (length) { if (loopData->corkedSocket == this) { /* We are corked */ - if (LoopData::CORK_BUFFER_SIZE - loopData->corkOffset >= (unsigned int) length) { + if (LoopData::CORK_BUFFER_SIZE[SSL] - loopData->corkOffset >= (unsigned int) length) { /* If the entire chunk fits in cork buffer */ memcpy(loopData->corkBuffer + loopData->corkOffset, src, (unsigned int) length); loopData->corkOffset += (unsigned int) length; @@ -280,9 +280,9 @@ struct AsyncSocket { /* Strategy differences between SSL and non-SSL regarding syscall minimizing */ if constexpr (false) { /* Cork up as much as we can */ - unsigned int stripped = LoopData::CORK_BUFFER_SIZE - loopData->corkOffset; + unsigned int stripped = LoopData::CORK_BUFFER_SIZE[SSL] - loopData->corkOffset; memcpy(loopData->corkBuffer + loopData->corkOffset, src, stripped); - loopData->corkOffset = LoopData::CORK_BUFFER_SIZE; + loopData->corkOffset = LoopData::CORK_BUFFER_SIZE[SSL]; auto [written, failed] = uncork(src + stripped, length - (int) stripped, optionally); return {written + (int) stripped, failed}; diff --git a/src/LoopData.h b/src/LoopData.h index 1b79659c7..6276e8ff2 100644 --- a/src/LoopData.h +++ b/src/LoopData.h @@ -92,11 +92,11 @@ struct alignas(16) LoopData { /* Be silent */ bool noMark = false; - /* Good 16k for SSL perf. */ - static const unsigned int CORK_BUFFER_SIZE = 16 * 1024; + /* Good 16k for SSL perf. 32kb for TCP */ + static const unsigned int CORK_BUFFER_SIZE[2] = {32 * 1024, 16 * 1024}; /* Cork data */ - char *corkBuffer = new char[CORK_BUFFER_SIZE]; + char *corkBuffer = new char[CORK_BUFFER_SIZE[0]]; unsigned int corkOffset = 0; void *corkedSocket = nullptr; diff --git a/src/WebSocket.h b/src/WebSocket.h index 0b738abe6..aaf7fcee8 100644 --- a/src/WebSocket.h +++ b/src/WebSocket.h @@ -365,7 +365,7 @@ struct WebSocket : AsyncSocket { } /* Publish as sender, does not receive its own messages even if subscribed to relevant topics */ - if (message.length() >= LoopData::CORK_BUFFER_SIZE) { + if (message.length() >= LoopData::CORK_BUFFER_SIZE[SSL]) { return webSocketContextData->topicTree->publishBig(webSocketData->subscriber, topic, {message, opCode, compress}, [](Subscriber *s, TopicTreeBigMessage &message) { auto *ws = (WebSocket *) s->user;