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
This commit is contained in:
Yi-Yo Chiang 2023-08-02 15:18:35 +08:00
parent da5323e2d6
commit e54c0be60f
2 changed files with 9 additions and 9 deletions

View File

@ -122,10 +122,8 @@ void ImportKernelCmdlineFromString(const std::string& cmdline,
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 @@ void ImportKernelCmdlineFromString(const std::string& cmdline,
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;

View File

@ -831,9 +831,8 @@ bool ReadDefaultFstab(Fstab* fstab) {
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 @@ std::set<std::string> GetBootDevices() {
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) {