OLD | NEW |
1 // Copyright 2008 the V8 project authors. All rights reserved. | 1 // Copyright 2008 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 13 matching lines...) Expand all Loading... |
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | 27 |
28 #ifndef V8_COMPILATION_CACHE_H_ | 28 #ifndef V8_COMPILATION_CACHE_H_ |
29 #define V8_COMPILATION_CACHE_H_ | 29 #define V8_COMPILATION_CACHE_H_ |
30 | 30 |
31 namespace v8 { | 31 namespace v8 { |
32 namespace internal { | 32 namespace internal { |
33 | 33 |
| 34 class HashMap; |
| 35 |
| 36 // The compilation cache consists of several generational sub-caches which uses |
| 37 // this class as a base class. A sub-cache contains a compilation cache tables |
| 38 // for each generation of the sub-cache. Since the same source code string has |
| 39 // different compiled code for scripts and evals, we use separate sub-caches |
| 40 // for different compilation modes, to avoid retrieving the wrong result. |
| 41 class CompilationSubCache { |
| 42 public: |
| 43 explicit CompilationSubCache(int generations): generations_(generations) { |
| 44 tables_ = NewArray<Object*>(generations); |
| 45 } |
| 46 |
| 47 ~CompilationSubCache() { DeleteArray(tables_); } |
| 48 |
| 49 // Index for the first generation in the cache. |
| 50 static const int kFirstGeneration = 0; |
| 51 |
| 52 // Get the compilation cache tables for a specific generation. |
| 53 Handle<CompilationCacheTable> GetTable(int generation); |
| 54 |
| 55 // Accessors for first generation. |
| 56 Handle<CompilationCacheTable> GetFirstTable() { |
| 57 return GetTable(kFirstGeneration); |
| 58 } |
| 59 void SetFirstTable(Handle<CompilationCacheTable> value) { |
| 60 ASSERT(kFirstGeneration < generations_); |
| 61 tables_[kFirstGeneration] = *value; |
| 62 } |
| 63 |
| 64 // Age the sub-cache by evicting the oldest generation and creating a new |
| 65 // young generation. |
| 66 void Age(); |
| 67 |
| 68 // GC support. |
| 69 void Iterate(ObjectVisitor* v); |
| 70 void IterateFunctions(ObjectVisitor* v); |
| 71 |
| 72 // Clear this sub-cache evicting all its content. |
| 73 void Clear(); |
| 74 |
| 75 // Remove given shared function info from sub-cache. |
| 76 void Remove(Handle<SharedFunctionInfo> function_info); |
| 77 |
| 78 // Number of generations in this sub-cache. |
| 79 inline int generations() { return generations_; } |
| 80 |
| 81 private: |
| 82 int generations_; // Number of generations. |
| 83 Object** tables_; // Compilation cache tables - one for each generation. |
| 84 |
| 85 DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationSubCache); |
| 86 }; |
| 87 |
| 88 |
| 89 // Sub-cache for scripts. |
| 90 class CompilationCacheScript : public CompilationSubCache { |
| 91 public: |
| 92 explicit CompilationCacheScript(int generations); |
| 93 |
| 94 Handle<SharedFunctionInfo> Lookup(Handle<String> source, |
| 95 Handle<Object> name, |
| 96 int line_offset, |
| 97 int column_offset); |
| 98 void Put(Handle<String> source, Handle<SharedFunctionInfo> function_info); |
| 99 |
| 100 private: |
| 101 MUST_USE_RESULT MaybeObject* TryTablePut( |
| 102 Handle<String> source, Handle<SharedFunctionInfo> function_info); |
| 103 |
| 104 // Note: Returns a new hash table if operation results in expansion. |
| 105 Handle<CompilationCacheTable> TablePut( |
| 106 Handle<String> source, Handle<SharedFunctionInfo> function_info); |
| 107 |
| 108 bool HasOrigin(Handle<SharedFunctionInfo> function_info, |
| 109 Handle<Object> name, |
| 110 int line_offset, |
| 111 int column_offset); |
| 112 |
| 113 void* script_histogram_; |
| 114 bool script_histogram_initialized_; |
| 115 |
| 116 DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationCacheScript); |
| 117 }; |
| 118 |
| 119 |
| 120 // Sub-cache for eval scripts. |
| 121 class CompilationCacheEval: public CompilationSubCache { |
| 122 public: |
| 123 explicit CompilationCacheEval(int generations) |
| 124 : CompilationSubCache(generations) { } |
| 125 |
| 126 Handle<SharedFunctionInfo> Lookup(Handle<String> source, |
| 127 Handle<Context> context, |
| 128 StrictModeFlag strict_mode); |
| 129 |
| 130 void Put(Handle<String> source, |
| 131 Handle<Context> context, |
| 132 Handle<SharedFunctionInfo> function_info); |
| 133 |
| 134 private: |
| 135 MUST_USE_RESULT MaybeObject* TryTablePut( |
| 136 Handle<String> source, |
| 137 Handle<Context> context, |
| 138 Handle<SharedFunctionInfo> function_info); |
| 139 |
| 140 // Note: Returns a new hash table if operation results in expansion. |
| 141 Handle<CompilationCacheTable> TablePut( |
| 142 Handle<String> source, |
| 143 Handle<Context> context, |
| 144 Handle<SharedFunctionInfo> function_info); |
| 145 |
| 146 DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationCacheEval); |
| 147 }; |
| 148 |
| 149 |
| 150 // Sub-cache for regular expressions. |
| 151 class CompilationCacheRegExp: public CompilationSubCache { |
| 152 public: |
| 153 explicit CompilationCacheRegExp(int generations) |
| 154 : CompilationSubCache(generations) { } |
| 155 |
| 156 Handle<FixedArray> Lookup(Handle<String> source, JSRegExp::Flags flags); |
| 157 |
| 158 void Put(Handle<String> source, |
| 159 JSRegExp::Flags flags, |
| 160 Handle<FixedArray> data); |
| 161 private: |
| 162 MUST_USE_RESULT MaybeObject* TryTablePut(Handle<String> source, |
| 163 JSRegExp::Flags flags, |
| 164 Handle<FixedArray> data); |
| 165 |
| 166 // Note: Returns a new hash table if operation results in expansion. |
| 167 Handle<CompilationCacheTable> TablePut(Handle<String> source, |
| 168 JSRegExp::Flags flags, |
| 169 Handle<FixedArray> data); |
| 170 |
| 171 DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationCacheRegExp); |
| 172 }; |
| 173 |
34 | 174 |
35 // The compilation cache keeps shared function infos for compiled | 175 // The compilation cache keeps shared function infos for compiled |
36 // scripts and evals. The shared function infos are looked up using | 176 // scripts and evals. The shared function infos are looked up using |
37 // the source string as the key. For regular expressions the | 177 // the source string as the key. For regular expressions the |
38 // compilation data is cached. | 178 // compilation data is cached. |
39 class CompilationCache { | 179 class CompilationCache { |
40 public: | 180 public: |
41 // Finds the script shared function info for a source | 181 // Finds the script shared function info for a source |
42 // string. Returns an empty handle if the cache doesn't contain a | 182 // string. Returns an empty handle if the cache doesn't contain a |
43 // script for the given source string with the right origin. | 183 // script for the given source string with the right origin. |
44 static Handle<SharedFunctionInfo> LookupScript(Handle<String> source, | 184 Handle<SharedFunctionInfo> LookupScript(Handle<String> source, |
45 Handle<Object> name, | 185 Handle<Object> name, |
46 int line_offset, | 186 int line_offset, |
47 int column_offset); | 187 int column_offset); |
48 | 188 |
49 // Finds the shared function info for a source string for eval in a | 189 // Finds the shared function info for a source string for eval in a |
50 // given context. Returns an empty handle if the cache doesn't | 190 // given context. Returns an empty handle if the cache doesn't |
51 // contain a script for the given source string. | 191 // contain a script for the given source string. |
52 static Handle<SharedFunctionInfo> LookupEval(Handle<String> source, | 192 Handle<SharedFunctionInfo> LookupEval(Handle<String> source, |
53 Handle<Context> context, | 193 Handle<Context> context, |
54 bool is_global, | 194 bool is_global, |
55 StrictModeFlag strict_mode); | 195 StrictModeFlag strict_mode); |
56 | 196 |
57 // Returns the regexp data associated with the given regexp if it | 197 // Returns the regexp data associated with the given regexp if it |
58 // is in cache, otherwise an empty handle. | 198 // is in cache, otherwise an empty handle. |
59 static Handle<FixedArray> LookupRegExp(Handle<String> source, | 199 Handle<FixedArray> LookupRegExp(Handle<String> source, |
60 JSRegExp::Flags flags); | 200 JSRegExp::Flags flags); |
61 | 201 |
62 // Associate the (source, kind) pair to the shared function | 202 // Associate the (source, kind) pair to the shared function |
63 // info. This may overwrite an existing mapping. | 203 // info. This may overwrite an existing mapping. |
64 static void PutScript(Handle<String> source, | 204 void PutScript(Handle<String> source, |
65 Handle<SharedFunctionInfo> function_info); | 205 Handle<SharedFunctionInfo> function_info); |
66 | 206 |
67 // Associate the (source, context->closure()->shared(), kind) triple | 207 // Associate the (source, context->closure()->shared(), kind) triple |
68 // with the shared function info. This may overwrite an existing mapping. | 208 // with the shared function info. This may overwrite an existing mapping. |
69 static void PutEval(Handle<String> source, | 209 void PutEval(Handle<String> source, |
70 Handle<Context> context, | 210 Handle<Context> context, |
71 bool is_global, | 211 bool is_global, |
72 Handle<SharedFunctionInfo> function_info); | 212 Handle<SharedFunctionInfo> function_info); |
73 | 213 |
74 // Associate the (source, flags) pair to the given regexp data. | 214 // Associate the (source, flags) pair to the given regexp data. |
75 // This may overwrite an existing mapping. | 215 // This may overwrite an existing mapping. |
76 static void PutRegExp(Handle<String> source, | 216 void PutRegExp(Handle<String> source, |
77 JSRegExp::Flags flags, | 217 JSRegExp::Flags flags, |
78 Handle<FixedArray> data); | 218 Handle<FixedArray> data); |
79 | 219 |
80 // Support for eager optimization tracking. | 220 // Support for eager optimization tracking. |
81 static bool ShouldOptimizeEagerly(Handle<JSFunction> function); | 221 bool ShouldOptimizeEagerly(Handle<JSFunction> function); |
82 static void MarkForEagerOptimizing(Handle<JSFunction> function); | 222 void MarkForEagerOptimizing(Handle<JSFunction> function); |
83 static void MarkForLazyOptimizing(Handle<JSFunction> function); | 223 void MarkForLazyOptimizing(Handle<JSFunction> function); |
84 | 224 |
85 // Reset the eager optimization tracking data. | 225 // Reset the eager optimization tracking data. |
86 static void ResetEagerOptimizingData(); | 226 void ResetEagerOptimizingData(); |
87 | 227 |
88 // Clear the cache - also used to initialize the cache at startup. | 228 // Clear the cache - also used to initialize the cache at startup. |
89 static void Clear(); | 229 void Clear(); |
90 | 230 |
91 // Remove given shared function info from all caches. | 231 // Remove given shared function info from all caches. |
92 static void Remove(Handle<SharedFunctionInfo> function_info); | 232 void Remove(Handle<SharedFunctionInfo> function_info); |
93 | 233 |
94 // GC support. | 234 // GC support. |
95 static void Iterate(ObjectVisitor* v); | 235 void Iterate(ObjectVisitor* v); |
96 static void IterateFunctions(ObjectVisitor* v); | 236 void IterateFunctions(ObjectVisitor* v); |
97 | 237 |
98 // Notify the cache that a mark-sweep garbage collection is about to | 238 // Notify the cache that a mark-sweep garbage collection is about to |
99 // take place. This is used to retire entries from the cache to | 239 // take place. This is used to retire entries from the cache to |
100 // avoid keeping them alive too long without using them. | 240 // avoid keeping them alive too long without using them. |
101 static void MarkCompactPrologue(); | 241 void MarkCompactPrologue(); |
102 | 242 |
103 // Enable/disable compilation cache. Used by debugger to disable compilation | 243 // Enable/disable compilation cache. Used by debugger to disable compilation |
104 // cache during debugging to make sure new scripts are always compiled. | 244 // cache during debugging to make sure new scripts are always compiled. |
105 static void Enable(); | 245 void Enable(); |
106 static void Disable(); | 246 void Disable(); |
| 247 private: |
| 248 CompilationCache(); |
| 249 ~CompilationCache(); |
| 250 |
| 251 HashMap* EagerOptimizingSet(); |
| 252 |
| 253 // The number of sub caches covering the different types to cache. |
| 254 static const int kSubCacheCount = 4; |
| 255 |
| 256 CompilationCacheScript script_; |
| 257 CompilationCacheEval eval_global_; |
| 258 CompilationCacheEval eval_contextual_; |
| 259 CompilationCacheRegExp reg_exp_; |
| 260 CompilationSubCache* subcaches_[kSubCacheCount]; |
| 261 |
| 262 // Current enable state of the compilation cache. |
| 263 bool enabled_; |
| 264 |
| 265 HashMap* eager_optimizing_set_; |
| 266 |
| 267 bool IsEnabled() { return FLAG_compilation_cache && enabled_; } |
| 268 |
| 269 friend class Isolate; |
| 270 |
| 271 DISALLOW_COPY_AND_ASSIGN(CompilationCache); |
107 }; | 272 }; |
108 | 273 |
109 | 274 |
110 } } // namespace v8::internal | 275 } } // namespace v8::internal |
111 | 276 |
112 #endif // V8_COMPILATION_CACHE_H_ | 277 #endif // V8_COMPILATION_CACHE_H_ |
OLD | NEW |