Line data Source code
1 : // Copyright 2012 the V8 project authors. All rights reserved.
2 : // Use of this source code is governed by a BSD-style license that can be
3 : // found in the LICENSE file.
4 :
5 : #ifndef V8_IC_STUB_CACHE_H_
6 : #define V8_IC_STUB_CACHE_H_
7 :
8 : #include "src/objects/name.h"
9 :
10 : namespace v8 {
11 : namespace internal {
12 :
13 : // The stub cache is used for megamorphic property accesses.
14 : // It maps (map, name, type) to property access handlers. The cache does not
15 : // need explicit invalidation when a prototype chain is modified, since the
16 : // handlers verify the chain.
17 :
18 :
19 : class SCTableReference {
20 : public:
21 568 : Address address() const { return address_; }
22 :
23 : private:
24 : explicit SCTableReference(Address address) : address_(address) {}
25 :
26 : Address address_;
27 :
28 : friend class StubCache;
29 : };
30 :
31 : class V8_EXPORT_PRIVATE StubCache {
32 : public:
33 : struct Entry {
34 : // The values here have plain Address types because they are read
35 : // directly from generated code. As a nice side effect, this keeps
36 : // #includes lightweight.
37 : Address key;
38 : // {value} is a tagged heap object reference (weak or strong), equivalent
39 : // to a MaybeObject's payload.
40 : Address value;
41 : // {map} is a tagged Map pointer, or nullptr.
42 : Address map;
43 : };
44 :
45 : void Initialize();
46 : // Access cache for entry hash(name, map).
47 : void Set(Name name, Map map, MaybeObject handler);
48 : MaybeObject Get(Name name, Map map);
49 : // Clear the lookup table (@ mark compact collection).
50 : void Clear();
51 :
52 : enum Table { kPrimary, kSecondary };
53 :
54 : SCTableReference key_reference(StubCache::Table table) {
55 : return SCTableReference(
56 250336 : reinterpret_cast<Address>(&first_entry(table)->key));
57 : }
58 :
59 : SCTableReference map_reference(StubCache::Table table) {
60 : return SCTableReference(
61 249768 : reinterpret_cast<Address>(&first_entry(table)->map));
62 : }
63 :
64 : SCTableReference value_reference(StubCache::Table table) {
65 : return SCTableReference(
66 249768 : reinterpret_cast<Address>(&first_entry(table)->value));
67 : }
68 :
69 : StubCache::Entry* first_entry(StubCache::Table table) {
70 568 : switch (table) {
71 : case StubCache::kPrimary:
72 284 : return StubCache::primary_;
73 : case StubCache::kSecondary:
74 284 : return StubCache::secondary_;
75 : }
76 0 : UNREACHABLE();
77 : }
78 :
79 : Isolate* isolate() { return isolate_; }
80 :
81 : // Setting the entry size such that the index is shifted by Name::kHashShift
82 : // is convenient; shifting down the length field (to extract the hash code)
83 : // automatically discards the hash bit field.
84 : static const int kCacheIndexShift = Name::kHashShift;
85 :
86 : static const int kPrimaryTableBits = 11;
87 : static const int kPrimaryTableSize = (1 << kPrimaryTableBits);
88 : static const int kSecondaryTableBits = 9;
89 : static const int kSecondaryTableSize = (1 << kSecondaryTableBits);
90 :
91 : // We compute the hash code for a map as follows:
92 : // <code> = <address> ^ (<address> >> kMapKeyShift)
93 : static const int kMapKeyShift = kPrimaryTableBits + kCacheIndexShift;
94 :
95 : // Some magic number used in the secondary hash computation.
96 : static const int kSecondaryMagic = 0xb16ca6e5;
97 :
98 : static int PrimaryOffsetForTesting(Name name, Map map);
99 : static int SecondaryOffsetForTesting(Name name, int seed);
100 :
101 : // The constructor is made public only for the purposes of testing.
102 : explicit StubCache(Isolate* isolate);
103 :
104 : private:
105 : // The stub cache has a primary and secondary level. The two levels have
106 : // different hashing algorithms in order to avoid simultaneous collisions
107 : // in both caches. Unlike a probing strategy (quadratic or otherwise) the
108 : // update strategy on updates is fairly clear and simple: Any existing entry
109 : // in the primary cache is moved to the secondary cache, and secondary cache
110 : // entries are overwritten.
111 :
112 : // Hash algorithm for the primary table. This algorithm is replicated in
113 : // assembler for every architecture. Returns an index into the table that
114 : // is scaled by 1 << kCacheIndexShift.
115 : static int PrimaryOffset(Name name, Map map);
116 :
117 : // Hash algorithm for the secondary table. This algorithm is replicated in
118 : // assembler for every architecture. Returns an index into the table that
119 : // is scaled by 1 << kCacheIndexShift.
120 : static int SecondaryOffset(Name name, int seed);
121 :
122 : // Compute the entry for a given offset in exactly the same way as
123 : // we do in generated code. We generate an hash code that already
124 : // ends in Name::kHashShift 0s. Then we multiply it so it is a multiple
125 : // of sizeof(Entry). This makes it easier to avoid making mistakes
126 : // in the hashed offset computations.
127 : static Entry* entry(Entry* table, int offset) {
128 : const int multiplier = sizeof(*table) >> Name::kHashShift;
129 1138730 : return reinterpret_cast<Entry*>(reinterpret_cast<Address>(table) +
130 2277460 : offset * multiplier);
131 : }
132 :
133 : private:
134 : Entry primary_[kPrimaryTableSize];
135 : Entry secondary_[kSecondaryTableSize];
136 : Isolate* isolate_;
137 :
138 : friend class Isolate;
139 : friend class SCTableReference;
140 :
141 : DISALLOW_COPY_AND_ASSIGN(StubCache);
142 : };
143 : } // namespace internal
144 : } // namespace v8
145 :
146 : #endif // V8_IC_STUB_CACHE_H_
|