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//{{{ ...@@ -103,7 +103,7 @@ IPAACA_HEADER_EXPORT class PayloadDocumentEntry//{{{
public: public:
IPAACA_MEMBER_VAR_EXPORT ipaaca::Lock lock; IPAACA_MEMBER_VAR_EXPORT ipaaca::Lock lock;
IPAACA_MEMBER_VAR_EXPORT bool modified; 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_MEMBER_VAR_EXPORT rapidjson::Document document;
IPAACA_HEADER_EXPORT inline PayloadDocumentEntry(): modified(false) { } IPAACA_HEADER_EXPORT inline PayloadDocumentEntry(): modified(false) { }
IPAACA_HEADER_EXPORT inline ~PayloadDocumentEntry() { } IPAACA_HEADER_EXPORT inline ~PayloadDocumentEntry() { }
...@@ -114,7 +114,7 @@ IPAACA_HEADER_EXPORT class PayloadDocumentEntry//{{{ ...@@ -114,7 +114,7 @@ IPAACA_HEADER_EXPORT class PayloadDocumentEntry//{{{
IPAACA_HEADER_EXPORT static std::shared_ptr<PayloadDocumentEntry> create_null(); IPAACA_HEADER_EXPORT static std::shared_ptr<PayloadDocumentEntry> create_null();
IPAACA_HEADER_EXPORT std::shared_ptr<PayloadDocumentEntry> clone(); 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 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; typedef std::shared_ptr<PayloadDocumentEntry> ptr;
}; };
//}}} //}}}
...@@ -296,7 +296,7 @@ IPAACA_HEADER_EXPORT class PayloadEntryProxy//{{{ ...@@ -296,7 +296,7 @@ IPAACA_HEADER_EXPORT class PayloadEntryProxy//{{{
PayloadDocumentEntry::ptr new_entry = document_entry->clone(); // copy-on-write, no lock required 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); rapidjson::Value& newval = new_entry->get_or_create_nested_value_from_proxy_path(this);
pack_into_json_value(newval, new_entry->document.GetAllocator(), t); 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); _payload->set(_key, new_entry);
return *this; return *this;
} }
......
...@@ -50,6 +50,7 @@ int json_testbed_main(int argc, char** argv) ...@@ -50,6 +50,7 @@ int json_testbed_main(int argc, char** argv)
ipaaca::FakeIU::ptr iu = ipaaca::FakeIU::create(); ipaaca::FakeIU::ptr iu = ipaaca::FakeIU::create();
iu->add_fake_payload_item("a", entry); iu->add_fake_payload_item("a", entry);
iu->add_fake_payload_item("b", entrynew); iu->add_fake_payload_item("b", entrynew);
iu->payload()["c"] = "simpleString";
auto proxy = iu->payload()["a"][3]; auto proxy = iu->payload()["a"][3];
std::cout << proxy << std::endl; std::cout << proxy << std::endl;
......
...@@ -83,7 +83,7 @@ IPAACA_EXPORT std::ostream& operator<<(std::ostream& os, const Payload& obj)//{{ ...@@ -83,7 +83,7 @@ IPAACA_EXPORT std::ostream& operator<<(std::ostream& os, const Payload& obj)//{{
bool first = true; bool first = true;
for (auto& kv: obj._document_store) { for (auto& kv: obj._document_store) {
if (first) { first=false; } else { os << ", "; } if (first) { first=false; } else { os << ", "; }
os << "'" << kv.first << "':'" << kv.second->json_source << "'"; os << "\"" << kv.first << "\":" << kv.second->to_json_string_representation() << "";
} }
os << "}"; os << "}";
return os; return os;
...@@ -215,33 +215,36 @@ IPAACA_EXPORT PayloadDocumentEntry::ptr PayloadDocumentEntry::from_json_string_r ...@@ -215,33 +215,36 @@ IPAACA_EXPORT PayloadDocumentEntry::ptr PayloadDocumentEntry::from_json_string_r
if (entry->document.Parse(json_str.c_str()).HasParseError()) { if (entry->document.Parse(json_str.c_str()).HasParseError()) {
throw JsonParsingError(); throw JsonParsingError();
} }
entry->json_source = json_str; //entry->json_source = json_str;
return entry; return entry;
} }
IPAACA_EXPORT PayloadDocumentEntry::ptr PayloadDocumentEntry::from_unquoted_string_value(const std::string& str) IPAACA_EXPORT PayloadDocumentEntry::ptr PayloadDocumentEntry::from_unquoted_string_value(const std::string& str)
{ {
PayloadDocumentEntry::ptr entry = std::make_shared<ipaaca::PayloadDocumentEntry>(); PayloadDocumentEntry::ptr entry = std::make_shared<ipaaca::PayloadDocumentEntry>();
entry->document.SetString(str.c_str(), entry->document.GetAllocator()); entry->document.SetString(str.c_str(), entry->document.GetAllocator());
entry->update_json_source(); //entry->update_json_source();
return entry; return entry;
} }
/// update json_source after a write operation (on newly cloned entries) /// update json_source after a write operation (on newly cloned entries)
/*
IPAACA_EXPORT void PayloadDocumentEntry::update_json_source() IPAACA_EXPORT void PayloadDocumentEntry::update_json_source()
{ {
json_source = to_json_string_representation(); json_source = to_json_string_representation();
} }
*/
IPAACA_EXPORT PayloadDocumentEntry::ptr PayloadDocumentEntry::create_null() IPAACA_EXPORT PayloadDocumentEntry::ptr PayloadDocumentEntry::create_null()
{ {
PayloadDocumentEntry::ptr entry = std::make_shared<ipaaca::PayloadDocumentEntry>(); 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; return entry;
} }
IPAACA_EXPORT PayloadDocumentEntry::ptr PayloadDocumentEntry::clone() 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) IPAACA_DEBUG("Cloned for copy-on-write, contents: " << entry)
return entry; return entry;
} }
...@@ -462,7 +465,7 @@ IPAACA_EXPORT PayloadEntryProxy& PayloadEntryProxy::operator=(const PayloadEntry ...@@ -462,7 +465,7 @@ IPAACA_EXPORT PayloadEntryProxy& PayloadEntryProxy::operator=(const PayloadEntry
if (valueptr) { // only set if value is valid, keep default null value otherwise if (valueptr) { // only set if value is valid, keep default null value otherwise
newval.CopyFrom(*valueptr, new_entry->document.GetAllocator()); newval.CopyFrom(*valueptr, new_entry->document.GetAllocator());
} }
new_entry->update_json_source(); //new_entry->update_json_source();
_payload->set(_key, new_entry); _payload->set(_key, new_entry);
return *this; 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