From 539690a351d8c325707368729aafa2b4fa134d4c Mon Sep 17 00:00:00 2001 From: Vladimir Marko Date: Thu, 5 Jun 2014 18:36:42 +0100 Subject: Avoid a memory allocation in OatFile::GetOatDexFile(). Use StringPiece instead of std::string as the map key. Change-Id: I05516d273de617a7d714e39ce6c4236cec6a09f7 --- runtime/oat_file.cc | 13 ++++++++----- runtime/oat_file.h | 15 +++++++++------ 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/runtime/oat_file.cc b/runtime/oat_file.cc index 74dfe91..6c44aa9 100644 --- a/runtime/oat_file.cc +++ b/runtime/oat_file.cc @@ -292,11 +292,14 @@ bool OatFile::Setup(std::string* error_msg) { return false; } - oat_dex_files_.Put(dex_file_location, new OatDexFile(this, - dex_file_location, - dex_file_checksum, - dex_file_pointer, - methods_offsets_pointer)); + OatDexFile* oat_dex_file = new OatDexFile(this, + dex_file_location, + dex_file_checksum, + dex_file_pointer, + methods_offsets_pointer); + // Use a StringPiece backed by the oat_dex_file's internal std::string as the key. + StringPiece key(oat_dex_file->GetDexFileLocation()); + oat_dex_files_.Put(key, oat_dex_file); } return true; } diff --git a/runtime/oat_file.h b/runtime/oat_file.h index d703731..eae0418 100644 --- a/runtime/oat_file.h +++ b/runtime/oat_file.h @@ -20,6 +20,7 @@ #include #include +#include "base/stringpiece.h" #include "dex_file.h" #include "invoke_type.h" #include "mem_map.h" @@ -206,11 +207,11 @@ class OatFile { const byte* dex_file_pointer, const uint32_t* oat_class_offsets_pointer); - const OatFile* oat_file_; - std::string dex_file_location_; - uint32_t dex_file_location_checksum_; - const byte* dex_file_pointer_; - const uint32_t* oat_class_offsets_pointer_; + const OatFile* const oat_file_; + const std::string dex_file_location_; + const uint32_t dex_file_location_checksum_; + const byte* const dex_file_pointer_; + const uint32_t* const oat_class_offsets_pointer_; friend class OatFile; DISALLOW_COPY_AND_ASSIGN(OatDexFile); @@ -270,7 +271,9 @@ class OatFile { // dlopen handle during runtime. void* dlopen_handle_; - typedef SafeMap Table; + // NOTE: We use a StringPiece as the key type to avoid a memory allocation on every lookup + // with a const char* key. + typedef SafeMap Table; Table oat_dex_files_; friend class OatClass; -- cgit v1.1