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

Side by Side Diff: Source/bindings/core/v8/V8ScriptRunner.cpp

Issue 549533002: Follow-on to "Restore in-memory parser cache for V8 compile" (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: rebase Created 6 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
« no previous file with comments | « no previous file | Source/bindings/core/v8/V8ScriptRunnerTest.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2009 Google Inc. All rights reserved. 2 * Copyright (C) 2009 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions 5 * modification, are permitted provided that the following conditions
6 * are met: 6 * are met:
7 * 1. Redistributions of source code must retain the above copyright 7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer. 8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright 9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the 10 * notice, this list of conditions and the following disclaimer in the
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
55 // If we are already handling a recursion level error, we should 55 // If we are already handling a recursion level error, we should
56 // not invoke v8::Function::Call. 56 // not invoke v8::Function::Call.
57 return v8::Undefined(isolate); 57 return v8::Undefined(isolate);
58 } 58 }
59 V8PerIsolateData::from(isolate)->setIsHandlingRecursionLevelError(true); 59 V8PerIsolateData::from(isolate)->setIsHandlingRecursionLevelError(true);
60 v8::Local<v8::Value> result = v8::Function::New(isolate, throwStackOverflowE xception)->Call(v8::Undefined(isolate), 0, 0); 60 v8::Local<v8::Value> result = v8::Function::New(isolate, throwStackOverflowE xception)->Call(v8::Undefined(isolate), 0, 0);
61 V8PerIsolateData::from(isolate)->setIsHandlingRecursionLevelError(false); 61 V8PerIsolateData::from(isolate)->setIsHandlingRecursionLevelError(false);
62 return result; 62 return result;
63 } 63 }
64 64
65 v8::Local<v8::Script> compileAndProduceCache(v8::Handle<v8::String> code, v8::Sc riptOrigin origin, ScriptResource* resource, v8::Isolate* isolate, v8::ScriptCom piler::CompileOptions options, unsigned cacheTag, Resource::MetadataCacheType ca cheType) 65 v8::Local<v8::Script> compileAndProduceCache(v8::Isolate* isolate, v8::Handle<v8 ::String> code, v8::ScriptOrigin origin, ScriptResource* resource, v8::ScriptCom piler::CompileOptions options, unsigned cacheTag, Resource::MetadataCacheType ca cheType)
66 { 66 {
67 v8::ScriptCompiler::Source source(code, origin); 67 v8::ScriptCompiler::Source source(code, origin);
68 v8::Local<v8::Script> script = v8::ScriptCompiler::Compile(isolate, &source, options); 68 v8::Local<v8::Script> script = v8::ScriptCompiler::Compile(isolate, &source, options);
69 const v8::ScriptCompiler::CachedData* cachedData = source.GetCachedData(); 69 const v8::ScriptCompiler::CachedData* cachedData = source.GetCachedData();
70 if (resource && cachedData) { 70 if (resource && cachedData) {
71 resource->clearCachedMetadata(); 71 resource->clearCachedMetadata();
72 resource->setCachedMetadata( 72 resource->setCachedMetadata(
73 cacheTag, 73 cacheTag,
74 reinterpret_cast<const char*>(cachedData->data), 74 reinterpret_cast<const char*>(cachedData->data),
75 cachedData->length, 75 cachedData->length,
76 cacheType); 76 cacheType);
77 } 77 }
78 return script; 78 return script;
79 } 79 }
80 80
81 v8::Local<v8::Script> compileAndConsumeCache(v8::Handle<v8::String> code, v8::Sc riptOrigin origin, ScriptResource* resource, v8::Isolate* isolate, v8::ScriptCom piler::CompileOptions options, unsigned cacheTag) 81 v8::Local<v8::Script> compileAndConsumeCache(v8::Isolate* isolate, v8::Handle<v8 ::String> code, v8::ScriptOrigin origin, ScriptResource* resource, v8::ScriptCom piler::CompileOptions options, unsigned cacheTag)
82 { 82 {
83 // Consume existing cache data: 83 // Consume existing cache data:
84 CachedMetadata* cachedMetadata = resource->cachedMetadata(cacheTag); 84 CachedMetadata* cachedMetadata = resource->cachedMetadata(cacheTag);
85 v8::ScriptCompiler::CachedData* cachedData = new v8::ScriptCompiler::CachedD ata( 85 v8::ScriptCompiler::CachedData* cachedData = new v8::ScriptCompiler::CachedD ata(
86 reinterpret_cast<const uint8_t*>(cachedMetadata->data()), 86 reinterpret_cast<const uint8_t*>(cachedMetadata->data()),
87 cachedMetadata->size(), 87 cachedMetadata->size(),
88 v8::ScriptCompiler::CachedData::BufferNotOwned); 88 v8::ScriptCompiler::CachedData::BufferNotOwned);
89 v8::ScriptCompiler::Source source(code, origin, cachedData); 89 v8::ScriptCompiler::Source source(code, origin, cachedData);
90 return v8::ScriptCompiler::Compile(isolate, &source, options); 90 return v8::ScriptCompiler::Compile(isolate, &source, options);
91 } 91 }
92 92
93 unsigned tagForParserCache()
94 {
95 return StringHash::hash(v8::V8::GetVersion()) * 2;
96 }
97
98 unsigned tagForCodeCache()
99 {
100 return StringHash::hash(v8::V8::GetVersion()) * 2 + 1;
101 }
102
93 } // namespace 103 } // namespace
94 104
95 v8::Local<v8::Script> V8ScriptRunner::compileScript(const ScriptSourceCode& sour ce, v8::Isolate* isolate, AccessControlStatus corsStatus, V8CacheOptions cacheOp tions) 105 v8::Local<v8::Script> V8ScriptRunner::compileScript(const ScriptSourceCode& sour ce, v8::Isolate* isolate, AccessControlStatus corsStatus, V8CacheOptions cacheOp tions)
96 { 106 {
97 return compileScript(v8String(isolate, source.source()), source.url(), sourc e.startPosition(), source.resource(), isolate, corsStatus, cacheOptions); 107 return compileScript(v8String(isolate, source.source()), source.url(), sourc e.startPosition(), source.resource(), isolate, corsStatus, cacheOptions);
98 } 108 }
99 109
100 v8::Local<v8::Script> V8ScriptRunner::compileScript(v8::Handle<v8::String> code, const String& fileName, const TextPosition& scriptStartPosition, ScriptResource * resource, v8::Isolate* isolate, AccessControlStatus corsStatus, V8CacheOptions cacheOptions) 110 v8::Local<v8::Script> V8ScriptRunner::compileScript(v8::Handle<v8::String> code, const String& fileName, const TextPosition& scriptStartPosition, ScriptResource * resource, v8::Isolate* isolate, AccessControlStatus corsStatus, V8CacheOptions cacheOptions)
101 { 111 {
102 TRACE_EVENT1("v8", "v8.compile", "fileName", fileName.utf8()); 112 TRACE_EVENT1("v8", "v8.compile", "fileName", fileName.utf8());
103 TRACE_EVENT_SCOPED_SAMPLING_STATE("v8", "V8Compile"); 113 TRACE_EVENT_SCOPED_SAMPLING_STATE("v8", "V8Compile");
104 114
105 // NOTE: For compatibility with WebCore, ScriptSourceCode's line starts at 115 // NOTE: For compatibility with WebCore, ScriptSourceCode's line starts at
106 // 1, whereas v8 starts at 0. 116 // 1, whereas v8 starts at 0.
107 v8::Handle<v8::String> name = v8String(isolate, fileName); 117 v8::Handle<v8::String> name = v8String(isolate, fileName);
108 v8::Handle<v8::Integer> line = v8::Integer::New(isolate, scriptStartPosition .m_line.zeroBasedInt()); 118 v8::Handle<v8::Integer> line = v8::Integer::New(isolate, scriptStartPosition .m_line.zeroBasedInt());
109 v8::Handle<v8::Integer> column = v8::Integer::New(isolate, scriptStartPositi on.m_column.zeroBasedInt()); 119 v8::Handle<v8::Integer> column = v8::Integer::New(isolate, scriptStartPositi on.m_column.zeroBasedInt());
110 v8::Handle<v8::Boolean> isSharedCrossOrigin = corsStatus == SharableCrossOri gin ? v8::True(isolate) : v8::False(isolate); 120 v8::Handle<v8::Boolean> isSharedCrossOrigin = corsStatus == SharableCrossOri gin ? v8::True(isolate) : v8::False(isolate);
111 v8::ScriptOrigin origin(name, line, column, isSharedCrossOrigin); 121 v8::ScriptOrigin origin(name, line, column, isSharedCrossOrigin);
112 122
113 v8::Local<v8::Script> script; 123 v8::Local<v8::Script> script;
114 unsigned cacheTag = 0; 124 unsigned cacheTag = 0;
115 if (!resource || !resource->url().protocolIsInHTTPFamily() || code->Length() < 1024) { 125 if (!resource || !resource->url().protocolIsInHTTPFamily() || code->Length() < 1024) {
116 v8::ScriptCompiler::Source source(code, origin); 126 v8::ScriptCompiler::Source source(code, origin);
117 script = v8::ScriptCompiler::Compile(isolate, &source, v8::ScriptCompile r::kNoCompileOptions); 127 script = v8::ScriptCompiler::Compile(isolate, &source, v8::ScriptCompile r::kNoCompileOptions);
118 } else { 128 } else {
119 switch (cacheOptions) { 129 switch (cacheOptions) {
120 case V8CacheOptionsParse: 130 case V8CacheOptionsParse:
121 cacheTag = StringHash::hash(v8::V8::GetVersion()) * 2; 131 cacheTag = tagForParserCache();
122 script = resource->cachedMetadata(cacheTag) 132 script = resource->cachedMetadata(cacheTag)
123 ? compileAndConsumeCache(code, origin, resource, isolate, v8::Sc riptCompiler::kConsumeParserCache, cacheTag) 133 ? compileAndConsumeCache(isolate, code, origin, resource, v8::Sc riptCompiler::kConsumeParserCache, cacheTag)
124 : compileAndProduceCache(code, origin, resource, isolate, v8::Sc riptCompiler::kProduceParserCache, cacheTag, Resource::SendToPlatform); 134 : compileAndProduceCache(isolate, code, origin, resource, v8::Sc riptCompiler::kProduceParserCache, cacheTag, Resource::SendToPlatform);
125 break; 135 break;
126 case V8CacheOptionsCode: 136 case V8CacheOptionsCode:
127 cacheTag = StringHash::hash(v8::V8::GetVersion()) * 2 + 1; 137 cacheTag = tagForCodeCache();
128 script = resource->cachedMetadata(cacheTag) 138 script = resource->cachedMetadata(cacheTag)
129 ? compileAndConsumeCache(code, origin, resource, isolate, v8::Sc riptCompiler::kConsumeCodeCache, cacheTag) 139 ? compileAndConsumeCache(isolate, code, origin, resource, v8::Sc riptCompiler::kConsumeCodeCache, cacheTag)
130 : compileAndProduceCache(code, origin, resource, isolate, v8::Sc riptCompiler::kProduceCodeCache, cacheTag, Resource::SendToPlatform); 140 : compileAndProduceCache(isolate, code, origin, resource, v8::Sc riptCompiler::kProduceCodeCache, cacheTag, Resource::SendToPlatform);
131 break; 141 break;
132 case V8CacheOptionsOff: 142 case V8CacheOptionsOff:
133 // Previous behaviour was to always generate an in-memory parser 143 // Previous behaviour was to always generate an in-memory parser
134 // cache. We emulate this here. 144 // cache. We emulate this here.
135 // TODO(vogelheim): Determine whether this should get its own 145 // FIXME: Determine whether this should get its own setting, so we
136 // setting, so we can also have a true 'off'. 146 // can also have a true 'off'.
137 cacheTag = StringHash::hash(v8::V8::GetVersion()) * 2; 147 cacheTag = tagForParserCache();
138 script = resource->cachedMetadata(cacheTag) 148 script = resource->cachedMetadata(cacheTag)
139 ? compileAndConsumeCache(code, origin, resource, isolate, v8::Sc riptCompiler::kConsumeParserCache, cacheTag) 149 ? compileAndConsumeCache(isolate, code, origin, resource, v8::Sc riptCompiler::kConsumeParserCache, cacheTag)
140 : compileAndProduceCache(code, origin, resource, isolate, v8::Sc riptCompiler::kProduceParserCache, cacheTag, Resource::CacheLocally); 150 : compileAndProduceCache(isolate, code, origin, resource, v8::Sc riptCompiler::kProduceParserCache, cacheTag, Resource::CacheLocally);
141 break; 151 break;
142 } 152 }
143 } 153 }
144 return script; 154 return script;
145 } 155 }
146 156
147 v8::Local<v8::Value> V8ScriptRunner::runCompiledScript(v8::Handle<v8::Script> sc ript, ExecutionContext* context, v8::Isolate* isolate) 157 v8::Local<v8::Value> V8ScriptRunner::runCompiledScript(v8::Handle<v8::Script> sc ript, ExecutionContext* context, v8::Isolate* isolate)
148 { 158 {
149 if (script.IsEmpty()) 159 if (script.IsEmpty())
150 return v8::Local<v8::Value>(); 160 return v8::Local<v8::Value>();
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
257 { 267 {
258 TRACE_EVENT0("v8", "v8.newInstance"); 268 TRACE_EVENT0("v8", "v8.newInstance");
259 TRACE_EVENT_SCOPED_SAMPLING_STATE("v8", "V8Execution"); 269 TRACE_EVENT_SCOPED_SAMPLING_STATE("v8", "V8Execution");
260 V8RecursionScope scope(isolate, context); 270 V8RecursionScope scope(isolate, context);
261 v8::Local<v8::Object> result = function->NewInstance(argc, argv); 271 v8::Local<v8::Object> result = function->NewInstance(argc, argv);
262 crashIfV8IsDead(); 272 crashIfV8IsDead();
263 return result; 273 return result;
264 } 274 }
265 275
266 } // namespace blink 276 } // namespace blink
OLDNEW
« no previous file with comments | « no previous file | Source/bindings/core/v8/V8ScriptRunnerTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698