Fix performance-inefficient-vector-operation clang-tidy issues am: 6a5b398d98 am: a2726f55d0

Original change: https://android-review.googlesource.com/c/platform/art/+/2388212

Change-Id: I444fc59ea673c464369a7927551f0869f08bedb0
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
diff --git a/build/Android.bp b/build/Android.bp
index c7fc420..49a4363 100644
--- a/build/Android.bp
+++ b/build/Android.bp
@@ -42,6 +42,7 @@
     "performance-faster-string-find",
     "performance-for-range-copy",
     "performance-implicit-conversion-in-loop",
+    "performance-inefficient-vector-operation",
     "performance-no-automatic-move",
     "performance-noexcept-move-constructor",
     "performance-unnecessary-copy-initialization",
diff --git a/cmdline/detail/cmdline_parse_argument_detail.h b/cmdline/detail/cmdline_parse_argument_detail.h
index 936d290..c47efe1 100644
--- a/cmdline/detail/cmdline_parse_argument_detail.h
+++ b/cmdline/detail/cmdline_parse_argument_detail.h
@@ -485,6 +485,7 @@
 
       // Error case: Fail, telling the user what the allowed values were.
       std::vector<std::string> allowed_values;
+      allowed_values.reserve(argument_info_.names_.size());
       for (auto&& arg_name : argument_info_.names_) {
         allowed_values.push_back(arg_name);
       }
