From bdca366a46acd3451f2083575a0d7fe924f01898 Mon Sep 17 00:00:00 2001 From: Hwankyu Jhun Date: Thu, 22 Sep 2022 04:30:23 +0000 Subject: [PATCH] Use insert() instead of std::copy() To improve the performance of the bundle instance creation, we use the insert(). Change-Id: Iec2ddc05a1583a11612c40d6ef6d12a125ffabd8 Signed-off-by: Hwankyu Jhun --- src/bundle-internal.cc | 15 ++++++++------- src/json-internal.cc | 6 ++---- src/key-info-internal.cc | 15 +++++++-------- src/stub.cc | 4 ++-- 4 files changed, 19 insertions(+), 21 deletions(-) diff --git a/src/bundle-internal.cc b/src/bundle-internal.cc index 6c3be38..8b50c7b 100644 --- a/src/bundle-internal.cc +++ b/src/bundle-internal.cc @@ -185,7 +185,9 @@ unsigned char* Bundle::EncodeRaw(int* size) { std::vector bytes; for (const auto& key_info : list_) { auto encoded_bytes = key_info->Encode(); - bytes.insert(bytes.end(), encoded_bytes.begin(), encoded_bytes.end()); + bytes.insert(bytes.end(), + std::make_move_iterator(encoded_bytes.begin()), + std::make_move_iterator(encoded_bytes.end())); } gchar* checksum = g_compute_checksum_for_string( @@ -202,7 +204,8 @@ unsigned char* Bundle::EncodeRaw(int* size) { if (raw == nullptr) THROW(BUNDLE_ERROR_OUT_OF_MEMORY); - std::copy(bytes.begin(), bytes.end(), raw); + std::copy(std::make_move_iterator(bytes.begin()), + std::make_move_iterator(bytes.end()), raw); *size = static_cast(bytes.size()); return raw; } @@ -240,9 +243,8 @@ int Bundle::DecodeRaw(unsigned char* raw, int size) { unsigned char* p = reinterpret_cast(&total_size); std::copy(&bytes[reader], &bytes[reader] + sizeof(total_size), p); - std::vector encoded_bytes; - std::copy(&bytes[reader], &bytes[reader] + total_size, - std::back_inserter(encoded_bytes)); + std::vector encoded_bytes( + &bytes[reader], &bytes[reader] + total_size); reader += total_size; KeyInfo* new_key_info; @@ -298,8 +300,7 @@ int Bundle::Import(int argc, char** argv) { for (int idx = 1; idx + 1 < argc; idx += 2) { auto* p = reinterpret_cast(argv[idx +1]); auto len = strlen(argv[idx + 1]) + 1; - std::vector value; - std::copy(p, p + len, std::back_inserter(value)); + std::vector value(p, p + len); KeyInfo* new_key_info; try { diff --git a/src/json-internal.cc b/src/json-internal.cc index be83a5a..f658fc7 100644 --- a/src/json-internal.cc +++ b/src/json-internal.cc @@ -141,8 +141,7 @@ void Json::OnJsonObjectMember(JsonObject* object, const char* key, val = ""; auto* p = reinterpret_cast(const_cast(val)); - std::vector value; - std::copy(p, p + (strlen(val) + 1), std::back_inserter(value)); + std::vector value(p, p + (strlen(val) + 1)); values.push_back(std::move(value)); } @@ -161,8 +160,7 @@ void Json::OnJsonObjectMember(JsonObject* object, const char* key, val = ""; auto* p = reinterpret_cast(const_cast(val)); - std::vector value; - std::copy(p, p + (strlen(val) + 1), std::back_inserter(value)); + std::vector value(p, p + (strlen(val) + 1)); try { key_info = new KeyInfo(Bundle::Type::String, key, std::move(value)); diff --git a/src/key-info-internal.cc b/src/key-info-internal.cc index 8dd56cd..e89ac79 100644 --- a/src/key-info-internal.cc +++ b/src/key-info-internal.cc @@ -194,35 +194,34 @@ std::vector KeyInfo::Encode() { // total size unsigned char* p = reinterpret_cast(&encoded_size); - std::copy(p, p + sizeof(encoded_size), std::back_inserter(bytes)); + bytes.insert(bytes.end(), p, p + sizeof(encoded_size)); // type p = reinterpret_cast(&type_); - std::copy(p, p + sizeof(type_), std::back_inserter(bytes)); + bytes.insert(bytes.end(), p, p + sizeof(type_)); // key size std::size_t key_length = key_.length() + 1; p = reinterpret_cast(&key_length); - std::copy(p, p + sizeof(key_length), std::back_inserter(bytes)); + bytes.insert(bytes.end(), p, p + sizeof(key_length)); // key - std::copy(key_.begin(), key_.end() + 1, std::back_inserter(bytes)); + bytes.insert(bytes.end(), key_.begin(), key_.end() + 1); if (type_ & BUNDLE_TYPE_ARRAY) { // values size std::size_t values_size = values_.size(); p = reinterpret_cast(&values_size); - std::copy(p , p + sizeof(values_size), std::back_inserter(bytes)); + bytes.insert(bytes.end(), p , p + sizeof(values_size)); } // values for (unsigned int i = 0; i < values_.size(); i++) { std::size_t value_size = values_size_[i]; p = reinterpret_cast(&value_size); - std::copy(p, p + sizeof(value_size), std::back_inserter(bytes)); + bytes.insert(bytes.end(), p, p + sizeof(value_size)); - std::copy(values_[i].get(), values_[i].get() + value_size, - std::back_inserter(bytes)); + bytes.insert(bytes.end(), values_[i].get(), values_[i].get() + value_size); } return bytes; diff --git a/src/stub.cc b/src/stub.cc index 81815ee..fdfc9dc 100644 --- a/src/stub.cc +++ b/src/stub.cc @@ -481,7 +481,7 @@ extern "C" EXPORT_API int bundle_add_byte(bundle* b, const char* key, auto* p = reinterpret_cast(bytes); std::vector value; if (bytes) - std::copy(p, p + size, std::back_inserter(value)); + value.insert(value.end(), p, p + size); KeyInfo* key_info; try { @@ -722,7 +722,7 @@ extern "C" EXPORT_API int bundle_set_byte_array_element(bundle* b, std::vector value; if (bytes) { auto* p = reinterpret_cast(const_cast(bytes)); - std::copy(p, p + size, std::back_inserter(value)); + value.insert(value.end(), p, p + size); } auto* h = reinterpret_cast(b); -- 2.7.4