| OLD | NEW |
| 1 // Copyright 2014 the V8 project authors. All rights reserved. | 1 // Copyright 2014 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "src/compiler/node-cache.h" | 5 #include "src/compiler/node-cache.h" |
| 6 | 6 |
| 7 #include "src/zone.h" |
| 8 |
| 7 namespace v8 { | 9 namespace v8 { |
| 8 namespace internal { | 10 namespace internal { |
| 9 namespace compiler { | 11 namespace compiler { |
| 10 | 12 |
| 11 #define INITIAL_SIZE 16 | 13 #define INITIAL_SIZE 16 |
| 12 #define LINEAR_PROBE 5 | 14 #define LINEAR_PROBE 5 |
| 13 | 15 |
| 14 template <typename Key> | 16 template <typename Key> |
| 15 int32_t NodeCacheHash(Key key) { | 17 inline int NodeCacheHash(Key key); |
| 16 UNIMPLEMENTED(); | 18 |
| 17 return 0; | |
| 18 } | |
| 19 | 19 |
| 20 template <> | 20 template <> |
| 21 inline int32_t NodeCacheHash(int32_t key) { | 21 inline int NodeCacheHash(int32_t key) { |
| 22 return ComputeIntegerHash(key, 0); | 22 return ComputeIntegerHash(key, 0); |
| 23 } | 23 } |
| 24 | 24 |
| 25 | 25 |
| 26 template <> | 26 template <> |
| 27 inline int32_t NodeCacheHash(int64_t key) { | 27 inline int NodeCacheHash(int64_t key) { |
| 28 return ComputeLongHash(key); | 28 return ComputeLongHash(key); |
| 29 } | 29 } |
| 30 | 30 |
| 31 | 31 |
| 32 template <> | |
| 33 inline int32_t NodeCacheHash(double key) { | |
| 34 return ComputeLongHash(BitCast<int64_t>(key)); | |
| 35 } | |
| 36 | |
| 37 | |
| 38 template <> | |
| 39 inline int32_t NodeCacheHash(void* key) { | |
| 40 return ComputePointerHash(key); | |
| 41 } | |
| 42 | |
| 43 | |
| 44 template <typename Key> | 32 template <typename Key> |
| 45 bool NodeCache<Key>::Resize(Zone* zone) { | 33 bool NodeCache<Key>::Resize(Zone* zone) { |
| 46 if (size_ >= max_) return false; // Don't grow past the maximum size. | 34 if (size_ >= max_) return false; // Don't grow past the maximum size. |
| 47 | 35 |
| 48 // Allocate a new block of entries 4x the size. | 36 // Allocate a new block of entries 4x the size. |
| 49 Entry* old_entries = entries_; | 37 Entry* old_entries = entries_; |
| 50 int old_size = size_ + LINEAR_PROBE; | 38 int old_size = size_ + LINEAR_PROBE; |
| 51 size_ = size_ * 4; | 39 size_ = size_ * 4; |
| 52 int num_entries = size_ + LINEAR_PROBE; | 40 int num_entries = size_ + LINEAR_PROBE; |
| 53 entries_ = zone->NewArray<Entry>(num_entries); | 41 entries_ = zone->NewArray<Entry>(num_entries); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 69 } | 57 } |
| 70 } | 58 } |
| 71 } | 59 } |
| 72 } | 60 } |
| 73 return true; | 61 return true; |
| 74 } | 62 } |
| 75 | 63 |
| 76 | 64 |
| 77 template <typename Key> | 65 template <typename Key> |
| 78 Node** NodeCache<Key>::Find(Zone* zone, Key key) { | 66 Node** NodeCache<Key>::Find(Zone* zone, Key key) { |
| 79 int32_t hash = NodeCacheHash(key); | 67 int hash = NodeCacheHash(key); |
| 80 if (entries_ == NULL) { | 68 if (entries_ == NULL) { |
| 81 // Allocate the initial entries and insert the first entry. | 69 // Allocate the initial entries and insert the first entry. |
| 82 int num_entries = INITIAL_SIZE + LINEAR_PROBE; | 70 int num_entries = INITIAL_SIZE + LINEAR_PROBE; |
| 83 entries_ = zone->NewArray<Entry>(num_entries); | 71 entries_ = zone->NewArray<Entry>(num_entries); |
| 84 size_ = INITIAL_SIZE; | 72 size_ = INITIAL_SIZE; |
| 85 memset(entries_, 0, sizeof(Entry) * num_entries); | 73 memset(entries_, 0, sizeof(Entry) * num_entries); |
| 86 Entry* entry = &entries_[hash & (INITIAL_SIZE - 1)]; | 74 Entry* entry = &entries_[hash & (INITIAL_SIZE - 1)]; |
| 87 entry->key_ = key; | 75 entry->key_ = key; |
| 88 return &entry->value_; | 76 return &entry->value_; |
| 89 } | 77 } |
| (...skipping 15 matching lines...) Expand all Loading... |
| 105 } | 93 } |
| 106 | 94 |
| 107 // If resized to maximum and still didn't find space, overwrite an entry. | 95 // If resized to maximum and still didn't find space, overwrite an entry. |
| 108 Entry* entry = &entries_[hash & (size_ - 1)]; | 96 Entry* entry = &entries_[hash & (size_ - 1)]; |
| 109 entry->key_ = key; | 97 entry->key_ = key; |
| 110 entry->value_ = NULL; | 98 entry->value_ = NULL; |
| 111 return &entry->value_; | 99 return &entry->value_; |
| 112 } | 100 } |
| 113 | 101 |
| 114 | 102 |
| 103 template class NodeCache<int32_t>; |
| 115 template class NodeCache<int64_t>; | 104 template class NodeCache<int64_t>; |
| 116 template class NodeCache<int32_t>; | 105 |
| 117 template class NodeCache<void*>; | 106 } // namespace compiler |
| 118 } | 107 } // namespace internal |
| 119 } | 108 } // namespace v8 |
| 120 } // namespace v8::internal::compiler | |
| OLD | NEW |