Related to issue #5876: reduce excessive copying of message data

Many .png tile messages are very large so avoid using
-[NSString stringByAppendingString:] calls to create the JavaScript
string.

Signed-off-by: Patrick Luby <plubius@neooffice.org>
Change-Id: I8163ef93da315122f938c9ed2b66b49a443f283f
pull/6366/head
Patrick Luby 2023-05-15 14:22:38 -04:00 committed by Andras Timar
parent 94bbaa7c3f
commit 9b2a3d8cc7
1 changed files with 14 additions and 7 deletions

View File

@ -98,8 +98,6 @@ static std::atomic<unsigned> appDocIdCounter(1);
- (void)send2JS:(const char *)buffer length:(int)length {
LOG_TRC("To JS: " << COOLProtocol::getAbbreviatedMessage(buffer, length).c_str());
NSString *js;
const unsigned char *ubufp = (const unsigned char *)buffer;
std::vector<char> data;
bool newlineFound = false;
@ -109,6 +107,12 @@ static std::atomic<unsigned> appDocIdCounter(1);
isMessageOfType(buffer, "renderfont:", length) ||
isMessageOfType(buffer, "rendersearchlist:", length) ||
isMessageOfType(buffer, "windowpaint:", length));
const char *pretext = "window.TheFakeWebSocket.onmessage({'data': '";
const int pretextlen = strlen(pretext);
for (int i = 0; i < pretextlen; i++)
data.push_back(pretext[i]);
for (int i = 0; i < length; i++) {
// Another fix for issue #5843 limit non-ASCII escaping to only
// certain message types
@ -131,19 +135,22 @@ static std::atomic<unsigned> appDocIdCounter(1);
data.push_back(ubufp[i]);
}
}
const char *posttext = "'});";
const int posttextlen = strlen(posttext);
for (int i = 0; i < posttextlen; i++)
data.push_back(posttext[i]);
data.push_back(0);
NSString *escapedData = [NSString stringWithUTF8String:data.data()];
if (!escapedData) {
NSString *js = [NSString stringWithUTF8String:data.data()];
if (!js) {
char outBuf[length + 1];
memcpy(outBuf, buffer, length);
outBuf[length] = '\0';
LOG_ERR("Couldn't create NSString with message: " << outBuf);
return;
}
js = @"window.TheFakeWebSocket.onmessage({'data': '";
js = [js stringByAppendingString:escapedData];
js = [js stringByAppendingString:@"'});"];
NSString *subjs = [js substringToIndex:std::min(100ul, js.length)];
if (subjs.length < js.length)