Cache DexFile begin and size
Avoid virtual function invocations in DexFile Begin() and Size() methods
by caching duplicate copies of the beginning address and size at construction.
Bug: 110980617
Test: make -j 50 test-art-host
Change-Id: I70867bcd69e30a93b91873171d365a916b76bc96
diff --git a/libdexfile/dex/dex_file.cc b/libdexfile/dex/dex_file.cc
index c2b478a..47a6c1b 100644
--- a/libdexfile/dex/dex_file.cc
+++ b/libdexfile/dex/dex_file.cc
@@ -99,6 +99,11 @@
const OatDexFile* oat_dex_file,
bool is_compact_dex)
: main_section_(std::move(main_section)),
+ main_begin_(main_section_->Begin()),
+ main_size_(main_section_->Size()),
+ data_section_(std::move(data_section)),
+ data_begin_(data_section_->Begin()),
+ data_size_(data_section_->Size()),
location_(location),
location_checksum_(location_checksum),
header_(reinterpret_cast<const Header*>(main_section_->Begin())),
@@ -127,8 +132,6 @@
// any of the sections via a pointer.
CHECK_ALIGNED(main_section_->Begin(), alignof(Header));
- data_section_ = std::move(data_section);
-
InitializeSectionsFromMapList();
}
@@ -138,6 +141,12 @@
const OatDexFile* oat_dex_file,
bool is_compact_dex)
: main_section_(std::move(main_section)),
+ main_begin_(main_section_->Begin()),
+ main_size_(main_section_->Size()),
+ data_section_(std::make_unique<NonOwningDexFileContainer>(main_section_->Begin(),
+ main_section_->Size())),
+ data_begin_(data_section_->Begin()),
+ data_size_(data_section_->Size()),
location_(location),
location_checksum_(location_checksum),
header_(reinterpret_cast<const Header*>(main_section_->Begin())),
@@ -166,9 +175,6 @@
// any of the sections via a pointer.
CHECK_ALIGNED(main_section_->Begin(), alignof(Header));
- data_section_ = std::make_unique<NonOwningDexFileContainer>(main_section_->Begin(),
- main_section_->Size());
-
InitializeSectionsFromMapList();
}
diff --git a/libdexfile/dex/dex_file.h b/libdexfile/dex/dex_file.h
index c94c786..714e42c 100644
--- a/libdexfile/dex/dex_file.h
+++ b/libdexfile/dex/dex_file.h
@@ -966,19 +966,19 @@
bool DisableWrite() const;
const uint8_t* Begin() const {
- return main_section_->Begin();
+ return main_begin_;
}
size_t Size() const {
- return main_section_->Size();
+ return main_size_;
}
const uint8_t* DataBegin() const {
- return data_section_->Begin();
+ return data_begin_;
}
size_t DataSize() const {
- return data_section_->Size();
+ return data_size_;
}
template <typename T>
@@ -1089,10 +1089,14 @@
void InitializeSectionsFromMapList();
// The container for the header and fixed portions.
- std::unique_ptr<DexFileContainer> main_section_;
+ const std::unique_ptr<DexFileContainer> main_section_;
+ const uint8_t* const main_begin_;
+ const size_t main_size_;
- // The container for the data section
- std::unique_ptr<DexFileContainer> data_section_;
+ // The container for the data section.
+ const std::unique_ptr<DexFileContainer> data_section_;
+ const uint8_t* const data_begin_;
+ const size_t data_size_;
// Typically the dex file name when available, alternatively some identifying string.
//