V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
compilation-cache.h
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  return key->IsMatch(value);
21  }
22 
23  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 InfoCellPair {
41  public:
42  InfoCellPair() : shared_(nullptr), feedback_cell_(nullptr) {}
43  InfoCellPair(SharedFunctionInfo* shared, FeedbackCell* feedback_cell)
44  : shared_(shared), feedback_cell_(feedback_cell) {}
45 
46  FeedbackCell* feedback_cell() const { return feedback_cell_; }
47  SharedFunctionInfo* shared() const { return shared_; }
48 
49  bool has_feedback_cell() const { return feedback_cell_ != nullptr; }
50  bool has_shared() const { return shared_ != nullptr; }
51 
52  private:
53  SharedFunctionInfo* shared_;
54  FeedbackCell* feedback_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.
69  : public HashTable<CompilationCacheTable, CompilationCacheShape> {
70  public:
71  NEVER_READ_ONLY_SPACE
72  static MaybeHandle<SharedFunctionInfo> LookupScript(
74  Handle<Context> native_context, LanguageMode language_mode);
75  static InfoCellPair LookupEval(Handle<CompilationCacheTable> table,
76  Handle<String> src,
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> PutScript(
83  Handle<Context> native_context, LanguageMode language_mode,
85  static Handle<CompilationCacheTable> PutEval(
88  Handle<Context> native_context, Handle<FeedbackCell> feedback_cell,
89  int position);
90  static Handle<CompilationCacheTable> PutRegExp(
92  JSRegExp::Flags flags, Handle<FixedArray> value);
93  void Remove(Object* value);
94  void Age();
95  static const int kHashGenerations = 10;
96 
97  DECL_CAST2(CompilationCacheTable)
98 
99  private:
100  OBJECT_CONSTRUCTORS(CompilationCacheTable,
102 };
103 
104 } // namespace internal
105 } // namespace v8
106 
107 #include "src/objects/object-macros-undef.h"
108 
109 #endif // V8_OBJECTS_COMPILATION_CACHE_H_
Definition: libplatform.h:13