libfstab: Optimize out C++ object copy
* Edit / truncate string objects in-place, don't copy a temporary string
object just for storing intermeidate results.
* Replace copy construct semantics with move semantics.
* Use range-based std::vector::insert() to move whole range.
Bug: 293695109
Test: CtsFsMgrTestCases
Change-Id: I5437303ba9900dbad3276a981413cba138f17157
diff --git a/fs_mgr/libfstab/boot_config.cpp b/fs_mgr/libfstab/boot_config.cpp
index ae537b5..b21495e 100644
--- a/fs_mgr/libfstab/boot_config.cpp
+++ b/fs_mgr/libfstab/boot_config.cpp
@@ -122,10 +122,8 @@
if ((found = cmdline.find(quote, found + 1)) == cmdline.npos) break;
++found;
}
- std::string piece;
- auto source = cmdline.substr(base, found - base);
- std::remove_copy(source.begin(), source.end(),
- std::back_insert_iterator<std::string>(piece), quote);
+ std::string piece = cmdline.substr(base, found - base);
+ piece.erase(std::remove(piece.begin(), piece.end(), quote), piece.end());
auto equal_sign = piece.find('=');
if (equal_sign == piece.npos) {
if (!piece.empty()) {
@@ -133,7 +131,9 @@
fn(std::move(piece), "");
}
} else {
- fn(piece.substr(0, equal_sign), piece.substr(equal_sign + 1));
+ std::string value = piece.substr(equal_sign + 1);
+ piece.resize(equal_sign);
+ fn(std::move(piece), std::move(value));
}
if (found == cmdline.npos) break;
base = found + 1;
diff --git a/fs_mgr/libfstab/fstab.cpp b/fs_mgr/libfstab/fstab.cpp
index 21b0024..32460b1 100644
--- a/fs_mgr/libfstab/fstab.cpp
+++ b/fs_mgr/libfstab/fstab.cpp
@@ -831,9 +831,8 @@
Fstab default_fstab;
const std::string default_fstab_path = GetFstabPath();
if (!default_fstab_path.empty() && ReadFstabFromFile(default_fstab_path, &default_fstab)) {
- for (auto&& entry : default_fstab) {
- fstab->emplace_back(std::move(entry));
- }
+ fstab->insert(fstab->end(), std::make_move_iterator(default_fstab.begin()),
+ std::make_move_iterator(default_fstab.end()));
} else {
LINFO << __FUNCTION__ << "(): failed to find device default fstab";
}
@@ -879,7 +878,8 @@
const std::string dt_file_name = GetAndroidDtDir() + "boot_devices";
if (GetKernelCmdline("androidboot.boot_devices", &value) || ReadDtFile(dt_file_name, &value)) {
auto boot_devices_list = Split(value, ",");
- return {boot_devices_list.begin(), boot_devices_list.end()};
+ return {std::make_move_iterator(boot_devices_list.begin()),
+ std::make_move_iterator(boot_devices_list.end())};
}
ImportKernelCmdline([&](std::string key, std::string value) {