Skip to content
Snippets Groups Projects
Commit da4a3b00 authored by Ramin Yaghoubzadeh Torky's avatar Ramin Yaghoubzadeh Torky
Browse files

C++: optimization: found CopyFrom -> used in lieu of repeated parse

parent bef9e36f
No related branches found
No related tags found
No related merge requests found
......@@ -103,7 +103,7 @@ IPAACA_HEADER_EXPORT class PayloadDocumentEntry//{{{
public:
IPAACA_MEMBER_VAR_EXPORT ipaaca::Lock lock;
IPAACA_MEMBER_VAR_EXPORT bool modified;
IPAACA_MEMBER_VAR_EXPORT std::string json_source;
//IPAACA_MEMBER_VAR_EXPORT std::string json_source;
IPAACA_MEMBER_VAR_EXPORT rapidjson::Document document;
IPAACA_HEADER_EXPORT inline PayloadDocumentEntry(): modified(false) { }
IPAACA_HEADER_EXPORT inline ~PayloadDocumentEntry() { }
......@@ -114,7 +114,7 @@ IPAACA_HEADER_EXPORT class PayloadDocumentEntry//{{{
IPAACA_HEADER_EXPORT static std::shared_ptr<PayloadDocumentEntry> create_null();
IPAACA_HEADER_EXPORT std::shared_ptr<PayloadDocumentEntry> clone();
IPAACA_HEADER_EXPORT rapidjson::Value& get_or_create_nested_value_from_proxy_path(PayloadEntryProxy* pep);
IPAACA_HEADER_EXPORT void update_json_source();
//IPAACA_HEADER_EXPORT void update_json_source();
typedef std::shared_ptr<PayloadDocumentEntry> ptr;
};
//}}}
......@@ -296,7 +296,7 @@ IPAACA_HEADER_EXPORT class PayloadEntryProxy//{{{
PayloadDocumentEntry::ptr new_entry = document_entry->clone(); // copy-on-write, no lock required
rapidjson::Value& newval = new_entry->get_or_create_nested_value_from_proxy_path(this);
pack_into_json_value(newval, new_entry->document.GetAllocator(), t);
new_entry->update_json_source();
//new_entry->update_json_source();
_payload->set(_key, new_entry);
return *this;
}
......
......@@ -50,6 +50,7 @@ int json_testbed_main(int argc, char** argv)
ipaaca::FakeIU::ptr iu = ipaaca::FakeIU::create();
iu->add_fake_payload_item("a", entry);
iu->add_fake_payload_item("b", entrynew);
iu->payload()["c"] = "simpleString";
auto proxy = iu->payload()["a"][3];
std::cout << proxy << std::endl;
......
......@@ -83,7 +83,7 @@ IPAACA_EXPORT std::ostream& operator<<(std::ostream& os, const Payload& obj)//{{
bool first = true;
for (auto& kv: obj._document_store) {
if (first) { first=false; } else { os << ", "; }
os << "'" << kv.first << "':'" << kv.second->json_source << "'";
os << "\"" << kv.first << "\":" << kv.second->to_json_string_representation() << "";
}
os << "}";
return os;
......@@ -215,33 +215,36 @@ IPAACA_EXPORT PayloadDocumentEntry::ptr PayloadDocumentEntry::from_json_string_r
if (entry->document.Parse(json_str.c_str()).HasParseError()) {
throw JsonParsingError();
}
entry->json_source = json_str;
//entry->json_source = json_str;
return entry;
}
IPAACA_EXPORT PayloadDocumentEntry::ptr PayloadDocumentEntry::from_unquoted_string_value(const std::string& str)
{
PayloadDocumentEntry::ptr entry = std::make_shared<ipaaca::PayloadDocumentEntry>();
entry->document.SetString(str.c_str(), entry->document.GetAllocator());
entry->update_json_source();
//entry->update_json_source();
return entry;
}
/// update json_source after a write operation (on newly cloned entries)
/*
IPAACA_EXPORT void PayloadDocumentEntry::update_json_source()
{
json_source = to_json_string_representation();
}
*/
IPAACA_EXPORT PayloadDocumentEntry::ptr PayloadDocumentEntry::create_null()
{
PayloadDocumentEntry::ptr entry = std::make_shared<ipaaca::PayloadDocumentEntry>();
entry->json_source = "null"; // rapidjson::Document value is also null implicitly
//entry->json_source = "null"; // rapidjson::Document value is also null implicitly
return entry;
}
IPAACA_EXPORT PayloadDocumentEntry::ptr PayloadDocumentEntry::clone()
{
auto entry = PayloadDocumentEntry::from_json_string_representation(this->json_source);
//auto entry = PayloadDocumentEntry::from_json_string_representation(this->json_source);
auto entry = PayloadDocumentEntry::create_null();
entry->document.CopyFrom(this->document, entry->document.GetAllocator());
IPAACA_DEBUG("Cloned for copy-on-write, contents: " << entry)
return entry;
}
......@@ -462,7 +465,7 @@ IPAACA_EXPORT PayloadEntryProxy& PayloadEntryProxy::operator=(const PayloadEntry
if (valueptr) { // only set if value is valid, keep default null value otherwise
newval.CopyFrom(*valueptr, new_entry->document.GetAllocator());
}
new_entry->update_json_source();
//new_entry->update_json_source();
_payload->set(_key, new_entry);
return *this;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment