From f7e8255f5ce539f8ba578b32bd5f4c30e9a9d31e Mon Sep 17 00:00:00 2001 From: "ishell@chromium.org" Date: Wed, 30 Apr 2014 17:27:40 +0000 Subject: [PATCH] StringTable::Lookup*IfExist() handlified. R=yangguo@chromium.org Review URL: https://codereview.chromium.org/265553003 git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@21100 ce2b1a6d-e550-0410-aec6-3dcde31c8c00 --- src/factory.cc | 8 ++++---- src/heap.cc | 16 +++------------- src/heap.h | 2 -- src/objects.cc | 48 +++++++++++++++++++++++++++++------------------- src/objects.h | 20 ++++++++++++++------ src/scopeinfo.cc | 16 ++++++++-------- 6 files changed, 58 insertions(+), 52 deletions(-) diff --git a/src/factory.cc b/src/factory.cc index 7fc2a30343..864cf6729f 100644 --- a/src/factory.cc +++ b/src/factory.cc @@ -376,10 +376,10 @@ static inline Handle MakeOrFindTwoCharacterString(Isolate* isolate, // Numeric strings have a different hash algorithm not known by // LookupTwoCharsStringIfExists, so we skip this step for such strings. if (!Between(c1, '0', '9') || !Between(c2, '0', '9')) { - String* result; - StringTable* table = isolate->heap()->string_table(); - if (table->LookupTwoCharsStringIfExists(c1, c2, &result)) { - return handle(result); + Handle result; + if (StringTable::LookupTwoCharsStringIfExists(isolate, c1, c2). + ToHandle(&result)) { + return result; } } diff --git a/src/heap.cc b/src/heap.cc index f306bcfc03..ab12a6484e 100644 --- a/src/heap.cc +++ b/src/heap.cc @@ -4656,15 +4656,6 @@ void Heap::Verify() { #endif -bool Heap::InternalizeStringIfExists(String* string, String** result) { - if (string->IsInternalizedString()) { - *result = string; - return true; - } - return string_table()->LookupStringIfExists(string, result); -} - - void Heap::ZapFromSpace() { NewSpacePageIterator it(new_space_.FromSpaceStart(), new_space_.FromSpaceEnd()); @@ -6246,12 +6237,11 @@ void KeyedLookupCache::Update(Handle map, int field_offset) { DisallowHeapAllocation no_gc; if (!name->IsUniqueName()) { - String* internalized_string; - if (!map->GetIsolate()->heap()->InternalizeStringIfExists( - String::cast(*name), &internalized_string)) { + if (!StringTable::InternalizeStringIfExists(name->GetIsolate(), + Handle::cast(name)). + ToHandle(&name)) { return; } - name = handle(internalized_string); } // This cache is cleared only between mark compact passes, so we expect the // cache to only contain old space names. diff --git a/src/heap.h b/src/heap.h index 2821eaf8ca..44e1b94593 100644 --- a/src/heap.h +++ b/src/heap.h @@ -730,8 +730,6 @@ class Heap { // Maintain marking consistency for IncrementalMarking. void AdjustLiveBytes(Address address, int by, InvocationMode mode); - bool InternalizeStringIfExists(String* str, String** result); - // Converts the given boolean condition to JavaScript boolean value. inline Object* ToBoolean(bool condition); diff --git a/src/objects.cc b/src/objects.cc index 363d1054e8..b37cf3b43c 100644 --- a/src/objects.cc +++ b/src/objects.cc @@ -15416,35 +15416,45 @@ class TwoCharHashTableKey : public HashTableKey { }; -bool StringTable::LookupStringIfExists(String* string, String** result) { - SLOW_ASSERT(this == HeapObject::cast(this)->GetHeap()->string_table()); - DisallowHeapAllocation no_alloc; - // TODO(ishell): Handlify all the callers and remove this scope. - HandleScope scope(GetIsolate()); - InternalizedStringKey key(handle(string)); - int entry = FindEntry(&key); +MaybeHandle StringTable::InternalizeStringIfExists( + Isolate* isolate, + Handle string) { + if (string->IsInternalizedString()) { + return string; + } + return LookupStringIfExists(isolate, string); +} + + +MaybeHandle StringTable::LookupStringIfExists( + Isolate* isolate, + Handle string) { + Handle string_table = isolate->factory()->string_table(); + InternalizedStringKey key(string); + int entry = string_table->FindEntry(&key); if (entry == kNotFound) { - return false; + return MaybeHandle(); } else { - *result = String::cast(KeyAt(entry)); + Handle result(String::cast(string_table->KeyAt(entry)), isolate); ASSERT(StringShape(*result).IsInternalized()); - return true; + return result; } } -bool StringTable::LookupTwoCharsStringIfExists(uint16_t c1, - uint16_t c2, - String** result) { - SLOW_ASSERT(this == HeapObject::cast(this)->GetHeap()->string_table()); - TwoCharHashTableKey key(c1, c2, GetHeap()->HashSeed()); - int entry = FindEntry(&key); +MaybeHandle StringTable::LookupTwoCharsStringIfExists( + Isolate* isolate, + uint16_t c1, + uint16_t c2) { + Handle string_table = isolate->factory()->string_table(); + TwoCharHashTableKey key(c1, c2, isolate->heap()->HashSeed()); + int entry = string_table->FindEntry(&key); if (entry == kNotFound) { - return false; + return MaybeHandle(); } else { - *result = String::cast(KeyAt(entry)); + Handle result(String::cast(string_table->KeyAt(entry)), isolate); ASSERT(StringShape(*result).IsInternalized()); - return true; + return result; } } diff --git a/src/objects.h b/src/objects.h index f5167b041f..0291e850b9 100644 --- a/src/objects.h +++ b/src/objects.h @@ -3741,8 +3741,6 @@ class SeqOneByteString; // // No special elements in the prefix and the element size is 1 // because only the string itself (the key) needs to be stored. -// TODO(ishell): Make StringTable a singleton class and move -// Heap::InternalizeStringXX() methods here. class StringTable: public HashTable { @@ -3752,11 +3750,21 @@ class StringTable: public HashTable LookupString(Isolate* isolate, Handle key); static Handle LookupKey(Isolate* isolate, HashTableKey* key); + // Tries to internalize given string and returns string handle on success + // or an empty handle otherwise. + MUST_USE_RESULT static MaybeHandle InternalizeStringIfExists( + Isolate* isolate, + Handle string); + // Looks up a string that is equal to the given string and returns - // true if it is found, assigning the string to the given output - // parameter. - bool LookupStringIfExists(String* str, String** result); - bool LookupTwoCharsStringIfExists(uint16_t c1, uint16_t c2, String** result); + // string handle if it is found, or an empty handle otherwise. + MUST_USE_RESULT static MaybeHandle LookupStringIfExists( + Isolate* isolate, + Handle str); + MUST_USE_RESULT static MaybeHandle LookupTwoCharsStringIfExists( + Isolate* isolate, + uint16_t c1, + uint16_t c2); // Casting. static inline StringTable* cast(Object* obj); diff --git a/src/scopeinfo.cc b/src/scopeinfo.cc index daa60c9f3d..1ed7e0b777 100644 --- a/src/scopeinfo.cc +++ b/src/scopeinfo.cc @@ -437,14 +437,14 @@ void ContextSlotCache::Update(Handle data, InitializationFlag init_flag, int slot_index) { DisallowHeapAllocation no_gc; - String* internalized_name; + Handle internalized_name; ASSERT(slot_index > kNotFound); - if (name->GetIsolate()->heap()->InternalizeStringIfExists( - *name, &internalized_name)) { - int index = Hash(*data, internalized_name); + if (StringTable::InternalizeStringIfExists(name->GetIsolate(), name). + ToHandle(&internalized_name)) { + int index = Hash(*data, *internalized_name); Key& key = keys_[index]; key.data = *data; - key.name = internalized_name; + key.name = *internalized_name; // Please note value only takes a uint as index. values_[index] = Value(mode, init_flag, slot_index - kNotFound).raw(); #ifdef DEBUG @@ -467,9 +467,9 @@ void ContextSlotCache::ValidateEntry(Handle data, InitializationFlag init_flag, int slot_index) { DisallowHeapAllocation no_gc; - String* internalized_name; - if (name->GetIsolate()->heap()->InternalizeStringIfExists( - *name, &internalized_name)) { + Handle internalized_name; + if (StringTable::InternalizeStringIfExists(name->GetIsolate(), name). + ToHandle(&internalized_name)) { int index = Hash(*data, *name); Key& key = keys_[index]; ASSERT(key.data == *data);