blob: bf326212466ab102e13b5912111ac6bd82b441eb [file] [log] [blame]
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001// Copyright 2008 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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.
27
28#include "v8.h"
29
30#include "compilation-cache.h"
31
32namespace v8 { namespace internal {
33
34enum {
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +000035 NUMBER_OF_ENTRY_KINDS = CompilationCache::LAST_ENTRY + 1
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000036};
37
38
39// Keep separate tables for the different entry kinds.
40static Object* tables[NUMBER_OF_ENTRY_KINDS] = { 0, };
41
42
43static Handle<CompilationCacheTable> AllocateTable(int size) {
44 CALL_HEAP_FUNCTION(CompilationCacheTable::Allocate(size),
45 CompilationCacheTable);
46}
47
48
49static Handle<CompilationCacheTable> GetTable(CompilationCache::Entry entry) {
50 Handle<CompilationCacheTable> result;
51 if (tables[entry]->IsUndefined()) {
52 static const int kInitialCacheSize = 64;
53 result = AllocateTable(kInitialCacheSize);
54 tables[entry] = *result;
55 } else {
56 CompilationCacheTable* table = CompilationCacheTable::cast(tables[entry]);
57 result = Handle<CompilationCacheTable>(table);
58 }
59 return result;
60}
61
62
63// We only re-use a cached function for some script source code if the
64// script originates from the same places. This is to avoid issues
65// when reporting errors, etc.
66static bool HasOrigin(Handle<JSFunction> boilerplate,
67 Handle<Object> name,
68 int line_offset,
69 int column_offset) {
70 Handle<Script> script =
71 Handle<Script>(Script::cast(boilerplate->shared()->script()));
72 // If the script name isn't set, the boilerplate script should have
73 // an undefined name to have the same origin.
74 if (name.is_null()) {
75 return script->name()->IsUndefined();
76 }
77 // Do the fast bailout checks first.
78 if (line_offset != script->line_offset()->value()) return false;
79 if (column_offset != script->column_offset()->value()) return false;
80 // Check that both names are strings. If not, no match.
81 if (!name->IsString() || !script->name()->IsString()) return false;
82 // Compare the two name strings for equality.
83 return String::cast(*name)->Equals(String::cast(script->name()));
84}
85
86
87static Handle<JSFunction> Lookup(Handle<String> source,
88 CompilationCache::Entry entry) {
89 // Make sure not to leak the table into the surrounding handle
90 // scope. Otherwise, we risk keeping old tables around even after
91 // having cleared the cache.
92 Object* result;
93 { HandleScope scope;
94 Handle<CompilationCacheTable> table = GetTable(entry);
95 result = table->Lookup(*source);
96 }
97 if (result->IsJSFunction()) {
98 return Handle<JSFunction>(JSFunction::cast(result));
99 } else {
100 return Handle<JSFunction>::null();
101 }
102}
103
104
105Handle<JSFunction> CompilationCache::LookupScript(Handle<String> source,
106 Handle<Object> name,
107 int line_offset,
108 int column_offset) {
109 Handle<JSFunction> result = Lookup(source, SCRIPT);
110 if (result.is_null()) {
111 Counters::compilation_cache_misses.Increment();
112 } else if (HasOrigin(result, name, line_offset, column_offset)) {
113 Counters::compilation_cache_hits.Increment();
114 } else {
115 result = Handle<JSFunction>::null();
116 Counters::compilation_cache_misses.Increment();
117 }
118 return result;
119}
120
121
122Handle<JSFunction> CompilationCache::LookupEval(Handle<String> source,
123 Entry entry) {
124 ASSERT(entry == EVAL_GLOBAL || entry == EVAL_CONTEXTUAL);
125 Handle<JSFunction> result = Lookup(source, entry);
126 if (result.is_null()) {
127 Counters::compilation_cache_misses.Increment();
128 } else {
129 Counters::compilation_cache_hits.Increment();
130 }
131 return result;
132}
133
134
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000135void CompilationCache::PutFunction(Handle<String> source,
136 Entry entry,
137 Handle<JSFunction> boilerplate) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000138 HandleScope scope;
139 ASSERT(boilerplate->IsBoilerplate());
140 Handle<CompilationCacheTable> table = GetTable(entry);
141 CALL_HEAP_FUNCTION_VOID(table->Put(*source, *boilerplate));
142}
143
144
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000145Handle<FixedArray> CompilationCache::LookupRegExp(Handle<String> source,
146 JSRegExp::Flags flags) {
147 Handle<CompilationCacheTable> table = GetTable(REGEXP);
148 Object* result = table->LookupRegExp(*source, flags);
149 if (result->IsFixedArray()) {
150 Counters::regexp_cache_hits.Increment();
151 return Handle<FixedArray>(FixedArray::cast(result));
152 } else {
153 Counters::regexp_cache_misses.Increment();
154 return Handle<FixedArray>();
155 }
156}
157
158
159void CompilationCache::PutRegExp(Handle<String> source,
160 JSRegExp::Flags flags,
161 Handle<FixedArray> data) {
162 HandleScope scope;
163 Handle<CompilationCacheTable> table = GetTable(REGEXP);
164 CALL_HEAP_FUNCTION_VOID(table->PutRegExp(*source, flags, *data));
165}
166
167
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000168void CompilationCache::Clear() {
169 for (int i = 0; i < NUMBER_OF_ENTRY_KINDS; i++) {
170 tables[i] = Heap::undefined_value();
171 }
172}
173
174
175void CompilationCache::Iterate(ObjectVisitor* v) {
176 v->VisitPointers(&tables[0], &tables[NUMBER_OF_ENTRY_KINDS]);
177}
178
179
180} } // namespace v8::internal