compilation-cache.h 10.4 KB
Newer Older
1
// Copyright 2012 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4

5 6
#ifndef V8_CODEGEN_COMPILATION_CACHE_H_
#define V8_CODEGEN_COMPILATION_CACHE_H_
7

8
#include "src/base/hashmap.h"
9
#include "src/objects/compilation-cache.h"
10
#include "src/utils/allocation.h"
11

12 13
namespace v8 {
namespace internal {
14

marja's avatar
marja committed
15 16 17
template <typename T>
class Handle;

18 19
class RootVisitor;

20 21 22 23 24 25 26
// The compilation cache consists of several generational sub-caches which uses
// this class as a base class. A sub-cache contains a compilation cache tables
// for each generation of the sub-cache. Since the same source code string has
// different compiled code for scripts and evals, we use separate sub-caches
// for different compilation modes, to avoid retrieving the wrong result.
class CompilationSubCache {
 public:
27
  CompilationSubCache(Isolate* isolate, int generations)
28
      : isolate_(isolate), generations_(generations) {
29
    DCHECK_LE(generations, kMaxGenerations);
30 31
  }

32 33
  static constexpr int kFirstGeneration = 0;
  static constexpr int kMaxGenerations = 2;
34 35 36 37 38 39 40 41 42

  // Get the compilation cache tables for a specific generation.
  Handle<CompilationCacheTable> GetTable(int generation);

  // Accessors for first generation.
  Handle<CompilationCacheTable> GetFirstTable() {
    return GetTable(kFirstGeneration);
  }
  void SetFirstTable(Handle<CompilationCacheTable> value) {
43
    DCHECK_LT(kFirstGeneration, generations_);
44 45 46 47 48
    tables_[kFirstGeneration] = *value;
  }

  // Age the sub-cache by evicting the oldest generation and creating a new
  // young generation.
49
  virtual void Age() = 0;
50 51

  // GC support.
52
  void Iterate(RootVisitor* v);
53 54 55 56 57 58 59 60

  // Clear this sub-cache evicting all its content.
  void Clear();

  // Remove given shared function info from sub-cache.
  void Remove(Handle<SharedFunctionInfo> function_info);

  // Number of generations in this sub-cache.
61
  int generations() const { return generations_; }
62

63
 protected:
64 65 66 67 68 69
  Isolate* isolate() const { return isolate_; }

  // Ageing occurs either by removing the oldest generation, or with
  // custom logic implemented in CompilationCacheTable::Age.
  static void AgeByGeneration(CompilationSubCache* c);
  static void AgeCustom(CompilationSubCache* c);
70

71
 private:
72 73 74
  Isolate* const isolate_;
  const int generations_;
  Object tables_[kMaxGenerations];  // One for each generation.
75 76 77 78 79 80 81

  DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationSubCache);
};

// Sub-cache for scripts.
class CompilationCacheScript : public CompilationSubCache {
 public:
82
  explicit CompilationCacheScript(Isolate* isolate);
83

84 85 86 87
  MaybeHandle<SharedFunctionInfo> Lookup(Handle<String> source,
                                         MaybeHandle<Object> name,
                                         int line_offset, int column_offset,
                                         ScriptOriginOptions resource_options,
88
                                         Handle<Context> native_context,
89
                                         LanguageMode language_mode);
90 91

  void Put(Handle<String> source, Handle<Context> context,
92 93
           LanguageMode language_mode,
           Handle<SharedFunctionInfo> function_info);
94

95 96
  void Age() override;

97
 private:
98 99
  bool HasOrigin(Handle<SharedFunctionInfo> function_info,
                 MaybeHandle<Object> name, int line_offset, int column_offset,
100
                 ScriptOriginOptions resource_options);
101 102 103 104

  DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationCacheScript);
};

105
// Sub-cache for eval scripts. Two caches for eval are used. One for eval calls
106
// in native contexts and one for eval calls in other contexts. The cache
107 108 109 110
// considers the following pieces of information when checking for matching
// entries:
// 1. The source string.
// 2. The shared function info of the calling function.
111
// 3. Whether the source should be compiled as strict code or as sloppy code.
112
//    Note: Currently there are clients of CompileEval that always compile
113
//    sloppy code even if the calling function is a strict mode function.
114 115 116
//    More specifically these are the CompileString, DebugEvaluate and
//    DebugEvaluateGlobal runtime functions.
// 4. The start position of the calling scope.
117
class CompilationCacheEval : public CompilationSubCache {
118
 public:
119 120
  explicit CompilationCacheEval(Isolate* isolate)
      : CompilationSubCache(isolate, 1) {}
121

122 123 124 125
  InfoCellPair Lookup(Handle<String> source,
                      Handle<SharedFunctionInfo> outer_info,
                      Handle<Context> native_context,
                      LanguageMode language_mode, int position);
126

127
  void Put(Handle<String> source, Handle<SharedFunctionInfo> outer_info,
128
           Handle<SharedFunctionInfo> function_info,
129 130
           Handle<Context> native_context, Handle<FeedbackCell> feedback_cell,
           int position);
131

132 133
  void Age() override;

134 135 136 137 138
 private:
  DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationCacheEval);
};

// Sub-cache for regular expressions.
139
class CompilationCacheRegExp : public CompilationSubCache {
140
 public:
141
  CompilationCacheRegExp(Isolate* isolate, int generations)
142
      : CompilationSubCache(isolate, generations) {}
143

144
  MaybeHandle<FixedArray> Lookup(Handle<String> source, JSRegExp::Flags flags);
145

146
  void Put(Handle<String> source, JSRegExp::Flags flags,
147
           Handle<FixedArray> data);
148

149 150
  void Age() override;

151 152 153 154
 private:
  DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationCacheRegExp);
};

