1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
|
// Copyright 2011 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "src/codegen/compilation-cache.h"
#include "src/common/globals.h"
#include "src/heap/factory.h"
#include "src/logging/counters.h"
#include "src/logging/log.h"
#include "src/objects/compilation-cache-table-inl.h"
#include "src/objects/objects-inl.h"
#include "src/objects/slots.h"
#include "src/objects/visitors.h"
#include "src/utils/ostreams.h"
namespace v8 {
namespace internal {
// Initial size of each compilation cache table allocated.
static const int kInitialCacheSize = 64;
CompilationCache::CompilationCache(Isolate* isolate)
: isolate_(isolate),
script_(isolate),
eval_global_(isolate),
eval_contextual_(isolate),
reg_exp_(isolate),
enabled_script_and_eval_(true) {}
Handle<CompilationCacheTable> CompilationCacheEvalOrScript::GetTable() {
if (table_.IsUndefined(isolate())) {
return CompilationCacheTable::New(isolate(), kInitialCacheSize);
}
return handle(CompilationCacheTable::cast(table_), isolate());
}
Handle<CompilationCacheTable> CompilationCacheRegExp::GetTable(int generation) {
DCHECK_LT(generation, kGenerations);
Handle<CompilationCacheTable> result;
if (tables_[generation].IsUndefined(isolate())) {
result = CompilationCacheTable::New(isolate(), kInitialCacheSize);
tables_[generation] = *result;
} else {
CompilationCacheTable table =
CompilationCacheTable::cast(tables_[generation]);
result = Handle<CompilationCacheTable>(table, isolate());
}
return result;
}
void CompilationCacheRegExp::Age() {
static_assert(kGenerations > 1);
// Age the generations implicitly killing off the oldest.
for (int i = kGenerations - 1; i > 0; i--) {
tables_[i] = tables_[i - 1];
}
// Set the first generation as unborn.
tables_[0] = ReadOnlyRoots(isolate()).undefined_value();
}
void CompilationCacheScript::Age() {
DisallowGarbageCollection no_gc;
if (!v8_flags.isolate_script_cache_ageing) return;
if (table_.IsUndefined(isolate())) return;
CompilationCacheTable table = CompilationCacheTable::cast(table_);
for (InternalIndex entry : table.IterateEntries()) {
Object key;
if (!table.ToKey(isolate(), entry, &key)) continue;
DCHECK(key.IsWeakFixedArray());
Object value = table.PrimaryValueAt(entry);
if (!value.IsUndefined(isolate())) {
SharedFunctionInfo info = SharedFunctionInfo::cast(value);
if (!info.HasBytecodeArray() ||
info.GetBytecodeArray(isolate()).IsOld()) {
table.SetPrimaryValueAt(entry,
ReadOnlyRoots(isolate()).undefined_value());
}
}
}
}
void CompilationCacheEval::Age() {
DisallowGarbageCollection no_gc;
if (table_.IsUndefined(isolate())) return;
CompilationCacheTable table = CompilationCacheTable::cast(table_);
for (InternalIndex entry : table.IterateEntries()) {
Object key;
if (!table.ToKey(isolate(), entry, &key)) continue;
if (key.IsNumber(isolate())) {
// The ageing mechanism for the initial dummy entry in the eval cache.
// The 'key' is the hash represented as a Number. The 'value' is a smi
// counting down from kHashGenerations. On reaching zero, the entry is
// cleared.
// Note: The following static assert only establishes an explicit
// connection between initialization- and use-sites of the smi value
// field.
static_assert(CompilationCacheTable::kHashGenerations);
const int new_count = Smi::ToInt(table.PrimaryValueAt(entry)) - 1;
if (new_count == 0) {
table.RemoveEntry(entry);
} else {
DCHECK_GT(new_count, 0);
table.SetPrimaryValueAt(entry, Smi::FromInt(new_count),
SKIP_WRITE_BARRIER);
}
} else {
DCHECK(key.IsFixedArray());
// The ageing mechanism for eval caches.
SharedFunctionInfo info =
SharedFunctionInfo::cast(table.PrimaryValueAt(entry));
if (!info.HasBytecodeArray() ||
info.GetBytecodeArray(isolate()).IsOld()) {
table.RemoveEntry(entry);
}
}
}
}
void CompilationCacheEvalOrScript::Iterate(RootVisitor* v) {
v->VisitRootPointer(Root::kCompilationCache, nullptr,
FullObjectSlot(&table_));
}
void CompilationCacheRegExp::Iterate(RootVisitor* v) {
v->VisitRootPointers(Root::kCompilationCache, nullptr,
FullObjectSlot(&tables_[0]),
FullObjectSlot(&tables_[kGenerations]));
}
void CompilationCacheEvalOrScript::Clear() {
table_ = ReadOnlyRoots(isolate()).undefined_value();
}
void CompilationCacheRegExp::Clear() {
MemsetPointer(reinterpret_cast<Address*>(tables_),
ReadOnlyRoots(isolate()).undefined_value().ptr(), kGenerations);
}
void CompilationCacheEvalOrScript::Remove(
Handle<SharedFunctionInfo> function_info) {
if (table_.IsUndefined(isolate())) return;
CompilationCacheTable::cast(table_).Remove(*function_info);
}
CompilationCacheScript::LookupResult CompilationCacheScript::Lookup(
Handle<String> source, const ScriptDetails& script_details) {
LookupResult result;
LookupResult::RawObjects raw_result_for_escaping_handle_scope;
// Probe the script table. Make sure not to leak handles
// into the caller's handle scope.
{
HandleScope scope(isolate());
Handle<CompilationCacheTable> table = GetTable();
LookupResult probe = CompilationCacheTable::LookupScript(
table, source, script_details, isolate());
raw_result_for_escaping_handle_scope = probe.GetRawObjects();
}
result = LookupResult::FromRawObjects(raw_result_for_escaping_handle_scope,
isolate());
// Once outside the manacles of the handle scope, we need to recheck
// to see if we actually found a cached script. If so, we return a
// handle created in the caller's handle scope.
Handle<Script> script;
if (result.script().ToHandle(&script)) {
Handle<SharedFunctionInfo> sfi;
if (result.toplevel_sfi().ToHandle(&sfi)) {
isolate()->counters()->compilation_cache_hits()->Increment();
LOG(isolate(), CompilationCacheEvent("hit", "script", *sfi));
} else {
isolate()->counters()->compilation_cache_partial_hits()->Increment();
}
} else {
isolate()->counters()->compilation_cache_misses()->Increment();
}
return result;
}
void CompilationCacheScript::Put(Handle<String> source,
Handle<SharedFunctionInfo> function_info) {
HandleScope scope(isolate());
Handle<CompilationCacheTable> table = GetTable();
table_ = *CompilationCacheTable::PutScript(table, source, function_info,
isolate());
}
InfoCellPair CompilationCacheEval::Lookup(Handle<String> source,
Handle<SharedFunctionInfo> outer_info,
Handle<Context> native_context,
LanguageMode language_mode,
int position) {
HandleScope scope(isolate());
// Make sure not to leak the table into the surrounding handle
// scope. Otherwise, we risk keeping old tables around even after
// having cleared the cache.
InfoCellPair result;
Handle<CompilationCacheTable> table = GetTable();
result = CompilationCacheTable::LookupEval(
table, source, outer_info, native_context, language_mode, position);
if (result.has_shared()) {
isolate()->counters()->compilation_cache_hits()->Increment();
} else {
isolate()->counters()->compilation_cache_misses()->Increment();
}
return result;
}
void CompilationCacheEval::Put(Handle<String> source,
Handle<SharedFunctionInfo> outer_info,
Handle<SharedFunctionInfo> function_info,
Handle<Context> native_context,
Handle<FeedbackCell> feedback_cell,
int position) {
HandleScope scope(isolate());
Handle<CompilationCacheTable> table = GetTable();
table_ =
*CompilationCacheTable::PutEval(table, source, outer_info, function_info,
native_context, feedback_cell, position);
}
MaybeHandle<FixedArray> CompilationCacheRegExp::Lookup(Handle<String> source,
JSRegExp::Flags flags) {
HandleScope scope(isolate());
// Make sure not to leak the table into the surrounding handle
// scope. Otherwise, we risk keeping old tables around even after
// having cleared the cache.
Handle<Object> result = isolate()->factory()->undefined_value();
int generation;
for (generation = 0; generation < kGenerations; generation++) {
Handle<CompilationCacheTable> table = GetTable(generation);
result = table->LookupRegExp(source, flags);
if (result->IsFixedArray()) break;
}
if (result->IsFixedArray()) {
Handle<FixedArray> data = Handle<FixedArray>::cast(result);
if (generation != 0) {
Put(source, flags, data);
}
isolate()->counters()->compilation_cache_hits()->Increment();
return scope.CloseAndEscape(data);
} else {
isolate()->counters()->compilation_cache_misses()->Increment();
return MaybeHandle<FixedArray>();
}
}
void CompilationCacheRegExp::Put(Handle<String> source, JSRegExp::Flags flags,
Handle<FixedArray> data) {
HandleScope scope(isolate());
Handle<CompilationCacheTable> table = GetTable(0);
tables_[0] =
*CompilationCacheTable::PutRegExp(isolate(), table, source, flags, data);
}
void CompilationCache::Remove(Handle<SharedFunctionInfo> function_info) {
if (!IsEnabledScriptAndEval()) return;
eval_global_.Remove(function_info);
eval_contextual_.Remove(function_info);
script_.Remove(function_info);
}
CompilationCacheScript::LookupResult CompilationCache::LookupScript(
Handle<String> source, const ScriptDetails& script_details,
LanguageMode language_mode) {
if (!IsEnabledScript(language_mode)) return {};
return script_.Lookup(source, script_details);
}
InfoCellPair CompilationCache::LookupEval(Handle<String> source,
Handle<SharedFunctionInfo> outer_info,
Handle<Context> context,
LanguageMode language_mode,
int position) {
InfoCellPair result;
if (!IsEnabledScriptAndEval()) return result;
const char* cache_type;
if (context->IsNativeContext()) {
result = eval_global_.Lookup(source, outer_info, context, language_mode,
position);
cache_type = "eval-global";
} else {
DCHECK_NE(position, kNoSourcePosition);
Handle<Context> native_context(context->native_context(), isolate());
result = eval_contextual_.Lookup(source, outer_info, native_context,
language_mode, position);
cache_type = "eval-contextual";
}
if (result.has_shared()) {
LOG(isolate(), CompilationCacheEvent("hit", cache_type, result.shared()));
}
return result;
}
MaybeHandle<FixedArray> CompilationCache::LookupRegExp(Handle<String> source,
JSRegExp::Flags flags) {
return reg_exp_.Lookup(source, flags);
}
void CompilationCache::PutScript(Handle<String> source,
LanguageMode language_mode,
Handle<SharedFunctionInfo> function_info) {
if (!IsEnabledScript(language_mode)) return;
LOG(isolate(), CompilationCacheEvent("put", "script", *function_info));
script_.Put(source, function_info);
}
void CompilationCache::PutEval(Handle<String> source,
Handle<SharedFunctionInfo> outer_info,
Handle<Context> context,
Handle<SharedFunctionInfo> function_info,
Handle<FeedbackCell> feedback_cell,
int position) {
if (!IsEnabledScriptAndEval()) return;
const char* cache_type;
HandleScope scope(isolate());
if (context->IsNativeContext()) {
eval_global_.Put(source, outer_info, function_info, context, feedback_cell,
position);
cache_type = "eval-global";
} else {
DCHECK_NE(position, kNoSourcePosition);
Handle<Context> native_context(context->native_context(), isolate());
eval_contextual_.Put(source, outer_info, function_info, native_context,
feedback_cell, position);
cache_type = "eval-contextual";
}
LOG(isolate(), CompilationCacheEvent("put", cache_type, *function_info));
}
void CompilationCache::PutRegExp(Handle<String> source, JSRegExp::Flags flags,
Handle<FixedArray> data) {
reg_exp_.Put(source, flags, data);
}
void CompilationCache::Clear() {
script_.Clear();
eval_global_.Clear();
eval_contextual_.Clear();
reg_exp_.Clear();
}
void CompilationCache::Iterate(RootVisitor* v) {
script_.Iterate(v);
eval_global_.Iterate(v);
eval_contextual_.Iterate(v);
reg_exp_.Iterate(v);
}
void CompilationCache::MarkCompactPrologue() {
script_.Age();
eval_global_.Age();
eval_contextual_.Age();
reg_exp_.Age();
}
void CompilationCache::EnableScriptAndEval() {
enabled_script_and_eval_ = true;
}
void CompilationCache::DisableScriptAndEval() {
enabled_script_and_eval_ = false;
Clear();
}
} // namespace internal
} // namespace v8
|