ART: Preallocate offset map
Reserve space in the offset-to-type map in the dex file verifier.
This avoids reallocations and improves performance, and in the
common case reduces peak memory consumption.
Bug: 110852609
Test: m test-art-host
Change-Id: I57bf32e6486c115b35fb2bdccb789a8b09272840
diff --git a/libdexfile/dex/dex_file_verifier.cc b/libdexfile/dex/dex_file_verifier.cc
index 86a28e5..52700a6 100644
--- a/libdexfile/dex/dex_file_verifier.cc
+++ b/libdexfile/dex/dex_file_verifier.cc
@@ -18,6 +18,7 @@
#include <inttypes.h>
+#include <algorithm>
#include <memory>
#include "android-base/stringprintf.h"
@@ -1997,6 +1998,13 @@
uint32_t count = map->size_;
ptr_ = begin_;
+ // Preallocate offset map to avoid some allocations. We can only guess from the list items,
+ // not derived things.
+ offset_to_type_map_.reserve(
+ std::min(header_->class_defs_size_, 65535u) +
+ std::min(header_->string_ids_size_, 65535u) +
+ 2 * std::min(header_->method_ids_size_, 65535u));
+
// Check the items listed in the map.
for (; count != 0u; --count) {
const size_t current_offset = offset;