Rapidjson: Reusing ParseInsitu() uses same memory?

Created on 27 Oct 2018  路  2Comments  路  Source: Tencent/rapidjson

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;
    }
question

All 2 comments

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?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Andy1978 picture Andy1978  路  3Comments

europelee picture europelee  路  5Comments

srikarad07 picture srikarad07  路  5Comments

oneonce picture oneonce  路  3Comments

lasalvavida picture lasalvavida  路  5Comments