Commit 1961008a authored by Peter Marshall's avatar Peter Marshall Committed by Commit Bot

[cleanup] Change CounterMap to use unordered map.

Trying to reduce use of our self-baked data structures.

Bug: v8:7570
Change-Id: Ie4257911b388d320e4c5da5108cd763d3ab9299f
Reviewed-on: https://chromium-review.googlesource.com/1032555Reviewed-by: 's avatarMichael Starzinger <mstarzinger@chromium.org>
Commit-Queue: Peter Marshall <petermarshall@chromium.org>
Cr-Commit-Position: refs/heads/master@{#52840}
parent b9dd620b
...@@ -480,12 +480,6 @@ ArrayBuffer::Allocator* Shell::array_buffer_allocator; ...@@ -480,12 +480,6 @@ ArrayBuffer::Allocator* Shell::array_buffer_allocator;
ShellOptions Shell::options; ShellOptions Shell::options;
base::OnceType Shell::quit_once_ = V8_ONCE_INIT; base::OnceType Shell::quit_once_ = V8_ONCE_INIT;
bool CounterMap::Match(void* key1, void* key2) {
const char* name1 = reinterpret_cast<const char*>(key1);
const char* name2 = reinterpret_cast<const char*>(key2);
return strcmp(name1, name2) == 0;
}
// Dummy external source stream which returns the whole source in one go. // Dummy external source stream which returns the whole source in one go.
class DummySourceStream : public v8::ScriptCompiler::ExternalSourceStream { class DummySourceStream : public v8::ScriptCompiler::ExternalSourceStream {
public: public:
...@@ -1629,25 +1623,15 @@ void Shell::MapCounters(v8::Isolate* isolate, const char* name) { ...@@ -1629,25 +1623,15 @@ void Shell::MapCounters(v8::Isolate* isolate, const char* name) {
isolate->SetAddHistogramSampleFunction(AddHistogramSample); isolate->SetAddHistogramSampleFunction(AddHistogramSample);
} }
int CounterMap::Hash(const char* name) {
int h = 0;
int c;
while ((c = *name++) != 0) {
h += h << 5;
h += c;
}
return h;
}
Counter* Shell::GetCounter(const char* name, bool is_histogram) { Counter* Shell::GetCounter(const char* name, bool is_histogram) {
Counter* counter = counter_map_->Lookup(name); auto map_entry = counter_map_->find(name);
Counter* counter =
map_entry != counter_map_->end() ? map_entry->second : nullptr;
if (counter == nullptr) { if (counter == nullptr) {
counter = counters_->GetNextCounter(); counter = counters_->GetNextCounter();
if (counter != nullptr) { if (counter != nullptr) {
counter_map_->Set(name, counter); (*counter_map_)[name] = counter;
counter->Bind(name, is_histogram); counter->Bind(name, is_histogram);
} }
} else { } else {
...@@ -2083,15 +2067,13 @@ void Shell::OnExit(v8::Isolate* isolate) { ...@@ -2083,15 +2067,13 @@ void Shell::OnExit(v8::Isolate* isolate) {
isolate->Dispose(); isolate->Dispose();
if (i::FLAG_dump_counters || i::FLAG_dump_counters_nvp) { if (i::FLAG_dump_counters || i::FLAG_dump_counters_nvp) {
int number_of_counters = 0; const int number_of_counters = static_cast<int>(counter_map_->size());
for (CounterMap::Iterator i(counter_map_); i.More(); i.Next()) {
number_of_counters++;
}
CounterAndKey* counters = new CounterAndKey[number_of_counters]; CounterAndKey* counters = new CounterAndKey[number_of_counters];
int j = 0; int j = 0;
for (CounterMap::Iterator i(counter_map_); i.More(); i.Next(), j++) { for (auto map_entry : *counter_map_) {
counters[j].counter = i.CurrentValue(); counters[j].counter = map_entry.second;
counters[j].key = i.CurrentKey(); counters[j].key = map_entry.first;
j++;
} }
std::sort(counters, counters + number_of_counters); std::sort(counters, counters + number_of_counters);
......
...@@ -9,10 +9,10 @@ ...@@ -9,10 +9,10 @@
#include <map> #include <map>
#include <memory> #include <memory>
#include <string> #include <string>
#include <unordered_map>
#include <vector> #include <vector>
#include "src/allocation.h" #include "src/allocation.h"
#include "src/base/hashmap.h"
#include "src/base/platform/time.h" #include "src/base/platform/time.h"
#include "src/utils.h" #include "src/utils.h"
...@@ -56,41 +56,26 @@ class CounterCollection { ...@@ -56,41 +56,26 @@ class CounterCollection {
Counter counters_[kMaxCounters]; Counter counters_[kMaxCounters];
}; };
struct StringHasher {
class CounterMap { std::size_t operator()(const char* name) const {
public: size_t h = 0;
CounterMap(): hash_map_(Match) { } size_t c;
Counter* Lookup(const char* name) { while ((c = *name++) != 0) {
base::HashMap::Entry* answer = h += h << 5;
hash_map_.Lookup(const_cast<char*>(name), Hash(name)); h += c;
if (!answer) return nullptr; }
return reinterpret_cast<Counter*>(answer->value); return h;
}
void Set(const char* name, Counter* value) {
base::HashMap::Entry* answer =
hash_map_.LookupOrInsert(const_cast<char*>(name), Hash(name));
DCHECK_NOT_NULL(answer);
answer->value = value;
} }
class Iterator { };
public:
explicit Iterator(CounterMap* map)
: map_(&map->hash_map_), entry_(map_->Start()) { }
void Next() { entry_ = map_->Next(entry_); }
bool More() { return entry_ != nullptr; }
const char* CurrentKey() { return static_cast<const char*>(entry_->key); }
Counter* CurrentValue() { return static_cast<Counter*>(entry_->value); }
private:
base::CustomMatcherHashMap* map_;
base::CustomMatcherHashMap::Entry* entry_;
};
private: struct StringEquals {
static int Hash(const char* name); bool operator()(const char* name1, const char* name2) const {
static bool Match(void* key1, void* key2); return strcmp(name1, name2) == 0;
base::CustomMatcherHashMap hash_map_; }
}; };
typedef std::unordered_map<const char*, Counter*, StringHasher, StringEquals>
CounterMap;
class SourceGroup { class SourceGroup {
public: public:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment