Index: src/compiler/node-cache.cc |
diff --git a/src/compiler/node-cache.cc b/src/compiler/node-cache.cc |
index 7cda167bac91dc153ee3a55425d8a7ffd001edd0..dbf61acc40fca7f0ef005947fb576503acedb84a 100644 |
--- a/src/compiler/node-cache.cc |
+++ b/src/compiler/node-cache.cc |
@@ -4,65 +4,43 @@ |
#include "src/compiler/node-cache.h" |
+#include <cstring> |
+ |
+#include "src/zone.h" |
+ |
namespace v8 { |
namespace internal { |
namespace compiler { |
-#define INITIAL_SIZE 16 |
-#define LINEAR_PROBE 5 |
- |
-template <typename Key> |
-int32_t NodeCacheHash(Key key) { |
- UNIMPLEMENTED(); |
- return 0; |
-} |
- |
-template <> |
-inline int32_t NodeCacheHash(int32_t key) { |
- return ComputeIntegerHash(key, 0); |
-} |
- |
- |
-template <> |
-inline int32_t NodeCacheHash(int64_t key) { |
- return ComputeLongHash(key); |
-} |
- |
- |
-template <> |
-inline int32_t NodeCacheHash(double key) { |
- return ComputeLongHash(bit_cast<int64_t>(key)); |
-} |
+template <typename Key, typename Hash, typename Pred> |
+struct NodeCache<Key, Hash, Pred>::Entry { |
+ Key key_; |
+ Node* value_; |
+}; |
-template <> |
-inline int32_t NodeCacheHash(void* key) { |
- return ComputePointerHash(key); |
-} |
- |
- |
-template <typename Key> |
-bool NodeCache<Key>::Resize(Zone* zone) { |
+template <typename Key, typename Hash, typename Pred> |
+bool NodeCache<Key, Hash, Pred>::Resize(Zone* zone) { |
if (size_ >= max_) return false; // Don't grow past the maximum size. |
// Allocate a new block of entries 4x the size. |
Entry* old_entries = entries_; |
- int old_size = size_ + LINEAR_PROBE; |
- size_ = size_ * 4; |
- int num_entries = size_ + LINEAR_PROBE; |
- entries_ = zone->NewArray<Entry>(num_entries); |
+ size_t old_size = size_ + kLinearProbe; |
+ size_ *= 4; |
+ size_t num_entries = size_ + kLinearProbe; |
+ entries_ = zone->NewArray<Entry>(static_cast<int>(num_entries)); |
memset(entries_, 0, sizeof(Entry) * num_entries); |
// Insert the old entries into the new block. |
- for (int i = 0; i < old_size; i++) { |
+ for (size_t i = 0; i < old_size; ++i) { |
Entry* old = &old_entries[i]; |
- if (old->value_ != NULL) { |
- int hash = NodeCacheHash(old->key_); |
- int start = hash & (size_ - 1); |
- int end = start + LINEAR_PROBE; |
- for (int j = start; j < end; j++) { |
+ if (old->value_) { |
+ size_t hash = hash_(old->key_); |
+ size_t start = hash & (size_ - 1); |
+ size_t end = start + kLinearProbe; |
+ for (size_t j = start; j < end; ++j) { |
Entry* entry = &entries_[j]; |
- if (entry->value_ == NULL) { |
+ if (!entry->value_) { |
entry->key_ = old->key_; |
entry->value_ = old->value_; |
break; |
@@ -74,28 +52,28 @@ bool NodeCache<Key>::Resize(Zone* zone) { |
} |
-template <typename Key> |
-Node** NodeCache<Key>::Find(Zone* zone, Key key) { |
- int32_t hash = NodeCacheHash(key); |
- if (entries_ == NULL) { |
+template <typename Key, typename Hash, typename Pred> |
+Node** NodeCache<Key, Hash, Pred>::Find(Zone* zone, Key key) { |
+ size_t hash = hash_(key); |
+ if (!entries_) { |
// Allocate the initial entries and insert the first entry. |
- int num_entries = INITIAL_SIZE + LINEAR_PROBE; |
- entries_ = zone->NewArray<Entry>(num_entries); |
- size_ = INITIAL_SIZE; |
+ size_t num_entries = kInitialSize + kLinearProbe; |
+ entries_ = zone->NewArray<Entry>(static_cast<int>(num_entries)); |
+ size_ = kInitialSize; |
memset(entries_, 0, sizeof(Entry) * num_entries); |
- Entry* entry = &entries_[hash & (INITIAL_SIZE - 1)]; |
+ Entry* entry = &entries_[hash & (kInitialSize - 1)]; |
entry->key_ = key; |
return &entry->value_; |
} |
- while (true) { |
+ for (;;) { |
// Search up to N entries after (linear probing). |
- int start = hash & (size_ - 1); |
- int end = start + LINEAR_PROBE; |
- for (int i = start; i < end; i++) { |
+ size_t start = hash & (size_ - 1); |
+ size_t end = start + kLinearProbe; |
+ for (size_t i = start; i < end; i++) { |
Entry* entry = &entries_[i]; |
- if (entry->key_ == key) return &entry->value_; |
- if (entry->value_ == NULL) { |
+ if (pred_(entry->key_, key)) return &entry->value_; |
+ if (!entry->value_) { |
entry->key_ = key; |
return &entry->value_; |
} |
@@ -107,7 +85,7 @@ Node** NodeCache<Key>::Find(Zone* zone, Key key) { |
// If resized to maximum and still didn't find space, overwrite an entry. |
Entry* entry = &entries_[hash & (size_ - 1)]; |
entry->key_ = key; |
- entry->value_ = NULL; |
+ entry->value_ = nullptr; |
return &entry->value_; |
} |
@@ -115,6 +93,7 @@ Node** NodeCache<Key>::Find(Zone* zone, Key key) { |
template class NodeCache<int64_t>; |
template class NodeCache<int32_t>; |
template class NodeCache<void*>; |
-} |
-} |
-} // namespace v8::internal::compiler |
+ |
+} // namespace compiler |
+} // namespace internal |
+} // namespace v8 |