diff --git a/src/profile-generator-inl.h b/src/profile-generator-inl.h index ac23ee3f01..e363f67761 100644 --- a/src/profile-generator-inl.h +++ b/src/profile-generator-inl.h @@ -33,16 +33,6 @@ namespace v8 { namespace internal { -const char* StringsStorage::GetFunctionName(Name* name) { - return GetFunctionName(GetName(name)); -} - - -const char* StringsStorage::GetFunctionName(const char* name) { - return strlen(name) > 0 ? name : ProfileGenerator::kAnonymousFunctionName; -} - - CodeEntry::CodeEntry(Logger::LogEventsAndTags tag, const char* name, const char* name_prefix, diff --git a/src/profile-generator.cc b/src/profile-generator.cc index cf268afc9b..acf54da1c7 100644 --- a/src/profile-generator.cc +++ b/src/profile-generator.cc @@ -41,6 +41,12 @@ namespace v8 { namespace internal { +bool StringsStorage::StringsMatch(void* key1, void* key2) { + return strcmp(reinterpret_cast(key1), + reinterpret_cast(key2)) == 0; +} + + StringsStorage::StringsStorage(Heap* heap) : hash_seed_(heap->HashSeed()), names_(StringsMatch) { } @@ -57,12 +63,15 @@ StringsStorage::~StringsStorage() { const char* StringsStorage::GetCopy(const char* src) { int len = static_cast(strlen(src)); - Vector dst = Vector::New(len + 1); - OS::StrNCpy(dst, src, len); - dst[len] = '\0'; - uint32_t hash = - StringHasher::HashSequentialString(dst.start(), len, hash_seed_); - return AddOrDisposeString(dst.start(), hash); + HashMap::Entry* entry = GetEntry(src, len); + if (entry->value == NULL) { + Vector dst = Vector::New(len + 1); + OS::StrNCpy(dst, src, len); + dst[len] = '\0'; + entry->key = dst.start(); + entry->value = entry->key; + } + return reinterpret_cast(entry->value); } @@ -75,15 +84,16 @@ const char* StringsStorage::GetFormatted(const char* format, ...) { } -const char* StringsStorage::AddOrDisposeString(char* str, uint32_t hash) { - HashMap::Entry* cache_entry = names_.Lookup(str, hash, true); - if (cache_entry->value == NULL) { +const char* StringsStorage::AddOrDisposeString(char* str, int len) { + HashMap::Entry* entry = GetEntry(str, len); + if (entry->value == NULL) { // New entry added. - cache_entry->value = str; + entry->key = str; + entry->value = str; } else { DeleteArray(str); } - return reinterpret_cast(cache_entry->value); + return reinterpret_cast(entry->value); } @@ -92,11 +102,9 @@ const char* StringsStorage::GetVFormatted(const char* format, va_list args) { int len = OS::VSNPrintF(str, format, args); if (len == -1) { DeleteArray(str.start()); - return format; + return GetCopy(format); } - uint32_t hash = StringHasher::HashSequentialString( - str.start(), len, hash_seed_); - return AddOrDisposeString(str.start(), hash); + return AddOrDisposeString(str.start(), len); } @@ -104,11 +112,11 @@ const char* StringsStorage::GetName(Name* name) { if (name->IsString()) { String* str = String::cast(name); int length = Min(kMaxNameSize, str->length()); + int actual_length = 0; SmartArrayPointer data = - str->ToCString(DISALLOW_NULLS, ROBUST_STRING_TRAVERSAL, 0, length); - uint32_t hash = StringHasher::HashSequentialString( - *data, length, name->GetHeap()->HashSeed()); - return AddOrDisposeString(data.Detach(), hash); + str->ToCString(DISALLOW_NULLS, ROBUST_STRING_TRAVERSAL, 0, length, + &actual_length); + return AddOrDisposeString(data.Detach(), actual_length); } else if (name->IsSymbol()) { return ""; } @@ -121,6 +129,21 @@ const char* StringsStorage::GetName(int index) { } +const char* StringsStorage::GetFunctionName(Name* name) { + return BeautifyFunctionName(GetName(name)); +} + + +const char* StringsStorage::GetFunctionName(const char* name) { + return BeautifyFunctionName(GetCopy(name)); +} + + +const char* StringsStorage::BeautifyFunctionName(const char* name) { + return (*name == 0) ? ProfileGenerator::kAnonymousFunctionName : name; +} + + size_t StringsStorage::GetUsedMemorySize() const { size_t size = sizeof(*this); size += sizeof(HashMap::Entry) * names_.capacity(); @@ -131,6 +154,12 @@ size_t StringsStorage::GetUsedMemorySize() const { } +HashMap::Entry* StringsStorage::GetEntry(const char* str, int len) { + uint32_t hash = StringHasher::HashSequentialString(str, len, hash_seed_); + return names_.Lookup(const_cast(str), hash, true); +} + + const char* const CodeEntry::kEmptyNamePrefix = ""; const char* const CodeEntry::kEmptyResourceName = ""; const char* const CodeEntry::kEmptyBailoutReason = ""; diff --git a/src/profile-generator.h b/src/profile-generator.h index 890fb2e5f8..6e4758bece 100644 --- a/src/profile-generator.h +++ b/src/profile-generator.h @@ -49,20 +49,18 @@ class StringsStorage { const char* GetVFormatted(const char* format, va_list args); const char* GetName(Name* name); const char* GetName(int index); - inline const char* GetFunctionName(Name* name); - inline const char* GetFunctionName(const char* name); + const char* GetFunctionName(Name* name); + const char* GetFunctionName(const char* name); size_t GetUsedMemorySize() const; private: static const int kMaxNameSize = 1024; - static bool StringsMatch(void* key1, void* key2) { - return strcmp(reinterpret_cast(key1), - reinterpret_cast(key2)) == 0; - } - const char* AddOrDisposeString(char* str, uint32_t hash); + static bool StringsMatch(void* key1, void* key2); + const char* BeautifyFunctionName(const char* name); + const char* AddOrDisposeString(char* str, int len); + HashMap::Entry* GetEntry(const char* str, int len); - // Mapping of strings by String::Hash to const char* strings. uint32_t hash_seed_; HashMap names_;