compilation-cache.h 11.2 KB
Newer Older
1
// Copyright 2012 the V8 project authors. All rights reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
//       copyright notice, this list of conditions and the following
//       disclaimer in the documentation and/or other materials provided
//       with the distribution.
//     * Neither the name of Google Inc. nor the names of its
//       contributors may be used to endorse or promote products derived
//       from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#ifndef V8_COMPILATION_CACHE_H_
#define V8_COMPILATION_CACHE_H_

31 32
namespace v8 {
namespace internal {
33

34 35 36 37 38 39 40
// 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:
41 42 43
  CompilationSubCache(Isolate* isolate, int generations)
      : isolate_(isolate),
        generations_(generations) {
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
    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) {
    ASSERT(kFirstGeneration < generations_);
    tables_[kFirstGeneration] = *value;
  }

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

  // GC support.
  void Iterate(ObjectVisitor* v);
  void IterateFunctions(ObjectVisitor* v);

  // 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_; }

81 82 83
 protected:
  Isolate* isolate() { return isolate_; }

84
 private:
85
  Isolate* isolate_;
86 87 88 89 90 91 92 93 94 95
  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:
96
  CompilationCacheScript(Isolate* isolate, int generations);
97 98 99 100

  Handle<SharedFunctionInfo> Lookup(Handle<String> source,
                                    Handle<Object> name,
                                    int line_offset,
101
                                    int column_offset,
102
                                    bool is_shared_cross_origin,
103 104 105 106
                                    Handle<Context> context);
  void Put(Handle<String> source,
           Handle<Context> context,
           Handle<SharedFunctionInfo> function_info);
107 108 109

 private:
  MUST_USE_RESULT MaybeObject* TryTablePut(
110 111 112
      Handle<String> source,
      Handle<Context> context,
      Handle<SharedFunctionInfo> function_info);
113 114 115

  // Note: Returns a new hash table if operation results in expansion.
  Handle<CompilationCacheTable> TablePut(
116 117 118
      Handle<String> source,
      Handle<Context> context,
      Handle<SharedFunctionInfo> function_info);
119 120 121 122

  bool HasOrigin(Handle<SharedFunctionInfo> function_info,
                 Handle<Object> name,
                 int line_offset,
123 124
                 int column_offset,
                 bool is_shared_cross_origin);
125 126 127 128 129 130 131 132

  void* script_histogram_;
  bool script_histogram_initialized_;

  DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationCacheScript);
};


133
// Sub-cache for eval scripts. Two caches for eval are used. One for eval calls
134
// in native contexts and one for eval calls in other contexts. The cache
135 136 137 138 139 140 141 142 143 144 145
// considers the following pieces of information when checking for matching
// entries:
// 1. The source string.
// 2. The shared function info of the calling function.
// 3. Whether the source should be compiled as strict code or as non-strict
//    code.
//    Note: Currently there are clients of CompileEval that always compile
//    non-strict code even if the calling function is a strict mode function.
//    More specifically these are the CompileString, DebugEvaluate and
//    DebugEvaluateGlobal runtime functions.
// 4. The start position of the calling scope.
146 147
class CompilationCacheEval: public CompilationSubCache {
 public:
148 149
  CompilationCacheEval(Isolate* isolate, int generations)
      : CompilationSubCache(isolate, generations) { }
150 151 152

  Handle<SharedFunctionInfo> Lookup(Handle<String> source,
                                    Handle<Context> context,
153
                                    LanguageMode language_mode,
154
                                    int scope_position);
155 156 157

  void Put(Handle<String> source,
           Handle<Context> context,
158 159
           Handle<SharedFunctionInfo> function_info,
           int scope_position);
160 161 162 163 164

 private:
  MUST_USE_RESULT MaybeObject* TryTablePut(
      Handle<String> source,
      Handle<Context> context,
165 166
      Handle<SharedFunctionInfo> function_info,
      int scope_position);
167 168 169 170 171

