diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 6fe713f7..fe9c2b86 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -1,6 +1,10 @@ # Changelog All changes to this project will be documented in this file. +## [9.1.6] - 2020-03-29 + +(ws cobra subscriber) use a Json::StreamWriter to write to std::cout, and save one std::string allocation for each message printed + ## [9.1.5] - 2020-03-29 (docker) trim down docker image (300M -> 12M) / binary built without symbol and size optimization, and source code not copied over diff --git a/ixwebsocket/IXWebSocketVersion.h b/ixwebsocket/IXWebSocketVersion.h index 0b871525..28fad4aa 100644 --- a/ixwebsocket/IXWebSocketVersion.h +++ b/ixwebsocket/IXWebSocketVersion.h @@ -6,4 +6,4 @@ #pragma once -#define IX_WEBSOCKET_VERSION "9.1.5" +#define IX_WEBSOCKET_VERSION "9.1.6" diff --git a/ws/ws_cobra_subscribe.cpp b/ws/ws_cobra_subscribe.cpp index fd8f0ef7..4291bb0c 100644 --- a/ws/ws_cobra_subscribe.cpp +++ b/ws/ws_cobra_subscribe.cpp @@ -14,8 +14,19 @@ namespace ix { + using StreamWriterPtr = std::unique_ptr; + + StreamWriterPtr makeStreamWriter() + { + Json::StreamWriterBuilder builder; + builder["commentStyle"] = "None"; + builder["indentation"] = ""; // will make the JSON object compact + std::unique_ptr jsonWriter(builder.newStreamWriter()); + return jsonWriter; + } + void writeToStdout(bool fluentd, - Json::FastWriter& jsonWriter, + const StreamWriterPtr& jsonWriter, const Json::Value& msg, const std::string& position) { @@ -29,12 +40,15 @@ namespace ix msgWithPosition["position"] = position; enveloppe["message"] = msgWithPosition; - std::cout << jsonWriter.write(enveloppe); + jsonWriter->write(enveloppe, &std::cout); + std::cout << std::endl; // add lf and flush } else { enveloppe = msg; - std::cout << position << " " << jsonWriter.write(enveloppe); + std::cout << position << " "; + jsonWriter->write(enveloppe, &std::cout); + std::cout << std::endl; } } @@ -49,13 +63,10 @@ namespace ix conn.configure(config); conn.connect(); - Json::FastWriter jsonWriter; - - // Display incoming messages std::atomic msgPerSeconds(0); std::atomic msgCount(0); - std::atomic fatalCobraError(false); + auto jsonWriter = makeStreamWriter(); auto timer = [&msgPerSeconds, &msgCount, &fatalCobraError] { while (!fatalCobraError)