compilation-cache.h 8.65 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 7

#ifndef V8_COMPILATION_CACHE_H_
#define V8_COMPILATION_CACHE_H_

8
#include "src/allocation.h"
9
#include "src/objects/compilation-cache.h"
10

11 12
namespace v8 {
namespace internal {
13

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

17 18
class RootVisitor;

19 20 21 22 23 24 25
// 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:
26 27 28
  CompilationSubCache(Isolate* isolate, int generations)
      : isolate_(isolate),
        generations_(generations) {
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
    tables_ = NewArray<Object*>(generations);
  }

  ~CompilationSubCache() { DeleteArray(tables_); }

  // Index for the first generation in the cache.
  static const int kFirstGeneration = 0;

  // 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) {
45
    DCHECK_LT(kFirstGeneration, generations_);
46 47 48 49 50 51 52 53
    tables_[kFirstGeneration] = *value;
  }

  // Age the sub-cache by evicting the oldest generation and creating a new
  // young generation.
  void Age();

  // GC support.
54
  void Iterate(RootVisitor* v);
55 56 57 58 59 60 61 62 63 64

  // 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.
  inline int generations() { return generations_; }

65 66 67
 protected:
  Isolate* isolate() { return isolate_; }

68
 private:
69
  Isolate* isolate_;
70 71 72 73 74 75 76 77 78 79
  int generations_;  // Number of generations.
  Object** tables_;  // Compilation cache tables - one for each generation.

  DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationSubCache);
};


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

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

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

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

  DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationCacheScript);
};


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

119 120 121 122
  InfoCellPair Lookup(Handle<String> source,
                      Handle<SharedFunctionInfo> outer_info,
                      Handle<Context> native_context,
                      LanguageMode language_mode, int position);
123

124
  void Put(Handle<String> source, Handle<SharedFunctionInfo> outer_info,
125
           Handle<SharedFunctionInfo> function_info,
126 127
           Handle<Context> native_context, Handle<FeedbackCell> feedback_cell,
           int position);
128 129 130 131 132 133 134 135 136

 private:
  DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationCacheEval);
};


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

140
  MaybeHandle<FixedArray> Lookup(Handle<String> source, JSRegExp::Flags flags);
141 142 143 144 145 146 147 148

  void Put(Handle<String> source,
           JSRegExp::Flags flags,
           Handle<FixedArray> data);
 private:
  DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationCacheRegExp);
};

149 150 151 152
// 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.
153 154
class CompilationCache {
 public:
155
  // Finds the script shared function info for a source
156 157
  // string. Returns an empty handle if the cache doesn't contain a
  // script for the given source string with the right origin.
158 159 160
  MaybeHandle<SharedFunctionInfo> LookupScript(
      Handle<String> source, MaybeHandle<Object> name, int line_offset,
      int column_offset, ScriptOriginOptions resource_options,
161
      Handle<Context> native_context, LanguageMode language_mode);
162

163
  // Finds the shared function info for a source string for eval in a
164 165
  // given context.  Returns an empty handle if the cache doesn't
  // contain a script for the given source string.
166 167 168 169
  InfoCellPair LookupEval(Handle<String> source,
                          Handle<SharedFunctionInfo> outer_info,
                          Handle<Context> context, LanguageMode language_mode,
                          int position);
170

171 172
  // Returns the regexp data associated with the given regexp if it
  // is in cache, otherwise an empty handle.
173 174
  MaybeHandle<FixedArray> LookupRegExp(
      Handle<String> source, JSRegExp::Flags flags);
175

176 177
  // Associate the (source, kind) pair to the shared function
  // info. This may overwrite an existing mapping.
178
  void PutScript(Handle<String> source, Handle<Context> native_context,
179
                 LanguageMode language_mode,
180
                 Handle<SharedFunctionInfo> function_info);
181 182

  // Associate the (source, context->closure()->shared(), kind) triple
183
  // with the shared function info. This may overwrite an existing mapping.
184
  void PutEval(Handle<String> source, Handle<SharedFunctionInfo> outer_info,
185
               Handle<Context> context,
186 187
               Handle<SharedFunctionInfo> function_info,
               Handle<FeedbackCell> feedback_cell, int position);
188

189 190
  // Associate the (source, flags) pair to the given regexp data.
  // This may overwrite an existing mapping.
191 192 193
  void PutRegExp(Handle<String> source,
                 JSRegExp::Flags flags,
                 Handle<FixedArray> data);
194

195
  // Clear the cache - also used to initialize the cache at startup.
196
  void Clear();
197

198
  // Remove given shared function info from all caches.
199
  void Remove(Handle<SharedFunctionInfo> function_info);
200

201
  // GC support.
202
  void Iterate(RootVisitor* v);
203 204 205

  // Notify the cache that a mark-sweep garbage collection is about to
  // take place. This is used to retire entries from the cache to
206
  // avoid keeping them alive too long without using them.
207
  void MarkCompactPrologue();
208 209 210

  // Enable/disable compilation cache. Used by debugger to disable compilation
  // cache during debugging to make sure new scripts are always compiled.
211 212
  void Enable();
  void Disable();
213

214
 private:
215
  explicit CompilationCache(Isolate* isolate);
216
  ~CompilationCache() = default;
217

lpy's avatar
lpy committed
218
  base::HashMap* EagerOptimizingSet();
219 220 221 222

  // The number of sub caches covering the different types to cache.
  static const int kSubCacheCount = 4;

223
  bool IsEnabled() const { return FLAG_compilation_cache && enabled_; }
224

225
  Isolate* isolate() const { return isolate_; }
226 227 228

  Isolate* isolate_;

229 230 231 232 233 234 235 236 237 238 239 240
  CompilationCacheScript script_;
  CompilationCacheEval eval_global_;
  CompilationCacheEval eval_contextual_;
  CompilationCacheRegExp reg_exp_;
  CompilationSubCache* subcaches_[kSubCacheCount];

  // Current enable state of the compilation cache.
  bool enabled_;

  friend class Isolate;

  DISALLOW_COPY_AND_ASSIGN(CompilationCache);
241 242 243
};


244 245
}  // namespace internal
}  // namespace v8
246 247

#endif  // V8_COMPILATION_CACHE_H_