V8 Project
compilation-cache.h
Go to the documentation of this file.
1 // Copyright 2012 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_COMPILATION_CACHE_H_
6 #define V8_COMPILATION_CACHE_H_
7 
8 namespace v8 {
9 namespace internal {
10 
11 // The compilation cache consists of several generational sub-caches which uses
12 // this class as a base class. A sub-cache contains a compilation cache tables
13 // for each generation of the sub-cache. Since the same source code string has
14 // different compiled code for scripts and evals, we use separate sub-caches
15 // for different compilation modes, to avoid retrieving the wrong result.
17  public:
19  : isolate_(isolate),
21  tables_ = NewArray<Object*>(generations);
22  }
23 
25 
26  // Index for the first generation in the cache.
27  static const int kFirstGeneration = 0;
28 
29  // Get the compilation cache tables for a specific generation.
31 
32  // Accessors for first generation.
34  return GetTable(kFirstGeneration);
35  }
38  tables_[kFirstGeneration] = *value;
39  }
40 
41  // Age the sub-cache by evicting the oldest generation and creating a new
42  // young generation.
43  void Age();
44 
45  // GC support.
46  void Iterate(ObjectVisitor* v);
48 
49  // Clear this sub-cache evicting all its content.
50  void Clear();
51 
52  // Remove given shared function info from sub-cache.
53  void Remove(Handle<SharedFunctionInfo> function_info);
54 
55  // Number of generations in this sub-cache.
56  inline int generations() { return generations_; }
57 
58  protected:
59  Isolate* isolate() { return isolate_; }
60 
61  private:
63  int generations_; // Number of generations.
64  Object** tables_; // Compilation cache tables - one for each generation.
65 
67 };
68 
69 
70 // Sub-cache for scripts.
72  public:
74 
77  int line_offset,
78  int column_offset,
79  bool is_shared_cross_origin,
80  Handle<Context> context);
81  void Put(Handle<String> source,
82  Handle<Context> context,
83  Handle<SharedFunctionInfo> function_info);
84 
85  private:
86  bool HasOrigin(Handle<SharedFunctionInfo> function_info,
88  int line_offset,
89  int column_offset,
90  bool is_shared_cross_origin);
91 
94 
96 };
97 
98 
99 // Sub-cache for eval scripts. Two caches for eval are used. One for eval calls
100 // in native contexts and one for eval calls in other contexts. The cache
101 // considers the following pieces of information when checking for matching
102 // entries:
103 // 1. The source string.
104 // 2. The shared function info of the calling function.
105 // 3. Whether the source should be compiled as strict code or as sloppy code.
106 // Note: Currently there are clients of CompileEval that always compile
107 // sloppy code even if the calling function is a strict mode function.
108 // More specifically these are the CompileString, DebugEvaluate and
109 // DebugEvaluateGlobal runtime functions.
110 // 4. The start position of the calling scope.
112  public:
115 
117  Handle<Context> context,
118  StrictMode strict_mode,
119  int scope_position);
120 
121  void Put(Handle<String> source,
122  Handle<Context> context,
123  Handle<SharedFunctionInfo> function_info,
124  int scope_position);
125 
126  private:
128 };
129 
130 
131 // Sub-cache for regular expressions.
133  public:
136 
138 
139  void Put(Handle<String> source,
141  Handle<FixedArray> data);
142  private:
144 };
145 
146 
147 // The compilation cache keeps shared function infos for compiled
148 // scripts and evals. The shared function infos are looked up using
149 // the source string as the key. For regular expressions the
150 // compilation data is cached.
152  public:
153  // Finds the script shared function info for a source
154  // string. Returns an empty handle if the cache doesn't contain a
155  // script for the given source string with the right origin.
157  Handle<String> source, Handle<Object> name, int line_offset,
158  int column_offset, bool is_shared_cross_origin, Handle<Context> context);
159 
160  // Finds the shared function info for a source string for eval in a
161  // given context. Returns an empty handle if the cache doesn't
162  // contain a script for the given source string.
164  Handle<String> source, Handle<Context> context, StrictMode strict_mode,
165  int scope_position);
166 
167  // Returns the regexp data associated with the given regexp if it
168  // is in cache, otherwise an empty handle.
171 
172  // Associate the (source, kind) pair to the shared function
173  // info. This may overwrite an existing mapping.
174  void PutScript(Handle<String> source,
175  Handle<Context> context,
176  Handle<SharedFunctionInfo> function_info);
177 
178  // Associate the (source, context->closure()->shared(), kind) triple
179  // with the shared function info. This may overwrite an existing mapping.
180  void PutEval(Handle<String> source,
181  Handle<Context> context,
182  Handle<SharedFunctionInfo> function_info,
183  int scope_position);
184 
185  // Associate the (source, flags) pair to the given regexp data.
186  // This may overwrite an existing mapping.
187  void PutRegExp(Handle<String> source,
189  Handle<FixedArray> data);
190 
191  // Clear the cache - also used to initialize the cache at startup.
192  void Clear();
193 
194  // Remove given shared function info from all caches.
195  void Remove(Handle<SharedFunctionInfo> function_info);
196 
197  // GC support.
198  void Iterate(ObjectVisitor* v);
200 
201  // Notify the cache that a mark-sweep garbage collection is about to
202  // take place. This is used to retire entries from the cache to
203  // avoid keeping them alive too long without using them.
204  void MarkCompactPrologue();
205 
206  // Enable/disable compilation cache. Used by debugger to disable compilation
207  // cache during debugging to make sure new scripts are always compiled.
208  void Enable();
209  void Disable();
210 
211  private:
212  explicit CompilationCache(Isolate* isolate);
214 
216 
217  // The number of sub caches covering the different types to cache.
218  static const int kSubCacheCount = 4;
219 
220  bool IsEnabled() { return FLAG_compilation_cache && enabled_; }
221 
222  Isolate* isolate() { return isolate_; }
223 
225 
231 
232  // Current enable state of the compilation cache.
233  bool enabled_;
234 
235  friend class Isolate;
236 
238 };
239 
240 
241 } } // namespace v8::internal
242 
243 #endif // V8_COMPILATION_CACHE_H_
DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationCacheEval)
void Put(Handle< String > source, Handle< Context > context, Handle< SharedFunctionInfo > function_info, int scope_position)
CompilationCacheEval(Isolate *isolate, int generations)
MaybeHandle< SharedFunctionInfo > Lookup(Handle< String > source, Handle< Context > context, StrictMode strict_mode, int scope_position)
MaybeHandle< FixedArray > Lookup(Handle< String > source, JSRegExp::Flags flags)
void Put(Handle< String > source, JSRegExp::Flags flags, Handle< FixedArray > data)
DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationCacheRegExp)
CompilationCacheRegExp(Isolate *isolate, int generations)
DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationCacheScript)
Handle< SharedFunctionInfo > Lookup(Handle< String > source, Handle< Object > name, int line_offset, int column_offset, bool is_shared_cross_origin, Handle< Context > context)
void Put(Handle< String > source, Handle< Context > context, Handle< SharedFunctionInfo > function_info)
CompilationCacheScript(Isolate *isolate, int generations)
bool HasOrigin(Handle< SharedFunctionInfo > function_info, Handle< Object > name, int line_offset, int column_offset, bool is_shared_cross_origin)
void Iterate(ObjectVisitor *v)
CompilationCacheRegExp reg_exp_
void IterateFunctions(ObjectVisitor *v)
MaybeHandle< FixedArray > LookupRegExp(Handle< String > source, JSRegExp::Flags flags)
void PutEval(Handle< String > source, Handle< Context > context, Handle< SharedFunctionInfo > function_info, int scope_position)
MaybeHandle< SharedFunctionInfo > LookupScript(Handle< String > source, Handle< Object > name, int line_offset, int column_offset, bool is_shared_cross_origin, Handle< Context > context)
CompilationCacheEval eval_contextual_
MaybeHandle< SharedFunctionInfo > LookupEval(Handle< String > source, Handle< Context > context, StrictMode strict_mode, int scope_position)
void PutScript(Handle< String > source, Handle< Context > context, Handle< SharedFunctionInfo > function_info)
CompilationCacheScript script_
void Remove(Handle< SharedFunctionInfo > function_info)
void PutRegExp(Handle< String > source, JSRegExp::Flags flags, Handle< FixedArray > data)
CompilationSubCache * subcaches_[kSubCacheCount]
DISALLOW_COPY_AND_ASSIGN(CompilationCache)
CompilationCacheEval eval_global_
Handle< CompilationCacheTable > GetTable(int generation)
CompilationSubCache(Isolate *isolate, int generations)
void Remove(Handle< SharedFunctionInfo > function_info)
DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationSubCache)
void IterateFunctions(ObjectVisitor *v)
Handle< CompilationCacheTable > GetFirstTable()
void SetFirstTable(Handle< CompilationCacheTable > value)
enable harmony numeric enable harmony object literal extensions Optimize object Array DOM strings and string trace pretenuring decisions of HAllocate instructions Enables optimizations which favor memory size over execution speed maximum source size in bytes considered for a single inlining maximum cumulative number of AST nodes considered for inlining trace the tracking of allocation sites deoptimize every n garbage collections perform array bounds checks elimination analyze liveness of environment slots and zap dead values flushes the cache of optimized code for closures on every GC allow uint32 values on optimize frames if they are used only in safe operations track concurrent recompilation artificial compilation delay in ms do not emit check maps for constant values that have a leaf deoptimize the optimized code if the layout of the maps changes enable context specialization in TurboFan execution budget before interrupt is triggered max percentage of megamorphic generic ICs to allow optimization enable use of SAHF instruction if enable use of VFP3 instructions if available enable use of NEON instructions if enable use of SDIV and UDIV instructions if enable use of MLS instructions if enable loading bit constant by means of movw movt instruction enable unaligned accesses for enable use of d16 d31 registers on ARM this requires VFP3 force all emitted branches to be in long enable alignment of csp to bytes on platforms which prefer the register to always be expose gc extension under the specified name show built in functions in stack traces use random jit cookie to mask large constants minimum length for automatic enable preparsing CPU profiler sampling interval in microseconds trace out of bounds accesses to external arrays default size of stack region v8 is allowed to maximum length of function source code printed in a stack trace min size of a semi the new space consists of two semi spaces print one trace line following each garbage collection do not print trace line after scavenger collection print cumulative GC statistics in name
#define DCHECK(condition)
Definition: logging.h:205
void DeleteArray(T *array)
Definition: allocation.h:68
Debugger support for the V8 JavaScript engine.
Definition: accessors.cc:20