diff --git a/dex2oat/dex2oat_test.cc b/dex2oat/dex2oat_test.cc
index 9ee532a..0099ba3 100644
--- a/dex2oat/dex2oat_test.cc
+++ b/dex2oat/dex2oat_test.cc
@@ -74,6 +74,7 @@
                                     bool use_fd = false) {
     std::unique_ptr<File> oat_file;
     std::vector<std::string> args;
+    args.reserve(dex_locations.size() + extra_args.size() + 6);
     // Add dex file args.
     for (const std::string& dex_location : dex_locations) {
       args.push_back("--dex-file=" + dex_location);
diff --git a/dex2oat/verifier_deps_test.cc b/dex2oat/verifier_deps_test.cc
index 6a2deba..00593f5 100644
--- a/dex2oat/verifier_deps_test.cc
+++ b/dex2oat/verifier_deps_test.cc
@@ -502,6 +502,7 @@
   std::vector<std::unique_ptr<const DexFile>> first_dex_files = OpenTestDexFiles("VerifierDeps");
   std::vector<std::unique_ptr<const DexFile>> second_dex_files = OpenTestDexFiles("MultiDex");
   std::vector<const DexFile*> dex_files;
+  dex_files.reserve(first_dex_files.size() + second_dex_files.size());
   for (auto& dex_file : first_dex_files) {
     dex_files.push_back(dex_file.get());
   }
diff --git a/imgdiag/imgdiag.cc b/imgdiag/imgdiag.cc
index ed2ff72..e3310e9 100644
--- a/imgdiag/imgdiag.cc
+++ b/imgdiag/imgdiag.cc
@@ -169,7 +169,7 @@
   // Store value->key so that we can use the default sort from pair which
   // sorts by value first and then key
   std::vector<std::pair<V, K>> value_key_vector;
-
+  value_key_vector.reserve(map.size());
   for (const auto& kv_pair : map) {
     value_key_vector.push_back(std::make_pair(value_mapper(kv_pair.second), kv_pair.first));
   }
diff --git a/libartbase/base/common_art_test.cc b/libartbase/base/common_art_test.cc
index c839216..c921f2e 100644
--- a/libartbase/base/common_art_test.cc
+++ b/libartbase/base/common_art_test.cc
@@ -610,6 +610,7 @@
   result.stage = ForkAndExecResult::kLink;
 
   std::vector<const char*> c_args;
+  c_args.reserve(argv.size() + 1);
   for (const std::string& str : argv) {
     c_args.push_back(str.c_str());
   }
diff --git a/libprofile/profile/profile_boot_info_test.cc b/libprofile/profile/profile_boot_info_test.cc
index 9939f0a..3c5829c 100644
--- a/libprofile/profile/profile_boot_info_test.cc
+++ b/libprofile/profile/profile_boot_info_test.cc
@@ -66,6 +66,7 @@
   ScratchFile profile;
   std::vector<std::unique_ptr<const DexFile>> dex_files = OpenTestDexFiles("MultiDex");
   std::vector<const DexFile*> dex_files2;
+  dex_files2.reserve(dex_files.size());
   for (const std::unique_ptr<const DexFile>& file : dex_files) {
     dex_files2.push_back(file.get());
   }
@@ -104,6 +105,7 @@
   ProfileBootInfo loaded_info;
   std::vector<std::unique_ptr<const DexFile>> dex_files = OpenTestDexFiles("MultiDex");
   std::vector<const DexFile*> dex_files2;
+  dex_files2.reserve(dex_files.size());
   for (const std::unique_ptr<const DexFile>& file : dex_files) {
     dex_files2.push_back(file.get());
   }
diff --git a/runtime/exec_utils.cc b/runtime/exec_utils.cc
index dd389f8..1021429 100644
--- a/runtime/exec_utils.cc
+++ b/runtime/exec_utils.cc
@@ -68,6 +68,7 @@
   // Convert the args to char pointers.
   const char* program = arg_vector[0].c_str();
   std::vector<char*> args;
+  args.reserve(arg_vector.size() + 1);
   for (const auto& arg : arg_vector) {
     args.push_back(const_cast<char*>(arg.c_str()));
   }
diff --git a/runtime/jit/profiling_info_test.cc b/runtime/jit/profiling_info_test.cc
index ce0a30f..0bd21aa 100644
--- a/runtime/jit/profiling_info_test.cc
+++ b/runtime/jit/profiling_info_test.cc
@@ -72,6 +72,7 @@
       Hotness::Flag flags) {
     ProfileCompilationInfo info;
     std::vector<ProfileMethodInfo> profile_methods;
+    profile_methods.reserve(methods.size());
     ScopedObjectAccess soa(Thread::Current());
     for (ArtMethod* method : methods) {
       profile_methods.emplace_back(
diff --git a/test/2005-pause-all-redefine-multithreaded/pause-all.cc b/test/2005-pause-all-redefine-multithreaded/pause-all.cc
index 9928411..37d6c4d 100644
--- a/test/2005-pause-all-redefine-multithreaded/pause-all.cc
+++ b/test/2005-pause-all-redefine-multithreaded/pause-all.cc
@@ -41,10 +41,12 @@
                                                     jobjectArray new_fields,
                                                     jstring default_val) {
   std::vector<jthread> threads;
+  threads.reserve(env->GetArrayLength(threads_arr));
   for (jint i = 0; i < env->GetArrayLength(threads_arr); i++) {
     threads.push_back(env->GetObjectArrayElement(threads_arr, i));
   }
   std::vector<jfieldID> fields;
+  fields.reserve(env->GetArrayLength(new_fields));
   for (jint i = 0; i < env->GetArrayLength(new_fields); i++) {
     fields.push_back(env->FromReflectedField(env->GetObjectArrayElement(new_fields, i)));
   }
diff --git a/test/913-heaps/heaps.cc b/test/913-heaps/heaps.cc
index 311b029..671cff8 100644
--- a/test/913-heaps/heaps.cc
+++ b/test/913-heaps/heaps.cc
@@ -266,6 +266,7 @@
 
     std::vector<std::string> GetLines() const {
       std::vector<std::string> ret;
+      ret.reserve(lines_.size());
       for (const std::unique_ptr<Elem>& e : lines_) {
         ret.push_back(e->Print());
       }
diff --git a/test/ti-agent/redefinition_helper.cc b/test/ti-agent/redefinition_helper.cc
index 0baa9fe..706531e 100644
--- a/test/ti-agent/redefinition_helper.cc
+++ b/test/ti-agent/redefinition_helper.cc
@@ -392,6 +392,7 @@
 static void DoClassRetransformation(jvmtiEnv* jvmti_env, JNIEnv* env, jobjectArray targets) {
   std::vector<jclass> classes;
   jint len = env->GetArrayLength(targets);
+  classes.reserve(len);
   for (jint i = 0; i < len; i++) {
     classes.push_back(static_cast<jclass>(env->GetObjectArrayElement(targets, i)));
   }
diff --git a/test/ti-agent/suspension_helper.cc b/test/ti-agent/suspension_helper.cc
index b685cb2..2b1ab67 100644
--- a/test/ti-agent/suspension_helper.cc
+++ b/test/ti-agent/suspension_helper.cc
@@ -37,6 +37,7 @@
 static std::vector<jthread> CopyToVector(JNIEnv* env, jobjectArray thrs) {
   jsize len = env->GetArrayLength(thrs);
   std::vector<jthread> ret;
+  ret.reserve(len);
   for (jsize i = 0; i < len; i++) {
     ret.push_back(reinterpret_cast<jthread>(env->GetObjectArrayElement(thrs, i)));
   }
diff --git a/tools/dexanalyze/dexanalyze_experiments.cc b/tools/dexanalyze/dexanalyze_experiments.cc
index b124f43..384ab37 100644
--- a/tools/dexanalyze/dexanalyze_experiments.cc
+++ b/tools/dexanalyze/dexanalyze_experiments.cc
@@ -459,6 +459,7 @@
     }
     // Count uses of top 16n.
     std::vector<size_t> uses;
+    uses.reserve(types_accessed.size());
     for (auto&& p : types_accessed) {
       uses.push_back(p.second);
     }