| 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 14 matching lines...) Expand all Loading... |
| 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 | 34 |
| 35 // The compilation cache consists of several generational sub-caches which uses |
| 36 // this class as a base class. A sub-cache contains a compilation cache tables |
| 37 // for each generation of the sub-cache. Since the same source code string has |
| 38 // different compiled code for scripts and evals, we use separate sub-caches |
| 39 // for different compilation modes, to avoid retrieving the wrong result. |
| 40 class CompilationSubCache { |
| 41 public: |
| 42 explicit CompilationSubCache(int generations): generations_(generations) { |
| 43 tables_ = NewArray<Object*>(generations); |
| 44 } |
| 45 |
| 46 ~CompilationSubCache() { DeleteArray(tables_); } |
| 47 |
| 48 // Index for the first generation in the cache. |
| 49 static const int kFirstGeneration = 0; |
| 50 |
| 51 // Get the compilation cache tables for a specific generation. |
| 52 Handle<CompilationCacheTable> GetTable(int generation); |
| 53 |
| 54 // Accessors for first generation. |
| 55 Handle<CompilationCacheTable> GetFirstTable() { |
| 56 return GetTable(kFirstGeneration); |
| 57 } |
| 58 void SetFirstTable(Handle<CompilationCacheTable> value) { |
| 59 ASSERT(kFirstGeneration < generations_); |
| 60 tables_[kFirstGeneration] = *value; |
| 61 } |
| 62 |
| 63 // Age the sub-cache by evicting the oldest generation and creating a new |
| 64 // young generation. |
| 65 void Age(); |
| 66 |
| 67 bool HasFunction(SharedFunctionInfo* function_info); |
| 68 |
| 69 // GC support. |
| 70 void Iterate(ObjectVisitor* v); |
| 71 |
| 72 // Clear this sub-cache evicting all its content. |
| 73 void Clear(); |
| 74 |
| 75 // Number of generations in this sub-cache. |
| 76 inline int generations() { return generations_; } |
| 77 |
| 78 private: |
| 79 int generations_; // Number of generations. |
| 80 Object** tables_; // Compilation cache tables - one for each generation. |
| 81 |
| 82 DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationSubCache); |
| 83 }; |
| 84 |
| 85 |
| 86 // Sub-cache for scripts. |
| 87 class CompilationCacheScript : public CompilationSubCache { |
| 88 public: |
| 89 explicit CompilationCacheScript(int generations) |
| 90 : CompilationSubCache(generations) { } |
| 91 |
| 92 Handle<SharedFunctionInfo> Lookup(Handle<String> source, |
| 93 Handle<Object> name, |
| 94 int line_offset, |
| 95 int column_offset); |
| 96 void Put(Handle<String> source, Handle<SharedFunctionInfo> function_info); |
| 97 |
| 98 private: |
| 99 // Note: Returns a new hash table if operation results in expansion. |
| 100 Handle<CompilationCacheTable> TablePut( |
| 101 Handle<String> source, Handle<SharedFunctionInfo> function_info); |
| 102 |
| 103 bool HasOrigin(Handle<SharedFunctionInfo> function_info, |
| 104 Handle<Object> name, |
| 105 int line_offset, |
| 106 int column_offset); |
| 107 |
| 108 DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationCacheScript); |
| 109 }; |
| 110 |
| 111 |
| 112 // Sub-cache for eval scripts. |
| 113 class CompilationCacheEval: public CompilationSubCache { |
| 114 public: |
| 115 explicit CompilationCacheEval(int generations) |
| 116 : CompilationSubCache(generations) { } |
| 117 |
| 118 Handle<SharedFunctionInfo> Lookup(Handle<String> source, |
| 119 Handle<Context> context); |
| 120 |
| 121 void Put(Handle<String> source, |
| 122 Handle<Context> context, |
| 123 Handle<SharedFunctionInfo> function_info); |
| 124 |
| 125 private: |
| 126 // Note: Returns a new hash table if operation results in expansion. |
| 127 Handle<CompilationCacheTable> TablePut( |
| 128 Handle<String> source, |
| 129 Handle<Context> context, |
| 130 Handle<SharedFunctionInfo> function_info); |
| 131 |
| 132 DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationCacheEval); |
| 133 }; |
| 134 |
| 135 |
| 136 // Sub-cache for regular expressions. |
| 137 class CompilationCacheRegExp: public CompilationSubCache { |
| 138 public: |
| 139 explicit CompilationCacheRegExp(int generations) |
| 140 : CompilationSubCache(generations) { } |
| 141 |
| 142 Handle<FixedArray> Lookup(Handle<String> source, JSRegExp::Flags flags); |
| 143 |
| 144 void Put(Handle<String> source, |
| 145 JSRegExp::Flags flags, |
| 146 Handle<FixedArray> data); |
| 147 private: |
| 148 // Note: Returns a new hash table if operation results in expansion. |
| 149 Handle<CompilationCacheTable> TablePut(Handle<String> source, |
| 150 JSRegExp::Flags flags, |
| 151 Handle<FixedArray> data); |
| 152 |
| 153 DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationCacheRegExp); |
| 154 }; |
| 155 |
| 156 |
| 35 // The compilation cache keeps shared function infos for compiled | 157 // The compilation cache keeps shared function infos for compiled |
| 36 // scripts and evals. The shared function infos are looked up using | 158 // scripts and evals. The shared function infos are looked up using |
| 37 // the source string as the key. For regular expressions the | 159 // the source string as the key. For regular expressions the |
| 38 // compilation data is cached. | 160 // compilation data is cached. |
| 39 class CompilationCache { | 161 class CompilationCache { |
| 40 public: | 162 public: |
| 41 // Finds the script shared function info for a source | 163 // Finds the script shared function info for a source |
| 42 // string. Returns an empty handle if the cache doesn't contain a | 164 // string. Returns an empty handle if the cache doesn't contain a |
| 43 // script for the given source string with the right origin. | 165 // script for the given source string with the right origin. |
| 44 static Handle<SharedFunctionInfo> LookupScript(Handle<String> source, | 166 Handle<SharedFunctionInfo> LookupScript(Handle<String> source, |
| 45 Handle<Object> name, | 167 Handle<Object> name, |
| 46 int line_offset, | 168 int line_offset, |
| 47 int column_offset); | 169 int column_offset); |
| 48 | 170 |
| 49 // Finds the shared function info for a source string for eval in a | 171 // 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 | 172 // given context. Returns an empty handle if the cache doesn't |
| 51 // contain a script for the given source string. | 173 // contain a script for the given source string. |
| 52 static Handle<SharedFunctionInfo> LookupEval(Handle<String> source, | 174 Handle<SharedFunctionInfo> LookupEval(Handle<String> source, |
| 53 Handle<Context> context, | 175 Handle<Context> context, |
| 54 bool is_global); | 176 bool is_global); |
| 55 | 177 |
| 56 // Returns the regexp data associated with the given regexp if it | 178 // Returns the regexp data associated with the given regexp if it |
| 57 // is in cache, otherwise an empty handle. | 179 // is in cache, otherwise an empty handle. |
| 58 static Handle<FixedArray> LookupRegExp(Handle<String> source, | 180 Handle<FixedArray> LookupRegExp(Handle<String> source, |
| 59 JSRegExp::Flags flags); | 181 JSRegExp::Flags flags); |
| 60 | 182 |
| 61 // Associate the (source, kind) pair to the shared function | 183 // Associate the (source, kind) pair to the shared function |
| 62 // info. This may overwrite an existing mapping. | 184 // info. This may overwrite an existing mapping. |
| 63 static void PutScript(Handle<String> source, | 185 void PutScript(Handle<String> source, |
| 64 Handle<SharedFunctionInfo> function_info); | 186 Handle<SharedFunctionInfo> function_info); |
| 65 | 187 |
| 66 // Associate the (source, context->closure()->shared(), kind) triple | 188 // Associate the (source, context->closure()->shared(), kind) triple |
| 67 // with the shared function info. This may overwrite an existing mapping. | 189 // with the shared function info. This may overwrite an existing mapping. |
| 68 static void PutEval(Handle<String> source, | 190 void PutEval(Handle<String> source, |
| 69 Handle<Context> context, | 191 Handle<Context> context, |
| 70 bool is_global, | 192 bool is_global, |
| 71 Handle<SharedFunctionInfo> function_info); | 193 Handle<SharedFunctionInfo> function_info); |
| 72 | 194 |
| 73 // Associate the (source, flags) pair to the given regexp data. | 195 // Associate the (source, flags) pair to the given regexp data. |
| 74 // This may overwrite an existing mapping. | 196 // This may overwrite an existing mapping. |
| 75 static void PutRegExp(Handle<String> source, | 197 void PutRegExp(Handle<String> source, |
| 76 JSRegExp::Flags flags, | 198 JSRegExp::Flags flags, |
| 77 Handle<FixedArray> data); | 199 Handle<FixedArray> data); |
| 78 | 200 |
| 79 // Clear the cache - also used to initialize the cache at startup. | 201 // Clear the cache - also used to initialize the cache at startup. |
| 80 static void Clear(); | 202 void Clear(); |
| 81 | 203 |
| 82 | 204 |
| 83 static bool HasFunction(SharedFunctionInfo* function_info); | 205 bool HasFunction(SharedFunctionInfo* function_info); |
| 84 | 206 |
| 85 // GC support. | 207 // GC support. |
| 86 static void Iterate(ObjectVisitor* v); | 208 void Iterate(ObjectVisitor* v); |
| 87 | 209 |
| 88 // Notify the cache that a mark-sweep garbage collection is about to | 210 // Notify the cache that a mark-sweep garbage collection is about to |
| 89 // take place. This is used to retire entries from the cache to | 211 // take place. This is used to retire entries from the cache to |
| 90 // avoid keeping them alive too long without using them. | 212 // avoid keeping them alive too long without using them. |
| 91 static void MarkCompactPrologue(); | 213 void MarkCompactPrologue(); |
| 92 | 214 |
| 93 // Enable/disable compilation cache. Used by debugger to disable compilation | 215 // Enable/disable compilation cache. Used by debugger to disable compilation |
| 94 // cache during debugging to make sure new scripts are always compiled. | 216 // cache during debugging to make sure new scripts are always compiled. |
| 95 static void Enable(); | 217 void Enable(); |
| 96 static void Disable(); | 218 void Disable(); |
| 219 private: |
| 220 CompilationCache(); |
| 221 |
| 222 // The number of sub caches covering the different types to cache. |
| 223 static const int kSubCacheCount = 4; |
| 224 |
| 225 CompilationCacheScript script_; |
| 226 CompilationCacheEval eval_global_; |
| 227 CompilationCacheEval eval_contextual_; |
| 228 CompilationCacheRegExp reg_exp_; |
| 229 CompilationSubCache* subcaches_[kSubCacheCount]; |
| 230 |
| 231 // Current enable state of the compilation cache. |
| 232 bool enabled_; |
| 233 |
| 234 bool IsEnabled() { return FLAG_compilation_cache && enabled_; } |
| 235 |
| 236 friend class Isolate; |
| 237 |
| 238 DISALLOW_COPY_AND_ASSIGN(CompilationCache); |
| 97 }; | 239 }; |
| 98 | 240 |
| 99 | 241 |
| 100 } } // namespace v8::internal | 242 } } // namespace v8::internal |
| 101 | 243 |
| 102 #endif // V8_COMPILATION_CACHE_H_ | 244 #endif // V8_COMPILATION_CACHE_H_ |
| OLD | NEW |