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 16 matching lines...) Expand all Loading... |
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 keeps function boilerplates for compiled | 35 // The compilation cache keeps function boilerplates for compiled |
36 // scripts and evals. The boilerplates are looked up using the source | 36 // scripts and evals. The boilerplates are looked up using the source |
37 // string as the key. | 37 // string as the key. For regular expressions the compilation data is cached. |
38 class CompilationCache { | 38 class CompilationCache { |
39 public: | 39 public: |
40 // The same source code string has different compiled code for | |
41 // scripts and evals. Internally, we use separate caches to avoid | |
42 // getting the wrong kind of entry when looking up. | |
43 enum Entry { | |
44 EVAL_GLOBAL, | |
45 EVAL_CONTEXTUAL, | |
46 REGEXP, | |
47 SCRIPT, | |
48 LAST_ENTRY = SCRIPT | |
49 }; | |
50 | |
51 // Finds the script function boilerplate for a source | 40 // Finds the script function boilerplate for a source |
52 // string. Returns an empty handle if the cache doesn't contain a | 41 // string. Returns an empty handle if the cache doesn't contain a |
53 // script for the given source string with the right origin. | 42 // script for the given source string with the right origin. |
54 static Handle<JSFunction> LookupScript(Handle<String> source, | 43 static Handle<JSFunction> LookupScript(Handle<String> source, |
55 Handle<Object> name, | 44 Handle<Object> name, |
56 int line_offset, | 45 int line_offset, |
57 int column_offset); | 46 int column_offset); |
58 | 47 |
59 // Finds the function boilerplate for a source string for eval in a | 48 // Finds the function boilerplate for a source string for eval in a |
60 // given context. Returns an empty handle if the cache doesn't | 49 // given context. Returns an empty handle if the cache doesn't |
61 // contain a script for the given source string. | 50 // contain a script for the given source string. |
62 static Handle<JSFunction> LookupEval(Handle<String> source, | 51 static Handle<JSFunction> LookupEval(Handle<String> source, |
63 Handle<Context> context, | 52 Handle<Context> context, |
64 Entry entry); | 53 bool is_global); |
65 | 54 |
66 // Returns the regexp data associated with the given regexp if it | 55 // Returns the regexp data associated with the given regexp if it |
67 // is in cache, otherwise an empty handle. | 56 // is in cache, otherwise an empty handle. |
68 static Handle<FixedArray> LookupRegExp(Handle<String> source, | 57 static Handle<FixedArray> LookupRegExp(Handle<String> source, |
69 JSRegExp::Flags flags); | 58 JSRegExp::Flags flags); |
70 | 59 |
71 // Associate the (source, kind) pair to the boilerplate. This may | 60 // Associate the (source, kind) pair to the boilerplate. This may |
72 // overwrite an existing mapping. | 61 // overwrite an existing mapping. |
73 static void PutScript(Handle<String> source, | 62 static void PutScript(Handle<String> source, |
74 Handle<JSFunction> boilerplate); | 63 Handle<JSFunction> boilerplate); |
75 | 64 |
76 // Associate the (source, context->closure()->shared(), kind) triple | 65 // Associate the (source, context->closure()->shared(), kind) triple |
77 // with the boilerplate. This may overwrite an existing mapping. | 66 // with the boilerplate. This may overwrite an existing mapping. |
78 static void PutEval(Handle<String> source, | 67 static void PutEval(Handle<String> source, |
79 Handle<Context> context, | 68 Handle<Context> context, |
80 Entry entry, | 69 bool is_global, |
81 Handle<JSFunction> boilerplate); | 70 Handle<JSFunction> boilerplate); |
82 | 71 |
83 // Associate the (source, flags) pair to the given regexp data. | 72 // Associate the (source, flags) pair to the given regexp data. |
84 // This may overwrite an existing mapping. | 73 // This may overwrite an existing mapping. |
85 static void PutRegExp(Handle<String> source, | 74 static void PutRegExp(Handle<String> source, |
86 JSRegExp::Flags flags, | 75 JSRegExp::Flags flags, |
87 Handle<FixedArray> data); | 76 Handle<FixedArray> data); |
88 | 77 |
89 // Clear the cache - also used to initialize the cache at startup. | 78 // Clear the cache - also used to initialize the cache at startup. |
90 static void Clear(); | 79 static void Clear(); |
91 | 80 |
92 // GC support. | 81 // GC support. |
93 static void Iterate(ObjectVisitor* v); | 82 static void Iterate(ObjectVisitor* v); |
94 | 83 |
95 // Notify the cache that a mark-sweep garbage collection is about to | 84 // Notify the cache that a mark-sweep garbage collection is about to |
96 // take place. This is used to retire entries from the cache to | 85 // take place. This is used to retire entries from the cache to |
97 // avoid keeping them alive too long without using them. | 86 // avoid keeping them alive too long without using them. |
98 static void MarkCompactPrologue(); | 87 static void MarkCompactPrologue(); |
99 | 88 |
100 // Enable/disable compilation cache. Used by debugger to disable compilation | 89 // Enable/disable compilation cache. Used by debugger to disable compilation |
101 // cache during debugging to make sure new scripts are always compiled. | 90 // cache during debugging to make sure new scripts are always compiled. |
102 static void Enable(); | 91 static void Enable(); |
103 static void Disable(); | 92 static void Disable(); |
104 }; | 93 }; |
105 | 94 |
106 | 95 |
| 96 // The compilation cache consists of several generational sub-caches which uses |
| 97 // this class as a base class. A sub-cache contains a compilation cache tables |
| 98 // for each generation of the sub-cache. As the same source code string has |
| 99 // different compiled code for scripts and evals. Internally, we use separate |
| 100 // sub-caches to avoid getting the wrong kind of entry when looking up. |
| 101 class CompilationSubCache { |
| 102 public: |
| 103 explicit CompilationSubCache(int generations): generations_(generations) { |
| 104 // tables_ = new Object*[generations]; |
| 105 tables_ = NewArray<Object*>(generations); |
| 106 } |
| 107 virtual ~CompilationSubCache() { |
| 108 DeleteArray(tables_); |
| 109 } |
| 110 |
| 111 // Get the compilation cache tables for a specific generation. |
| 112 Handle<CompilationCacheTable> GetTable(int generation); |
| 113 |
| 114 // Age the sub-cache by evicting the oldest generation and creating a new |
| 115 // young generation. |
| 116 void Age(); |
| 117 |
| 118 // GC support. |
| 119 void Iterate(ObjectVisitor* v); |
| 120 |
| 121 // Clear this sub-cache evicting all its content. |
| 122 void Clear(); |
| 123 |
| 124 // Number of generations in this sub-cache. |
| 125 inline int generations() { return generations_; } |
| 126 |
| 127 private: |
| 128 int generations_; // Number of generations. |
| 129 Object** tables_; // Compilation cache tables - one for each generation. |
| 130 |
| 131 DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationSubCache); |
| 132 }; |
| 133 |
| 134 |
| 135 // Sub-cache for scripts. |
| 136 class CompilationCacheScript: public CompilationSubCache { |
| 137 public: |
| 138 explicit CompilationCacheScript(int generations) |
| 139 : CompilationSubCache(generations) { } |
| 140 |
| 141 Handle<JSFunction> Lookup(Handle<String> source, |
| 142 Handle<Object> name, |
| 143 int line_offset, |
| 144 int column_offset); |
| 145 void Put(Handle<String> source, Handle<JSFunction> boilerplate); |
| 146 |
| 147 private: |
| 148 bool HasOrigin(Handle<JSFunction> boilerplate, |
| 149 Handle<Object> name, |
| 150 int line_offset, |
| 151 int column_offset); |
| 152 |
| 153 DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationCacheScript); |
| 154 }; |
| 155 |
| 156 |
| 157 // Sub-cache for eval scripts. |
| 158 class CompilationCacheEval: public CompilationSubCache { |
| 159 public: |
| 160 explicit CompilationCacheEval(int generations) |
| 161 : CompilationSubCache(generations) { } |
| 162 |
| 163 Handle<JSFunction> Lookup(Handle<String> source, Handle<Context> context); |
| 164 |
| 165 void Put(Handle<String> source, |
| 166 Handle<Context> context, |
| 167 Handle<JSFunction> boilerplate); |
| 168 |
| 169 DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationCacheEval); |
| 170 }; |
| 171 |
| 172 |
| 173 // Sub-cache for regular expressions. |
| 174 class CompilationCacheRegExp: public CompilationSubCache { |
| 175 public: |
| 176 explicit CompilationCacheRegExp(int generations) |
| 177 : CompilationSubCache(generations) { } |
| 178 |
| 179 Handle<FixedArray> Lookup(Handle<String> source, JSRegExp::Flags flags); |
| 180 |
| 181 void Put(Handle<String> source, |
| 182 JSRegExp::Flags flags, |
| 183 Handle<FixedArray> data); |
| 184 |
| 185 DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationCacheRegExp); |
| 186 }; |
| 187 |
| 188 |
107 } } // namespace v8::internal | 189 } } // namespace v8::internal |
108 | 190 |
109 #endif // V8_COMPILATION_CACHE_H_ | 191 #endif // V8_COMPILATION_CACHE_H_ |
OLD | NEW |