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;
ShellOptions Shell::options;
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.
class DummySourceStream : public v8::ScriptCompiler::ExternalSourceStream {
public:
......@@ -1629,25 +1623,15 @@ void Shell::MapCounters(v8::Isolate* isolate, const char* name) {
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* 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) {
counter = counters_->GetNextCounter();
if (counter != nullptr) {
counter_map_->Set(name, counter);
(*counter_map_)[name] = counter;
counter->Bind(name, is_histogram);
}
} else {
......@@ -2083,15 +2067,13 @@ void Shell::OnExit(v8::Isolate* isolate) {
isolate->Dispose();
if (i::FLAG_dump_counters || i::FLAG_dump_counters_nvp) {
int number_of_counters = 0;
for (CounterMap::Iterator i(counter_map_); i.More(); i.Next()) {
number_of_counters++;
}
const int number_of_counters = static_cast<int>(counter_map_->size());
CounterAndKey* counters = new CounterAndKey[number_of_counters];
int j = 0;
for (CounterMap::Iterator i(counter_map_); i.More(); i.Next(), j++) {
counters[j].counter = i.CurrentValue();
counters[j].key = i.CurrentKey();
for (auto map_entry : *counter_map_) {
counters[j].counter = map_entry.second;
counters[j].key = map_entry.first;
j++;
}
std::sort(counters, counters + number_of_counters);
......
......@@ -9,10 +9,10 @@
#include <map>
#include <memory>
#include <string>
#include <unordered_map>
#include <vector>
#include "src/allocation.h"
#include "src/base/hashmap.h"
#include "src/base/platform/time.h"
#include "src/utils.h"
......@@ -56,41 +56,26 @@ class CounterCollection {
Counter counters_[kMaxCounters];
};
class CounterMap {
public:
CounterMap(): hash_map_(Match) { }
Counter* Lookup(const char* name) {
base::HashMap::Entry* answer =
hash_map_.Lookup(const_cast<char*>(name), Hash(name));
if (!answer) return nullptr;
return reinterpret_cast<Counter*>(answer->value);
struct StringHasher {
std::size_t operator()(const char* name) const {
size_t h = 0;
size_t c;
while ((c = *name++) != 0) {
h += h << 5;
h += c;
}
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;
return h;
}
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:
static int Hash(const char* name);
static bool Match(void* key1, void* key2);
base::CustomMatcherHashMap hash_map_;
struct StringEquals {
bool operator()(const char* name1, const char* name2) const {
return strcmp(name1, name2) == 0;
}
};
typedef std::unordered_map<const char*, Counter*, StringHasher, StringEquals>
CounterMap;
class SourceGroup {
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