v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
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_CODEGEN_COMPILATION_CACHE_H_
6#define V8_CODEGEN_COMPILATION_CACHE_H_
7
8#include "src/base/hashmap.h"
11
12namespace v8 {
13namespace internal {
14
15class RootVisitor;
16struct ScriptDetails;
17
18// The compilation cache consists of several sub-caches: one each for evals and
19// scripts, which use this class as a base class, and a separate generational
20// sub-cache for RegExps. Since the same source code string has different
21// compiled code for scripts and evals, we use separate sub-caches for different
22// compilation modes, to avoid retrieving the wrong result.
24 public:
25 explicit CompilationCacheEvalOrScript(Isolate* isolate) : isolate_(isolate) {}
26
27 // Allocates the table if it didn't yet exist.
29
30 // GC support.
31 void Iterate(RootVisitor* v);
32
33 // Clears this sub-cache evicting all its content.
34 void Clear();
35
36 // Removes given shared function info from sub-cache.
37 void Remove(DirectHandle<SharedFunctionInfo> function_info);
38
39 protected:
40 Isolate* isolate() const { return isolate_; }
41
44
46};
47
48// Sub-cache for scripts.
66
67// Sub-cache for eval scripts. Two caches for eval are used. One for eval calls
68// in native contexts and one for eval calls in other contexts. The cache
69// considers the following pieces of information when checking for matching
70// entries:
71// 1. The source string.
72// 2. The shared function info of the calling function.
73// 3. Whether the source should be compiled as strict code or as sloppy code.
74// Note: Currently there are clients of CompileEval that always compile
75// sloppy code even if the calling function is a strict mode function.
76// More specifically these are the CompileString, DebugEvaluate and
77// DebugEvaluateGlobal runtime functions.
78// 4. The start position of the calling scope.
100
101// Sub-cache for regular expressions.
103 public:
105
107 JSRegExp::Flags flags);
108
109 void Put(DirectHandle<String> source, JSRegExp::Flags flags,
111
112 // The number of generations for the RegExp sub cache.
113 static const int kGenerations = 2;
114
115 // Gets the compilation cache tables for a specific generation. Allocates the
116 // table if it does not yet exist.
118
119 // Ages the sub-cache by evicting the oldest generation and creating a new
120 // young generation.
121 void Age();
122
123 // GC support.
124 void Iterate(RootVisitor* v);
125
126 // Clears this sub-cache evicting all its content.
127 void Clear();
128
129 private:
130 Isolate* isolate() const { return isolate_; }
131
133 Tagged<Object> tables_[kGenerations]; // One for each generation.
134
136};
137
138// The compilation cache keeps shared function infos for compiled
139// scripts and evals. The shared function infos are looked up using
140// the source string as the key. For regular expressions the
141// compilation data is cached.
143 public:
146
147 // Finds the Script and root SharedFunctionInfo for a script source string.
148 // Returns empty handles if the cache doesn't contain a script for the given
149 // source string with the right origin.
151 Handle<String> source, const ScriptDetails& script_details,
152 LanguageMode language_mode);
153
154 // Finds the shared function info for a source string for eval in a
155 // given context. Returns an empty handle if the cache doesn't
156 // contain a script for the given source string.
157 InfoCellPair LookupEval(DirectHandle<String> source,
159 DirectHandle<Context> context,
160 LanguageMode language_mode, int position);
161
162 // Returns the regexp data associated with the given regexp if it
163 // is in cache, otherwise an empty handle.
165 JSRegExp::Flags flags);
166
167 // Associate the (source, kind) pair to the shared function
168 // info. This may overwrite an existing mapping.
169 void PutScript(Handle<String> source, LanguageMode language_mode,
171
172 // Associate the (source, context->closure()->shared(), kind) triple
173 // with the shared function info. This may overwrite an existing mapping.
174 void PutEval(DirectHandle<String> source,
176 DirectHandle<Context> context,
178 DirectHandle<FeedbackCell> feedback_cell, int position);
179
180 // Associate the (source, flags) pair to the given regexp data.
181 // This may overwrite an existing mapping.
182 void PutRegExp(DirectHandle<String> source, JSRegExp::Flags flags,
184
185 // Clear the cache - also used to initialize the cache at startup.
186 void Clear();
187
188 // Remove given shared function info from all caches.
189 void Remove(DirectHandle<SharedFunctionInfo> function_info);
190
191 // GC support.
192 void Iterate(RootVisitor* v);
193
194 // Notify the cache that a mark-sweep garbage collection is about to
195 // take place. This is used to retire entries from the cache to
196 // avoid keeping them alive too long without using them.
197 void MarkCompactPrologue();
198
199 // Enable/disable compilation cache. Used by debugger to disable compilation
200 // cache during debugging so that eval and new scripts are always compiled.
201 // TODO(bmeurer, chromium:992277): The RegExp cache cannot be enabled and/or
202 // disabled, since it doesn't affect debugging. However ideally the other
203 // caches should also be always on, even in the presence of the debugger,
204 // but at this point there are too many unclear invariants, and so I decided
205 // to just fix the pressing performance problem for RegExp individually first.
206 void EnableScriptAndEval();
207 void DisableScriptAndEval();
208
209 private:
210 explicit CompilationCache(Isolate* isolate);
211 ~CompilationCache() = default;
212
214
216 return v8_flags.compilation_cache && enabled_script_and_eval_;
217 }
218 bool IsEnabledScript(LanguageMode language_mode) {
219 // Tests can change v8_flags.use_strict at runtime. The compilation cache
220 // only contains scripts which were compiled with the default language mode.
221 return IsEnabledScriptAndEval() && language_mode == LanguageMode::kSloppy;
222 }
223
224 Isolate* isolate() const { return isolate_; }
225
227
232
233 // Current enable state of the compilation cache for scripts and eval.
235
236 friend class Isolate;
237};
238
239} // namespace internal
240} // namespace v8
241
242#endif // V8_CODEGEN_COMPILATION_CACHE_H_
Isolate * isolate_
Handle< CompilationCacheTable > GetTable()
DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationCacheEvalOrScript)
void Remove(DirectHandle< SharedFunctionInfo > function_info)
void Put(DirectHandle< String > source, DirectHandle< SharedFunctionInfo > outer_info, DirectHandle< SharedFunctionInfo > function_info, DirectHandle< NativeContext > native_context, DirectHandle< FeedbackCell > feedback_cell, int position)
DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationCacheEval)
InfoCellPair Lookup(DirectHandle< String > source, DirectHandle< SharedFunctionInfo > outer_info, DirectHandle< NativeContext > native_context, LanguageMode language_mode, int position)
void Put(DirectHandle< String > source, JSRegExp::Flags flags, DirectHandle< RegExpData > data)
DirectHandle< CompilationCacheTable > GetTable(int generation)
Tagged< Object > tables_[kGenerations]
DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationCacheRegExp)
MaybeDirectHandle< RegExpData > Lookup(DirectHandle< String > source, JSRegExp::Flags flags)
LookupResult Lookup(Handle< String > source, const ScriptDetails &script_details)
DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationCacheScript)
void Put(Handle< String > source, DirectHandle< SharedFunctionInfo > function_info)
bool IsEnabledScript(LanguageMode language_mode)
CompilationCacheRegExp reg_exp_
CompilationCacheEval eval_contextual_
CompilationCache & operator=(const CompilationCache &)=delete
base::HashMap * EagerOptimizingSet()
CompilationCache(const CompilationCache &)=delete
int position
Definition liveedit.cc:290
V8_EXPORT_PRIVATE FlagValues v8_flags
!IsContextMap !IsContextMap native_context
Definition map-inl.h:877
#define V8_EXPORT_PRIVATE
Definition macros.h:460