I am new to C++ and was wondering if reusing a Document to parse multiple json from a websocket stream reuses the same memory as the previous Parse?
Or do I have to specifically allocate memory for it to reuse it?
My current code is:
if (this->document.ParseInsitu<rapidjson::kParseStopWhenDoneFlag>(message).HasParseError()) {
rapidjson::ParseErrorCode e = this->document.GetParseError();
size_t o = this->document.GetErrorOffset();
std::cout << "Error: " << rapidjson::GetParseError_En(e) << std::endl;;
std::cout << " at offset " << o << " near: " << std::string(message) << std::endl;
}
else if (this->document.HasMember("data")) {
// bidSize = Uint
// bidPrice = Double
rapidjson::Value& recent = this->document["data"][this->document["data"].Size() - 1];
std::cout << recent["symbol"].GetString() << " - Bid: " << recent["bidPrice"].GetDouble() << ", Ask: " << recent["askPrice"].GetDouble() << std::endl;
}
You may clean a document and its previous allocations by:
~cpp
document.SetNull();
document.GetAlloctaor().Clear();
~
Or simply swap it with a new document:
~cpp
Document n;
document.Swap(n);
~
thanks a lot @miloyip , but I am wondering which of these 2 is more latency friendly?