Callbacks: minor efficiency wins, avoid tokenizing where we can.

Signed-off-by: Michael Meeks <michael.meeks@collabora.com>
Change-Id: I2ef647fd0af7bc83b7b40fd3ce7a49dc799f4339
pull/8165/merge
Michael Meeks 2024-05-09 15:29:49 +01:00 committed by Caolán McNamara
parent 6f49f9398e
commit f8a0d6c086
2 changed files with 35 additions and 13 deletions

View File

@ -12,6 +12,8 @@
#include <config.h>
#include "KitQueue.hpp"
#include <string.h>
#include <climits>
#include <algorithm>
#include <string>
@ -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
@ -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"))
else if (textItem(value, firstToken, removeText))
{
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"))
{
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

View File

@ -256,7 +256,7 @@ public:
{
{
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();