Commit 1cdcc129 authored by Alex Hultman's avatar Alex Hultman

Fix sprintf bottleneck, add water mark

parent 1b565856
......@@ -203,13 +203,58 @@ struct HttpResponse {
return length + 128;
}
static unsigned int uIntToString(uint32_t val, char *dst) {
static const char *lut[] = {
"00", "01", "02", "03", "04", "05", "06", "07", "08", "09",
"10", "11", "12", "13", "14", "15", "16", "17", "18", "19",
"20", "21", "22", "23", "24", "25", "26", "27", "28", "29",
"30", "31", "32", "33", "34", "35", "36", "37", "38", "39",
"40", "41", "42", "43", "44", "45", "46", "47", "48", "49",
"50", "51", "52", "53", "54", "55", "56", "57", "58", "59",
"60", "61", "62", "63", "64", "65", "66", "67", "68", "69",
"70", "71", "72", "73", "74", "75", "76", "77", "78", "79",
"80", "81", "82", "83", "84", "85", "86", "87", "88", "89",
"90", "91", "92", "93", "94", "95", "96", "97", "98", "99"
};
static const char *lowLut = "0123456789";
char buf[10];
char *top = buf + 10;
while (val >= 100) {
memcpy(top -= 2, lut[val % 100], 2);
val /= 100;
}
if (val >= 10) {
memcpy(top -= 2, lut[val], 2);
} else {
memcpy(--top, &lowLut[val], 1);
}
int length = (buf + 10) - top;
memcpy(dst, top, length);
return length;
}
static size_t transform(const char *src, char *dst, size_t length, TransformData transformData) {
// todo: sprintf is extremely slow
int offset = transformData.hasHead ? 0 : std::sprintf(dst, "HTTP/1.1 200 OK\r\nContent-Length: %u\r\n\r\n", (unsigned int) length);
// "Hello World!" test
//memcpy(dst, "HTTP/1.1 200 OK\r\nContent-Length: 12\r\n\r\n", 39);
//int offset = 39;
// similar to HttpSocket::upgrade, make simpler
int offset;
if (!transformData.hasHead) {
static const char contentLength[] = "HTTP/1.1 200 OK\r\nContent-Length: ";
static const int contentLengthLength = sizeof(contentLength) - 1;
memcpy(dst, contentLength, (offset = contentLengthLength));
offset += uIntToString(length, dst + offset);
static const char server[] = "\r\nServer: uWebSockets";
static const int serverLength = sizeof(server) - 1;
memcpy(dst + offset, server, serverLength);
offset += serverLength;
memcpy(dst + offset, "\r\n\r\n", 4);
offset += 4;
} else {
offset = 0;
}
memcpy(dst + offset, src, length);
return length + offset;
......
......@@ -1152,7 +1152,7 @@ void serveHttp() {
int main(int argc, char *argv[])
{
// //serveEventSource();
// //serveHttp();
serveHttp();
// //serveBenchmark();
//#ifdef UWS_THREADSAFE
......
......@@ -23,9 +23,9 @@ HEADERS += ../src/WebSocketProtocol.h \
../uSockets/Epoll.h \
../src/uSockets.h
LIBS += -lasan -lssl -lcrypto -lz -lpthread -luv -lboost_system
LIBS += -lssl -lcrypto -lz -lpthread -luv -lboost_system
QMAKE_CXXFLAGS += -fsanitize=address -Wno-unused-parameter
QMAKE_CXXFLAGS += -Wno-unused-parameter
QMAKE_CXXFLAGS_RELEASE -= -O1
QMAKE_CXXFLAGS_RELEASE -= -O2
QMAKE_CXXFLAGS_RELEASE *= -O3 -g
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment