summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Vladimir Marko <vmarko@google.com> 2024-04-09 11:22:13 +0000
committer VladimĂ­r Marko <vmarko@google.com> 2024-04-09 15:43:41 +0000
commitcabfc7506c0fb25c0d5199c44612206d2c9b82c6 (patch)
tree2ee8c1b53b390d40442d4805a5c2b092e222e1ac
parent420ffd6837d9207933b05c3630ff619c839b5ccf (diff)
Change `TypeLookupTable` to use `std::string_view`.
Test: m test-art-host-gtest Test: testrunner.py --host --optimizing Change-Id: I7dc4725de0d6e866251c211d11e016fbe621725a
-rw-r--r--libdexfile/dex/dex_file-inl.h7
-rw-r--r--libdexfile/dex/dex_file.h2
-rw-r--r--libdexfile/dex/type_lookup_table.cc27
-rw-r--r--libdexfile/dex/type_lookup_table.h4
4 files changed, 21 insertions, 19 deletions
diff --git a/libdexfile/dex/dex_file-inl.h b/libdexfile/dex/dex_file-inl.h
index 2b3eb4ae69..8ad065b494 100644
--- a/libdexfile/dex/dex_file-inl.h
+++ b/libdexfile/dex/dex_file-inl.h
@@ -60,6 +60,13 @@ inline const char* DexFile::GetStringData(const dex::StringId& string_id) const
}
ALWAYS_INLINE
+inline std::string_view DexFile::GetStringView(const dex::StringId& string_id) const {
+ uint32_t utf16_length;
+ const char* data = GetStringDataAndUtf16Length(string_id, &utf16_length);
+ return StringViewFromUtf16Length(data, utf16_length);
+}
+
+ALWAYS_INLINE
inline const char* DexFile::StringDataAndUtf16LengthByIdx(dex::StringIndex idx,
uint32_t* utf16_length) const {
if (!idx.IsValid()) {
diff --git a/libdexfile/dex/dex_file.h b/libdexfile/dex/dex_file.h
index 4186a80b43..ed3bdcd036 100644
--- a/libdexfile/dex/dex_file.h
+++ b/libdexfile/dex/dex_file.h
@@ -350,6 +350,8 @@ class DexFile {
const char* GetStringData(const dex::StringId& string_id) const;
+ std::string_view GetStringView(const dex::StringId& string_id) const;
+
// Index version of GetStringDataAndUtf16Length.
const char* StringDataAndUtf16LengthByIdx(dex::StringIndex idx, uint32_t* utf16_length) const;
diff --git a/libdexfile/dex/type_lookup_table.cc b/libdexfile/dex/type_lookup_table.cc
index eaa35f5b61..fa5352e74c 100644
--- a/libdexfile/dex/type_lookup_table.cc
+++ b/libdexfile/dex/type_lookup_table.cc
@@ -26,10 +26,6 @@
namespace art {
-static inline bool ModifiedUtf8StringEquals(const char* lhs, const char* rhs) {
- return CompareModifiedUtf8ToModifiedUtf8AsUtf16CodePointValues(lhs, rhs) == 0;
-}
-
TypeLookupTable TypeLookupTable::Create(const DexFile& dex_file) {
uint32_t num_class_defs = dex_file.NumClassDefs();
if (UNLIKELY(!SupportedSize(num_class_defs))) {
@@ -50,7 +46,7 @@ TypeLookupTable TypeLookupTable::Create(const DexFile& dex_file) {
const dex::ClassDef& class_def = dex_file.GetClassDef(class_def_idx);
const dex::TypeId& type_id = dex_file.GetTypeId(class_def.class_idx_);
const dex::StringId& str_id = dex_file.GetStringId(type_id.descriptor_idx_);
- const uint32_t hash = ComputeModifiedUtf8Hash(dex_file.GetStringData(str_id));
+ const uint32_t hash = ComputeModifiedUtf8Hash(dex_file.GetStringView(str_id));
const uint32_t pos = hash & mask;
if (entries[pos].IsEmpty()) {
entries[pos] = Entry(str_id.string_data_off_, hash, class_def_idx, mask_bits);
@@ -65,7 +61,7 @@ TypeLookupTable TypeLookupTable::Create(const DexFile& dex_file) {
const dex::ClassDef& class_def = dex_file.GetClassDef(class_def_idx);
const dex::TypeId& type_id = dex_file.GetTypeId(class_def.class_idx_);
const dex::StringId& str_id = dex_file.GetStringId(type_id.descriptor_idx_);
- const uint32_t hash = ComputeModifiedUtf8Hash(dex_file.GetStringData(str_id));
+ const uint32_t hash = ComputeModifiedUtf8Hash(dex_file.GetStringView(str_id));
// Find the last entry in the chain.
uint32_t tail_pos = hash & mask;
DCHECK(!entries[tail_pos].IsEmpty());
@@ -97,7 +93,7 @@ TypeLookupTable TypeLookupTable::Open(const uint8_t* dex_data_pointer,
return TypeLookupTable(dex_data_pointer, mask_bits, entries, /* owned_entries= */ nullptr);
}
-uint32_t TypeLookupTable::Lookup(const char* str, uint32_t hash) const {
+uint32_t TypeLookupTable::Lookup(std::string_view str, uint32_t hash) const {
uint32_t mask = Entry::GetMask(mask_bits_);
uint32_t pos = hash & mask;
// Thanks to special insertion algorithm, the element at position pos can be empty
@@ -117,8 +113,8 @@ uint32_t TypeLookupTable::Lookup(const char* str, uint32_t hash) const {
DCHECK(!entry->IsEmpty());
}
// Found partial hash match, compare strings (expecting this to succeed).
- const char* first_checked_str = GetStringData(*entry);
- if (ModifiedUtf8StringEquals(str, first_checked_str)) {
+ const std::string_view first_checked_str = GetStringData(*entry);
+ if (str == first_checked_str) {
return entry->GetClassDefIdx(mask_bits_);
}
// If we're at the end of the chain, return before doing further expensive work.
@@ -135,8 +131,7 @@ uint32_t TypeLookupTable::Lookup(const char* str, uint32_t hash) const {
pos = (pos + entry->GetNextPosDelta(mask_bits_)) & mask;
entry = &entries_[pos];
DCHECK(!entry->IsEmpty());
- if (compared_hash_bits == entry->GetHashBits(mask_bits_) &&
- ModifiedUtf8StringEquals(str, GetStringData(*entry))) {
+ if (compared_hash_bits == entry->GetHashBits(mask_bits_) && str == GetStringData(*entry)) {
return entry->GetClassDefIdx(mask_bits_);
}
} while (!entry->IsLast(mask_bits_));
@@ -151,8 +146,7 @@ void TypeLookupTable::Dump(std::ostream& os) const {
if (entry.IsEmpty()) {
os << i << ": empty";
} else {
- const char* first_checked_str = GetStringData(entry);
- os << i << ": " << std::string(first_checked_str);
+ os << i << ": " << GetStringData(entry);
}
os << '\n';
}
@@ -179,12 +173,11 @@ TypeLookupTable::TypeLookupTable(const uint8_t* dex_data_pointer,
entries_(entries),
owned_entries_(std::move(owned_entries)) {}
-const char* TypeLookupTable::GetStringData(const Entry& entry) const {
+std::string_view TypeLookupTable::GetStringData(const Entry& entry) const {
DCHECK(dex_data_begin_ != nullptr);
const uint8_t* ptr = dex_data_begin_ + entry.GetStringOffset();
- // Skip string length.
- DecodeUnsignedLeb128(&ptr);
- return reinterpret_cast<const char*>(ptr);
+ uint32_t utf16_length = DecodeUnsignedLeb128(&ptr);
+ return StringViewFromUtf16Length(reinterpret_cast<const char*>(ptr), utf16_length);
}
} // namespace art
diff --git a/libdexfile/dex/type_lookup_table.h b/libdexfile/dex/type_lookup_table.h
index 6619c848b2..c94f03588f 100644
--- a/libdexfile/dex/type_lookup_table.h
+++ b/libdexfile/dex/type_lookup_table.h
@@ -70,7 +70,7 @@ class TypeLookupTable {
// Method search class_def_idx by class descriptor and it's hash.
// If no data found then the method returns dex::kDexNoIndex.
- uint32_t Lookup(const char* str, uint32_t hash) const;
+ uint32_t Lookup(std::string_view str, uint32_t hash) const;
// Method returns pointer to binary data of lookup table. Used by the oat writer.
const uint8_t* RawData() const {
@@ -169,7 +169,7 @@ class TypeLookupTable {
const Entry* entries,
std::unique_ptr<Entry[]> owned_entries);
- const char* GetStringData(const Entry& entry) const;
+ std::string_view GetStringData(const Entry& entry) const;
const uint8_t* dex_data_begin_;
uint32_t mask_bits_;