From e54c0be60f5aa32ce06f4366a3c55db45c41093c Mon Sep 17 00:00:00 2001 From: Yi-Yo Chiang Date: Wed, 2 Aug 2023 15:18:35 +0800 Subject: [PATCH] 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 --- fs_mgr/libfstab/boot_config.cpp | 10 +++++----- fs_mgr/libfstab/fstab.cpp | 8 ++++---- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/fs_mgr/libfstab/boot_config.cpp b/fs_mgr/libfstab/boot_config.cpp index ae537b5d2..b21495ecd 100644 --- a/fs_mgr/libfstab/boot_config.cpp +++ b/fs_mgr/libfstab/boot_config.cpp @@ -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(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; diff --git a/fs_mgr/libfstab/fstab.cpp b/fs_mgr/libfstab/fstab.cpp index 21b002434..32460b17b 100644 --- a/fs_mgr/libfstab/fstab.cpp +++ b/fs_mgr/libfstab/fstab.cpp @@ -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 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) {