Line data Source code
1 : // Copyright 2017 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_OBJECTS_COMPILATION_CACHE_H_
6 : #define V8_OBJECTS_COMPILATION_CACHE_H_
7 :
8 : #include "src/objects/hash-table.h"
9 : #include "src/objects/js-regexp.h"
10 :
11 : // Has to be the last include (doesn't have include guards):
12 : #include "src/objects/object-macros.h"
13 :
14 : namespace v8 {
15 : namespace internal {
16 :
17 : class CompilationCacheShape : public BaseShape<HashTableKey*> {
18 : public:
19 : static inline bool IsMatch(HashTableKey* key, Object* value) {
20 6978653 : return key->IsMatch(value);
21 : }
22 :
23 5841137 : static inline uint32_t Hash(Isolate* isolate, HashTableKey* key) {
24 : return key->Hash();
25 : }
26 :
27 : static inline uint32_t RegExpHash(String* string, Smi* flags);
28 :
29 : static inline uint32_t StringSharedHash(String* source,
30 : SharedFunctionInfo* shared,
31 : LanguageMode language_mode,
32 : int position);
33 :
34 : static inline uint32_t HashForObject(Isolate* isolate, Object* object);
35 :
36 : static const int kPrefixSize = 0;
37 : static const int kEntrySize = 3;
38 : };
39 :
40 : class InfoVectorPair {
41 : public:
42 : InfoVectorPair() : shared_(nullptr), vector_cell_(nullptr) {}
43 : InfoVectorPair(SharedFunctionInfo* shared, Cell* vector_cell)
44 : : shared_(shared), vector_cell_(vector_cell) {}
45 :
46 : SharedFunctionInfo* shared() const { return shared_; }
47 : Cell* vector() const { return vector_cell_; }
48 :
49 5 : bool has_shared() const { return shared_ != nullptr; }
50 : bool has_vector() const { return vector_cell_ != nullptr; }
51 :
52 : private:
53 : SharedFunctionInfo* shared_;
54 : Cell* vector_cell_;
55 : };
56 :
57 : // This cache is used in two different variants. For regexp caching, it simply
58 : // maps identifying info of the regexp to the cached regexp object. Scripts and
59 : // eval code only gets cached after a second probe for the code object. To do
60 : // so, on first "put" only a hash identifying the source is entered into the
61 : // cache, mapping it to a lifetime count of the hash. On each call to Age all
62 : // such lifetimes get reduced, and removed once they reach zero. If a second put
63 : // is called while such a hash is live in the cache, the hash gets replaced by
64 : // an actual cache entry. Age also removes stale live entries from the cache.
65 : // Such entries are identified by SharedFunctionInfos pointing to either the
66 : // recompilation stub, or to "old" code. This avoids memory leaks due to
67 : // premature caching of scripts and eval strings that are never needed later.
68 : class CompilationCacheTable
69 : : public HashTable<CompilationCacheTable, CompilationCacheShape> {
70 : public:
71 : // Find cached value for a string key, otherwise return null.
72 : Handle<Object> Lookup(Handle<String> src, Handle<Context> context,
73 : LanguageMode language_mode);
74 : InfoVectorPair LookupScript(Handle<String> src, Handle<Context> context,
75 : LanguageMode language_mode);
76 : InfoVectorPair LookupEval(Handle<String> src,
77 : Handle<SharedFunctionInfo> shared,
78 : Handle<Context> native_context,
79 : LanguageMode language_mode, int position);
80 : Handle<Object> LookupRegExp(Handle<String> source, JSRegExp::Flags flags);
81 : static Handle<CompilationCacheTable> Put(Handle<CompilationCacheTable> cache,
82 : Handle<String> src,
83 : Handle<Context> context,
84 : LanguageMode language_mode,
85 : Handle<Object> value);
86 : static Handle<CompilationCacheTable> PutScript(
87 : Handle<CompilationCacheTable> cache, Handle<String> src,
88 : Handle<Context> context, LanguageMode language_mode,
89 : Handle<SharedFunctionInfo> value, Handle<Cell> literals);
90 : static Handle<CompilationCacheTable> PutEval(
91 : Handle<CompilationCacheTable> cache, Handle<String> src,
92 : Handle<SharedFunctionInfo> outer_info, Handle<SharedFunctionInfo> value,
93 : Handle<Context> native_context, Handle<Cell> literals, int position);
94 : static Handle<CompilationCacheTable> PutRegExp(
95 : Handle<CompilationCacheTable> cache, Handle<String> src,
96 : JSRegExp::Flags flags, Handle<FixedArray> value);
97 : void Remove(Object* value);
98 : void Age();
99 : static const int kHashGenerations = 10;
100 :
101 : DECL_CAST(CompilationCacheTable)
102 :
103 : private:
104 : DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationCacheTable);
105 : };
106 :
107 : } // namespace internal
108 : } // namespace v8
109 :
110 : #include "src/objects/object-macros-undef.h"
111 :
112 : #endif // V8_OBJECTS_COMPILATION_CACHE_H_
|