Skip to content

Commit

Permalink
Callbacks: minor efficiency wins, avoid tokenizing where we can.
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Meeks <michael.meeks@collabora.com>
Change-Id: I2ef647fd0af7bc83b7b40fd3ce7a49dc799f4339
  • Loading branch information
mmeeks authored and caolanm committed May 9, 2024
1 parent 6f49f93 commit f8a0d6c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 13 deletions.
46 changes: 34 additions & 12 deletions kit/KitQueue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#include <config.h>

#include "KitQueue.hpp"

#include <string.h>
#include <climits>
#include <algorithm>
#include <string>
Expand All @@ -32,15 +34,39 @@
return str.str();
}

namespace {
bool textItem(const KitQueue::Payload &value, const std::string &firstToken, bool &removeText)
{
size_t offset = firstToken.size(); // in this case a session
if (value.size() < offset + 3)
return false;

size_t remaining = value.size() - firstToken.size();

if (!memcmp(value.data() + offset + 1, "textinput", std::min(remaining, size_t(9))))
{
removeText = false;
return true;
}

if (!memcmp(value.data() + offset + 1, "removetextcontext", std::min(remaining, size_t(17))))
{
removeText = true;
return true;
}
return false;
}
}

void KitQueue::put(const Payload& value)
{
if (value.empty())
throw std::runtime_error("Cannot queue empty item.");

StringVector tokens = StringVector::tokenize(value.data(), value.size());

const std::string firstToken = COOLProtocol::getFirstToken(value);

bool removeText = false;

if (firstToken == "tilecombine")
{
// Breakup tilecombine and deduplicate (we are re-combining
Expand All @@ -65,17 +91,13 @@ void KitQueue::put(const Payload& value)
else if (firstToken == "callback")
assert(false && "callbacks should not come from the client");

else if (tokens.equals(1, "textinput"))
{
const std::string newMsg = combineTextInput(tokens);
if (!newMsg.empty())
_queue.emplace_back(newMsg.data(), newMsg.data() + newMsg.size());
else
_queue.emplace_back(value);
}
else if (tokens.equals(1, "removetextcontext"))
else if (textItem(value, firstToken, removeText))
{
const std::string newMsg = combineRemoveText(tokens);
StringVector tokens = StringVector::tokenize(value.data(), value.size());

std::string newMsg = !removeText ? combineTextInput(tokens)
: combineRemoveText(tokens);

if (!newMsg.empty())
_queue.emplace_back(newMsg.data(), newMsg.data() + newMsg.size());
else
Expand Down
2 changes: 1 addition & 1 deletion test/WebSocketSession.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ class WebSocketSession final : public WebSocketHandler
{
{
std::unique_lock<std::mutex> lock(_outMutex);
_outQueue.emplace_back(std::vector<char>(msg.data(), msg.data() + msg.size()));
_outQueue.emplace_back(msg.data(), msg.data() + msg.size());
}

const auto pollPtr = _socketPoll.lock();
Expand Down

0 comments on commit f8a0d6c

Please sign in to comment.