155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
// Sub-cache for Code objects. All code inserted into this cache must
// be usable across different native contexts.
class CompilationCacheCode : public CompilationSubCache {
 public:
  explicit CompilationCacheCode(Isolate* isolate)
      : CompilationSubCache(isolate, kGenerations) {}

  MaybeHandle<Code> Lookup(Handle<SharedFunctionInfo> key);
  void Put(Handle<SharedFunctionInfo> key, Handle<Code> value);

  void Age() override;

  // TODO(jgruber,v8:8888): For simplicity we use the generational
  // approach here, but could consider something else (or more
  // generations) in the future.
  static constexpr int kGenerations = 2;

172 173 174 175 176
  static void TraceAgeing();
  static void TraceInsertion(Handle<SharedFunctionInfo> key,
                             Handle<Code> value);
  static void TraceHit(Handle<SharedFunctionInfo> key, Handle<Code> value);

177 178 179 180
 private:
  DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationCacheCode);
};

181 182 183 184
// The compilation cache keeps shared function infos for compiled
// scripts and evals. The shared function infos are looked up using
// the source string as the key. For regular expressions the
// compilation data is cached.
185
class V8_EXPORT_PRIVATE CompilationCache {
186
 public:
187
  // Finds the script shared function info for a source
188 189
  // string. Returns an empty handle if the cache doesn't contain a
  // script for the given source string with the right origin.
190 191 192
  MaybeHandle<SharedFunctionInfo> LookupScript(
      Handle<String> source, MaybeHandle<Object> name, int line_offset,
      int column_offset, ScriptOriginOptions resource_options,
193
      Handle<Context> native_context, LanguageMode language_mode);
194

195
  // Finds the shared function info for a source string for eval in a
196 197
  // given context.  Returns an empty handle if the cache doesn't
  // contain a script for the given source string.
198 199 200 201
  InfoCellPair LookupEval(Handle<String> source,
                          Handle<SharedFunctionInfo> outer_info,
                          Handle<Context> context, LanguageMode language_mode,
                          int position);
202

203 204
  // Returns the regexp data associated with the given regexp if it
  // is in cache, otherwise an empty handle.
205 206
  MaybeHandle<FixedArray> LookupRegExp(Handle<String> source,
                                       JSRegExp::Flags flags);
207

208 209
  MaybeHandle<Code> LookupCode(Handle<SharedFunctionInfo> sfi);

210 211
  // Associate the (source, kind) pair to the shared function
  // info. This may overwrite an existing mapping.
212
  void PutScript(Handle<String> source, Handle<Context> native_context,
213
                 LanguageMode language_mode,
214
                 Handle<SharedFunctionInfo> function_info);
215 216

  // Associate the (source, context->closure()->shared(), kind) triple
217
  // with the shared function info. This may overwrite an existing mapping.
218
  void PutEval(Handle<String> source, Handle<SharedFunctionInfo> outer_info,
219
               Handle<Context> context,
220 221
               Handle<SharedFunctionInfo> function_info,
               Handle<FeedbackCell> feedback_cell, int position);
222

223 224
  // Associate the (source, flags) pair to the given regexp data.
  // This may overwrite an existing mapping.
225
  void PutRegExp(Handle<String> source, JSRegExp::Flags flags,
226
                 Handle<FixedArray> data);
227

228 229
  void PutCode(Handle<SharedFunctionInfo> shared, Handle<Code> code);

230
  // Clear the cache - also used to initialize the cache at startup.
231
  void Clear();
232

233
  // Remove given shared function info from all caches.
234
  void Remove(Handle<SharedFunctionInfo> function_info);
235

236
  // GC support.
237
  void Iterate(RootVisitor* v);
238 239 240

  // Notify the cache that a mark-sweep garbage collection is about to
  // take place. This is used to retire entries from the cache to
241
  // avoid keeping them alive too long without using them.
242
  void MarkCompactPrologue();
243 244

  // Enable/disable compilation cache. Used by debugger to disable compilation
245 246 247 248 249 250 251 252
  // cache during debugging so that eval and new scripts are always compiled.
  // TODO(bmeurer, chromium:992277): The RegExp cache cannot be enabled and/or
  // disabled, since it doesn't affect debugging. However ideally the other
  // caches should also be always on, even in the presence of the debugger,
  // but at this point there are too many unclear invariants, and so I decided
  // to just fix the pressing performance problem for RegExp individually first.
  void EnableScriptAndEval();
  void DisableScriptAndEval();
253

254
 private:
255
  explicit CompilationCache(Isolate* isolate);
256
  ~CompilationCache() = default;
257

lpy's avatar
lpy committed
258
  base::HashMap* EagerOptimizingSet();
259

260 261 262
  bool IsEnabledScriptAndEval() const {
    return FLAG_compilation_cache && enabled_script_and_eval_;
  }
263

264
  Isolate* isolate() const { return isolate_; }
265 266 267

  Isolate* isolate_;

268 269 270 271
  CompilationCacheScript script_;
  CompilationCacheEval eval_global_;
  CompilationCacheEval eval_contextual_;
  CompilationCacheRegExp reg_exp_;
272 273 274
  CompilationCacheCode code_;

  static constexpr int kSubCacheCount = 5;
275 276
  CompilationSubCache* subcaches_[kSubCacheCount];

277 278
  // Current enable state of the compilation cache for scripts and eval.
  bool enabled_script_and_eval_;
279 280 281 282

  friend class Isolate;

  DISALLOW_COPY_AND_ASSIGN(CompilationCache);
283 284
};

285 286
}  // namespace internal
}  // namespace v8
287

288
#endif  // V8_CODEGEN_COMPILATION_CACHE_H_