Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(983)

Side by Side Diff: src/compilation-cache.cc

Issue 1933: Generalized the EvalCache into a CompilationCache and enabled... (Closed) Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge/
Patch Set: Created 12 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
Property Changes:
Added: svn:eol-style
+ native
OLDNEW
(Empty)
1 // 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
32 namespace v8 { namespace internal {
33
34 enum {
35 NUMBER_OF_ENTRY_KINDS = CompilationCache::EVAL_CONTEXTUAL + 1
36 };
37
38
39 // Keep separate tables for the different entry kinds.
40 static Object* tables[NUMBER_OF_ENTRY_KINDS] = { 0, };
41
42
43 static Handle<CompilationCacheTable> AllocateTable(int size) {
44 CALL_HEAP_FUNCTION(CompilationCacheTable::Allocate(size),
45 CompilationCacheTable);
46 }
47
48
49 static 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.
66 static 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
87 static Handle<JSFunction> Lookup(Handle<String> source,
88 CompilationCache::Entry entry) {
89 Handle<CompilationCacheTable> table = GetTable(entry);
90 Object* result = table->Lookup(*source);
91 if (result->IsJSFunction()) {
92 return Handle<JSFunction>(JSFunction::cast(result));
93 } else {
94 return Handle<JSFunction>::null();
95 }
96 }
97
98
99 Handle<JSFunction> CompilationCache::LookupScript(Handle<String> source,
100 Handle<Object> name,
101 int line_offset,
102 int column_offset) {
103 Handle<JSFunction> result = Lookup(source, SCRIPT);
104 if (result.is_null()) {
105 Counters::compilation_cache_misses.Increment();
106 } else if (HasOrigin(result, name, line_offset, column_offset)) {
107 Counters::compilation_cache_hits.Increment();
108 } else {
109 result = Handle<JSFunction>::null();
110 Counters::compilation_cache_misses.Increment();
111 }
112 return result;
113 }
114
115
116 Handle<JSFunction> CompilationCache::LookupEval(Handle<String> source,
117 Entry entry) {
118 ASSERT(entry == EVAL_GLOBAL || entry == EVAL_CONTEXTUAL);
119 Handle<JSFunction> result = Lookup(source, entry);
120 if (result.is_null()) {
121 Counters::compilation_cache_misses.Increment();
122 } else {
123 Counters::compilation_cache_hits.Increment();
124 }
125 return result;
126 }
127
128
129 void CompilationCache::Associate(Handle<String> source,
130 Entry entry,
131 Handle<JSFunction> boilerplate) {
132 ASSERT(boilerplate->IsBoilerplate());
133 Handle<CompilationCacheTable> table = GetTable(entry);
134 CALL_HEAP_FUNCTION_VOID(table->Put(*source, *boilerplate));
135 }
136
137
138 void CompilationCache::Clear() {
139 for (int i = 0; i < NUMBER_OF_ENTRY_KINDS; i++) {
140 tables[i] = Heap::undefined_value();
141 }
142 }
143
144
145 void CompilationCache::Iterate(ObjectVisitor* v) {
146 v->VisitPointers(&tables[0], &tables[NUMBER_OF_ENTRY_KINDS]);
147 }
148
149
150 } } // namespace v8::internal
OLDNEW
« src/compilation-cache.h ('K') | « src/compilation-cache.h ('k') | src/compiler.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698