compilation-cache.cc 13.3 KB
Newer Older
1
// Copyright 2011 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4

5
#include "src/v8.h"
6

7 8
#include "src/assembler.h"
#include "src/compilation-cache.h"
9

10 11
namespace v8 {
namespace internal {
12

13 14 15 16

// The number of generations for each sub cache.
static const int kRegExpGenerations = 2;

17
// Initial size of each compilation cache table allocated.
18 19 20
static const int kInitialCacheSize = 64;


21 22
CompilationCache::CompilationCache(Isolate* isolate)
    : isolate_(isolate),
23 24 25
      script_(isolate, 1),
      eval_global_(isolate, 1),
      eval_contextual_(isolate, 1),
26
      reg_exp_(isolate, kRegExpGenerations),
27
      enabled_(true) {
28 29 30 31
  CompilationSubCache* subcaches[kSubCacheCount] =
    {&script_, &eval_global_, &eval_contextual_, &reg_exp_};
  for (int i = 0; i < kSubCacheCount; ++i) {
    subcaches_[i] = subcaches[i];
32
  }
33
}
34 35


36
CompilationCache::~CompilationCache() {}
37

38

39
Handle<CompilationCacheTable> CompilationSubCache::GetTable(int generation) {
40
  DCHECK(generation < generations_);
41
  Handle<CompilationCacheTable> result;
42
  if (tables_[generation]->IsUndefined()) {
43
    result = CompilationCacheTable::New(isolate(), kInitialCacheSize);
44
    tables_[generation] = *result;
45
  } else {
46 47
    CompilationCacheTable* table =
        CompilationCacheTable::cast(tables_[generation]);
48
    result = Handle<CompilationCacheTable>(table, isolate());
49 50 51 52
  }
  return result;
}

53

54
void CompilationSubCache::Age() {
55 56 57 58 59 60 61 62
  // Don't directly age single-generation caches.
  if (generations_ == 1) {
    if (tables_[0] != isolate()->heap()->undefined_value()) {
      CompilationCacheTable::cast(tables_[0])->Age();
    }
    return;
  }

63 64 65
  // Age the generations implicitly killing off the oldest.
  for (int i = generations_ - 1; i > 0; i--) {
    tables_[i] = tables_[i - 1];
66
  }
67 68

  // Set the first generation as unborn.
69
  tables_[0] = isolate()->heap()->undefined_value();
70 71 72
}


73
void CompilationSubCache::IterateFunctions(ObjectVisitor* v) {
74
  Object* undefined = isolate()->heap()->undefined_value();
75 76 77 78 79 80 81 82
  for (int i = 0; i < generations_; i++) {
    if (tables_[i] != undefined) {
      reinterpret_cast<CompilationCacheTable*>(tables_[i])->IterateElements(v);
    }
  }
}


83 84 85 86 87 88
void CompilationSubCache::Iterate(ObjectVisitor* v) {
  v->VisitPointers(&tables_[0], &tables_[generations_]);
}


void CompilationSubCache::Clear() {
89
  MemsetPointer(tables_, isolate()->heap()->undefined_value(), generations_);
90 91 92
}


93 94 95
void CompilationSubCache::Remove(Handle<SharedFunctionInfo> function_info) {
  // Probe the script generation tables. Make sure not to leak handles
  // into the caller's handle scope.
96
  { HandleScope scope(isolate());
97 98 99 100 101 102 103 104
    for (int generation = 0; generation < generations(); generation++) {
      Handle<CompilationCacheTable> table = GetTable(generation);
      table->Remove(*function_info);
    }
  }
}


105 106
CompilationCacheScript::CompilationCacheScript(Isolate* isolate,
                                               int generations)
107
    : CompilationSubCache(isolate, generations) {}
108 109


110 111 112
// We only re-use a cached function for some script source code if the
// script originates from the same place. This is to avoid issues
// when reporting errors, etc.
113 114 115
bool CompilationCacheScript::HasOrigin(Handle<SharedFunctionInfo> function_info,
                                       Handle<Object> name, int line_offset,
                                       int column_offset,
116
                                       ScriptOriginOptions resource_options) {
117
  Handle<Script> script =
118
      Handle<Script>(Script::cast(function_info->script()), isolate());
119 120 121 122 123 124 125 126 127 128
  // If the script name isn't set, the boilerplate script should have
  // an undefined name to have the same origin.
  if (name.is_null()) {
    return script->name()->IsUndefined();
  }
  // Do the fast bailout checks first.
  if (line_offset != script->line_offset()->value()) return false;
  if (column_offset != script->column_offset()->value()) return false;
  // Check that both names are strings. If not, no match.
  if (!name->IsString() || !script->name()->IsString()) return false;
129 130
  // Are the origin_options same?
  if (resource_options.Flags() != script->origin_options().Flags())
131
    return false;
132
  // Compare the two name strings for equality.
133 134
  return String::Equals(Handle<String>::cast(name),
                        Handle<String>(String::cast(script->name())));
135 136 137
}


138 139 140 141
// TODO(245): Need to allow identical code from different contexts to
// be cached in the same script generation. Currently the first use
// will be cached, but subsequent code from different source / line
// won't.
142
Handle<SharedFunctionInfo> CompilationCacheScript::Lookup(
143
    Handle<String> source, Handle<Object> name, int line_offset,
144 145
    int column_offset, ScriptOriginOptions resource_options,
    Handle<Context> context, LanguageMode language_mode) {
146
  Object* result = NULL;
147
  int generation;
148 149 150

  // Probe the script generation tables. Make sure not to leak handles
  // into the caller's handle scope.
151
  { HandleScope scope(isolate());
152
    for (generation = 0; generation < generations(); generation++) {
153
      Handle<CompilationCacheTable> table = GetTable(generation);
154
      Handle<Object> probe = table->Lookup(source, context, language_mode);
155 156 157
      if (probe->IsSharedFunctionInfo()) {
        Handle<SharedFunctionInfo> function_info =
            Handle<SharedFunctionInfo>::cast(probe);
158
        // Break when we've found a suitable shared function info that
159
        // matches the origin.
160
        if (HasOrigin(function_info, name, line_offset, column_offset,
161
                      resource_options)) {
162
          result = *function_info;
163 164 165 166 167 168
          break;
        }
      }
    }
  }

whesse@chromium.org's avatar
whesse@chromium.org committed
169
  // Once outside the manacles of the handle scope, we need to recheck
170 171 172
  // to see if we actually found a cached script. If so, we return a
  // handle created in the caller's handle scope.
  if (result != NULL) {
173 174
    Handle<SharedFunctionInfo> shared(SharedFunctionInfo::cast(result),
                                      isolate());
175 176
    DCHECK(
        HasOrigin(shared, name, line_offset, column_offset, resource_options));
177 178
    // If the script was found in a later generation, we promote it to
    // the first generation to let it survive longer in the cache.
179
    if (generation != 0) Put(source, context, language_mode, shared);
180
    isolate()->counters()->compilation_cache_hits()->Increment();
181
    return shared;
182
  } else {
183
    isolate()->counters()->compilation_cache_misses()->Increment();
184
    return Handle<SharedFunctionInfo>::null();
185
  }
186 187 188
}


189
void CompilationCacheScript::Put(Handle<String> source,
190
                                 Handle<Context> context,
191
                                 LanguageMode language_mode,
192
                                 Handle<SharedFunctionInfo> function_info) {
193
  HandleScope scope(isolate());
194
  Handle<CompilationCacheTable> table = GetFirstTable();
195 196
  SetFirstTable(CompilationCacheTable::Put(table, source, context,
                                           language_mode, function_info));
197 198 199
}


200
MaybeHandle<SharedFunctionInfo> CompilationCacheEval::Lookup(
201
    Handle<String> source, Handle<SharedFunctionInfo> outer_info,
202
    LanguageMode language_mode, int scope_position) {
203
  HandleScope scope(isolate());
204 205 206
  // 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.
207
  Handle<Object> result = isolate()->factory()->undefined_value();
208
  int generation;
209 210
  for (generation = 0; generation < generations(); generation++) {
    Handle<CompilationCacheTable> table = GetTable(generation);
211 212
    result =
        table->LookupEval(source, outer_info, language_mode, scope_position);
213
    if (result->IsSharedFunctionInfo()) break;
214
  }
215
  if (result->IsSharedFunctionInfo()) {
216 217
    Handle<SharedFunctionInfo> function_info =
        Handle<SharedFunctionInfo>::cast(result);
218
    if (generation != 0) {
219
      Put(source, outer_info, function_info, scope_position);
220
    }
221
    isolate()->counters()->compilation_cache_hits()->Increment();
222
    return scope.CloseAndEscape(function_info);
223
  } else {
224
    isolate()->counters()->compilation_cache_misses()->Increment();
225
    return MaybeHandle<SharedFunctionInfo>();
226 227 228 229 230
  }
}


void CompilationCacheEval::Put(Handle<String> source,
231
                               Handle<SharedFunctionInfo> outer_info,
232 233
                               Handle<SharedFunctionInfo> function_info,
                               int scope_position) {
234
  HandleScope scope(isolate());
235
  Handle<CompilationCacheTable> table = GetFirstTable();
236
  table = CompilationCacheTable::PutEval(table, source, outer_info,
237 238
                                         function_info, scope_position);
  SetFirstTable(table);
239 240 241
}


242 243 244
MaybeHandle<FixedArray> CompilationCacheRegExp::Lookup(
    Handle<String> source,
    JSRegExp::Flags flags) {
245
  HandleScope scope(isolate());
246 247 248
  // 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.
249
  Handle<Object> result = isolate()->factory()->undefined_value();
250
  int generation;
251 252 253 254
  for (generation = 0; generation < generations(); generation++) {
    Handle<CompilationCacheTable> table = GetTable(generation);
    result = table->LookupRegExp(source, flags);
    if (result->IsFixedArray()) break;
255 256
  }
  if (result->IsFixedArray()) {
257
    Handle<FixedArray> data = Handle<FixedArray>::cast(result);
258 259 260
    if (generation != 0) {
      Put(source, flags, data);
    }
261
    isolate()->counters()->compilation_cache_hits()->Increment();
262
    return scope.CloseAndEscape(data);
263
  } else {
264
    isolate()->counters()->compilation_cache_misses()->Increment();
265
    return MaybeHandle<FixedArray>();
266 267 268 269 270 271 272
  }
}


void CompilationCacheRegExp::Put(Handle<String> source,
                                 JSRegExp::Flags flags,
                                 Handle<FixedArray> data) {
273
  HandleScope scope(isolate());
274 275
  Handle<CompilationCacheTable> table = GetFirstTable();
  SetFirstTable(CompilationCacheTable::PutRegExp(table, source, flags, data));
276 277 278
}


279 280 281
void CompilationCache::Remove(Handle<SharedFunctionInfo> function_info) {
  if (!IsEnabled()) return;

282 283 284
  eval_global_.Remove(function_info);
  eval_contextual_.Remove(function_info);
  script_.Remove(function_info);
285 286 287
}


288
MaybeHandle<SharedFunctionInfo> CompilationCache::LookupScript(
289
    Handle<String> source, Handle<Object> name, int line_offset,
290 291
    int column_offset, ScriptOriginOptions resource_options,
    Handle<Context> context, LanguageMode language_mode) {
292
  if (!IsEnabled()) return MaybeHandle<SharedFunctionInfo>();
293

294
  return script_.Lookup(source, name, line_offset, column_offset,
295
                        resource_options, context, language_mode);
296 297 298
}


299
MaybeHandle<SharedFunctionInfo> CompilationCache::LookupEval(
300
    Handle<String> source, Handle<SharedFunctionInfo> outer_info,
301
    Handle<Context> context, LanguageMode language_mode, int scope_position) {
302
  if (!IsEnabled()) return MaybeHandle<SharedFunctionInfo>();
303

304
  MaybeHandle<SharedFunctionInfo> result;
305
  if (context->IsNativeContext()) {
306
    result =
307
        eval_global_.Lookup(source, outer_info, language_mode, scope_position);
308
  } else {
309
    DCHECK(scope_position != RelocInfo::kNoPosition);
310
    result = eval_contextual_.Lookup(source, outer_info, language_mode,
311
                                     scope_position);
312 313 314 315 316
  }
  return result;
}


317
MaybeHandle<FixedArray> CompilationCache::LookupRegExp(Handle<String> source,
318
                                                       JSRegExp::Flags flags) {
319
  if (!IsEnabled()) return MaybeHandle<FixedArray>();
320

321
  return reg_exp_.Lookup(source, flags);
322 323 324
}


325
void CompilationCache::PutScript(Handle<String> source,
326
                                 Handle<Context> context,
327
                                 LanguageMode language_mode,
328
                                 Handle<SharedFunctionInfo> function_info) {
329
  if (!IsEnabled()) return;
330

331
  script_.Put(source, context, language_mode, function_info);
332 333 334 335
}


void CompilationCache::PutEval(Handle<String> source,
336
                               Handle<SharedFunctionInfo> outer_info,
337
                               Handle<Context> context,
338 339
                               Handle<SharedFunctionInfo> function_info,
                               int scope_position) {
340
  if (!IsEnabled()) return;
341

342
  HandleScope scope(isolate());
343
  if (context->IsNativeContext()) {
344
    eval_global_.Put(source, outer_info, function_info, scope_position);
345
  } else {
346
    DCHECK(scope_position != RelocInfo::kNoPosition);
347
    eval_contextual_.Put(source, outer_info, function_info, scope_position);
348
  }
349 350 351
}


352 353 354 355

void CompilationCache::PutRegExp(Handle<String> source,
                                 JSRegExp::Flags flags,
                                 Handle<FixedArray> data) {
356 357 358 359
  if (!IsEnabled()) {
    return;
  }

360
  reg_exp_.Put(source, flags, data);
361 362 363
}


364
void CompilationCache::Clear() {
365
  for (int i = 0; i < kSubCacheCount; i++) {
366
    subcaches_[i]->Clear();
367 368 369
  }
}

370

371 372
void CompilationCache::Iterate(ObjectVisitor* v) {
  for (int i = 0; i < kSubCacheCount; i++) {
373
    subcaches_[i]->Iterate(v);
374
  }
375 376 377
}


378
void CompilationCache::IterateFunctions(ObjectVisitor* v) {
379
  for (int i = 0; i < kSubCacheCount; i++) {
380
    subcaches_[i]->IterateFunctions(v);
381
  }
382 383 384 385
}


void CompilationCache::MarkCompactPrologue() {
386
  for (int i = 0; i < kSubCacheCount; i++) {
387
    subcaches_[i]->Age();
388
  }
389 390 391
}


392
void CompilationCache::Enable() {
393
  enabled_ = true;
394 395 396 397
}


void CompilationCache::Disable() {
398
  enabled_ = false;
399 400 401 402
  Clear();
}


403 404
}  // namespace internal
}  // namespace v8