  // Note: Returns a new hash table if operation results in expansion.
  Handle<CompilationCacheTable> TablePut(
      Handle<String> source,
      Handle<Context> context,
172 173
      Handle<SharedFunctionInfo> function_info,
      int scope_position);
174 175 176 177 178 179 180 181

  DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationCacheEval);
};


// Sub-cache for regular expressions.
class CompilationCacheRegExp: public CompilationSubCache {
 public:
182 183
  CompilationCacheRegExp(Isolate* isolate, int generations)
      : CompilationSubCache(isolate, generations) { }
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202

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

  void Put(Handle<String> source,
           JSRegExp::Flags flags,
           Handle<FixedArray> data);
 private:
  MUST_USE_RESULT MaybeObject* TryTablePut(Handle<String> source,
                                      JSRegExp::Flags flags,
                                      Handle<FixedArray> data);

  // Note: Returns a new hash table if operation results in expansion.
  Handle<CompilationCacheTable> TablePut(Handle<String> source,
                                         JSRegExp::Flags flags,
                                         Handle<FixedArray> data);

  DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationCacheRegExp);
};

203

204 205 206 207
// 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.
208 209
class CompilationCache {
 public:
210
  // Finds the script shared function info for a source
211 212
  // string. Returns an empty handle if the cache doesn't contain a
  // script for the given source string with the right origin.
213 214 215
  Handle<SharedFunctionInfo> LookupScript(Handle<String> source,
                                          Handle<Object> name,
                                          int line_offset,
216
                                          int column_offset,
217
                                          bool is_shared_cross_origin,
218
                                          Handle<Context> context);
219

220
  // Finds the shared function info for a source string for eval in a
221 222
  // given context.  Returns an empty handle if the cache doesn't
  // contain a script for the given source string.
223 224
  Handle<SharedFunctionInfo> LookupEval(Handle<String> source,
                                        Handle<Context> context,
225
                                        LanguageMode language_mode,
226
                                        int scope_position);
227

228 229
  // Returns the regexp data associated with the given regexp if it
  // is in cache, otherwise an empty handle.
230 231
  Handle<FixedArray> LookupRegExp(Handle<String> source,
                                  JSRegExp::Flags flags);
232

233 234
  // Associate the (source, kind) pair to the shared function
  // info. This may overwrite an existing mapping.
235
  void PutScript(Handle<String> source,
236
                 Handle<Context> context,
237
                 Handle<SharedFunctionInfo> function_info);
238 239

  // Associate the (source, context->closure()->shared(), kind) triple
240
  // with the shared function info. This may overwrite an existing mapping.
241 242
  void PutEval(Handle<String> source,
               Handle<Context> context,
243 244
               Handle<SharedFunctionInfo> function_info,
               int scope_position);
245

246 247
  // Associate the (source, flags) pair to the given regexp data.
  // This may overwrite an existing mapping.
248 249 250
  void PutRegExp(Handle<String> source,
                 JSRegExp::Flags flags,
                 Handle<FixedArray> data);
251

252
  // Clear the cache - also used to initialize the cache at startup.
253
  void Clear();
254

255
  // Remove given shared function info from all caches.
256
  void Remove(Handle<SharedFunctionInfo> function_info);
257

258
  // GC support.
259 260
  void Iterate(ObjectVisitor* v);
  void IterateFunctions(ObjectVisitor* v);
261 262 263

  // Notify the cache that a mark-sweep garbage collection is about to
  // take place. This is used to retire entries from the cache to
264
  // avoid keeping them alive too long without using them.
265
  void MarkCompactPrologue();
266 267 268

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

272
 private:
273
  explicit CompilationCache(Isolate* isolate);
274 275 276 277 278 279 280
  ~CompilationCache();

  HashMap* EagerOptimizingSet();

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

281 282 283 284 285 286
  bool IsEnabled() { return FLAG_compilation_cache && enabled_; }

  Isolate* isolate() { return isolate_; }

  Isolate* isolate_;

287 288 289 290 291 292 293 294 295 296 297 298
  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);
299 300 301 302 303 304
};


} }  // namespace v8::internal

#endif  // V8_COMPILATION_CACHE_H_