OLD | NEW |
---|---|
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 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
131 | 131 |
132 // Compile a script without any caching or compile options. | 132 // Compile a script without any caching or compile options. |
133 v8::MaybeLocal<v8::Script> compileWithoutOptions(V8CompileHistogram::Cacheabilit y cacheability, v8::Isolate* isolate, v8::Local<v8::String> code, v8::ScriptOrig in origin) | 133 v8::MaybeLocal<v8::Script> compileWithoutOptions(V8CompileHistogram::Cacheabilit y cacheability, v8::Isolate* isolate, v8::Local<v8::String> code, v8::ScriptOrig in origin) |
134 { | 134 { |
135 V8CompileHistogram histogramScope(cacheability); | 135 V8CompileHistogram histogramScope(cacheability); |
136 v8::ScriptCompiler::Source source(code, origin); | 136 v8::ScriptCompiler::Source source(code, origin); |
137 return v8::ScriptCompiler::Compile(isolate->GetCurrentContext(), &source, v8 ::ScriptCompiler::kNoCompileOptions); | 137 return v8::ScriptCompiler::Compile(isolate->GetCurrentContext(), &source, v8 ::ScriptCompiler::kNoCompileOptions); |
138 } | 138 } |
139 | 139 |
140 // Compile a script, and consume a V8 cache that was generated previously. | 140 // Compile a script, and consume a V8 cache that was generated previously. |
141 v8::MaybeLocal<v8::Script> compileAndConsumeCache(CachedMetadataHandler* cacheHa ndler, unsigned tag, v8::ScriptCompiler::CompileOptions compileOptions, v8::Isol ate* isolate, v8::Local<v8::String> code, v8::ScriptOrigin origin) | 141 static v8::MaybeLocal<v8::Script> compileAndConsumeCache(CachedMetadataHandler* cacheHandler, PassRefPtr<CachedMetadata> cachedMetadata, v8::ScriptCompiler::Com pileOptions compileOptions, v8::Isolate* isolate, v8::Local<v8::String> code, v8 ::ScriptOrigin origin) |
142 { | 142 { |
143 V8CompileHistogram histogramScope(V8CompileHistogram::Cacheable); | 143 V8CompileHistogram histogramScope(V8CompileHistogram::Cacheable); |
144 CachedMetadata* cachedMetadata = cacheHandler->cachedMetadata(tag); | |
145 const char* data = cachedMetadata->data(); | 144 const char* data = cachedMetadata->data(); |
146 int length = cachedMetadata->size(); | 145 int length = cachedMetadata->size(); |
147 v8::ScriptCompiler::CachedData* cachedData = new v8::ScriptCompiler::CachedD ata( | 146 v8::ScriptCompiler::CachedData* cachedData = new v8::ScriptCompiler::CachedD ata( |
148 reinterpret_cast<const uint8_t*>(data), length, v8::ScriptCompiler::Cach edData::BufferNotOwned); | 147 reinterpret_cast<const uint8_t*>(data), length, v8::ScriptCompiler::Cach edData::BufferNotOwned); |
149 v8::ScriptCompiler::Source source(code, origin, cachedData); | 148 v8::ScriptCompiler::Source source(code, origin, cachedData); |
150 v8::MaybeLocal<v8::Script> script = v8::ScriptCompiler::Compile(isolate->Get CurrentContext(), &source, compileOptions); | 149 v8::MaybeLocal<v8::Script> script = v8::ScriptCompiler::Compile(isolate->Get CurrentContext(), &source, compileOptions); |
151 if (cachedData->rejected) | 150 if (cachedData->rejected) |
152 cacheHandler->clearCachedMetadata(CachedMetadataHandler::SendToPlatform) ; | 151 cacheHandler->clearCachedMetadata(CachedMetadataHandler::SendToPlatform) ; |
153 return script; | 152 return script; |
154 } | 153 } |
(...skipping 17 matching lines...) Expand all Loading... | |
172 cacheHandler->clearCachedMetadata(CachedMetadataHandler::CacheLocally); | 171 cacheHandler->clearCachedMetadata(CachedMetadataHandler::CacheLocally); |
173 cacheHandler->setCachedMetadata(tag, data, length, cacheType); | 172 cacheHandler->setCachedMetadata(tag, data, length, cacheType); |
174 } | 173 } |
175 return script; | 174 return script; |
176 } | 175 } |
177 | 176 |
178 // Compile a script, and consume or produce a V8 Cache, depending on whether the | 177 // Compile a script, and consume or produce a V8 Cache, depending on whether the |
179 // given resource already has cached data available. | 178 // given resource already has cached data available. |
180 v8::MaybeLocal<v8::Script> compileAndConsumeOrProduce(CachedMetadataHandler* cac heHandler, unsigned tag, v8::ScriptCompiler::CompileOptions consumeOptions, v8:: ScriptCompiler::CompileOptions produceOptions, CachedMetadataHandler::CacheType cacheType, v8::Isolate* isolate, v8::Local<v8::String> code, v8::ScriptOrigin or igin) | 179 v8::MaybeLocal<v8::Script> compileAndConsumeOrProduce(CachedMetadataHandler* cac heHandler, unsigned tag, v8::ScriptCompiler::CompileOptions consumeOptions, v8:: ScriptCompiler::CompileOptions produceOptions, CachedMetadataHandler::CacheType cacheType, v8::Isolate* isolate, v8::Local<v8::String> code, v8::ScriptOrigin or igin) |
181 { | 180 { |
182 return cacheHandler->cachedMetadata(tag) | 181 RefPtr<CachedMetadata> codeCache(cacheHandler->cachedMetadata(tag)); |
183 ? compileAndConsumeCache(cacheHandler, tag, consumeOptions, isolate, cod e, origin) | 182 return codeCache.get() |
hiroshige
2016/08/25 12:48:11
We can remove .get() here.
| |
183 ? compileAndConsumeCache(cacheHandler, codeCache, consumeOptions, isolat e, code, origin) | |
184 : compileAndProduceCache(cacheHandler, tag, produceOptions, cacheType, i solate, code, origin); | 184 : compileAndProduceCache(cacheHandler, tag, produceOptions, cacheType, i solate, code, origin); |
185 } | 185 } |
186 | 186 |
187 enum CacheTagKind { | 187 enum CacheTagKind { |
188 CacheTagParser = 0, | 188 CacheTagParser = 0, |
189 CacheTagCode = 1, | 189 CacheTagCode = 1, |
190 CacheTagTimeStamp = 3, | 190 CacheTagTimeStamp = 3, |
191 CacheTagLast | 191 CacheTagLast |
192 }; | 192 }; |
193 | 193 |
(...skipping 11 matching lines...) Expand all Loading... | |
205 // later load the script from the cache and interpret it with a different | 205 // later load the script from the cache and interpret it with a different |
206 // encoding, the cached data is not valid for that encoding. | 206 // encoding, the cached data is not valid for that encoding. |
207 return (v8CacheDataVersion | kind) + StringHash::hash(cacheHandler->encoding ()); | 207 return (v8CacheDataVersion | kind) + StringHash::hash(cacheHandler->encoding ()); |
208 } | 208 } |
209 | 209 |
210 // Check previously stored timestamp. | 210 // Check previously stored timestamp. |
211 bool isResourceHotForCaching(CachedMetadataHandler* cacheHandler, int hotHours) | 211 bool isResourceHotForCaching(CachedMetadataHandler* cacheHandler, int hotHours) |
212 { | 212 { |
213 const double cacheWithinSeconds = hotHours * 60 * 60; | 213 const double cacheWithinSeconds = hotHours * 60 * 60; |
214 unsigned tag = cacheTag(CacheTagTimeStamp, cacheHandler); | 214 unsigned tag = cacheTag(CacheTagTimeStamp, cacheHandler); |
215 CachedMetadata* cachedMetadata = cacheHandler->cachedMetadata(tag); | 215 RefPtr<CachedMetadata> cachedMetadata = cacheHandler->cachedMetadata(tag); |
216 if (!cachedMetadata) | 216 if (!cachedMetadata) |
217 return false; | 217 return false; |
218 double timeStamp; | 218 double timeStamp; |
219 const int size = sizeof(timeStamp); | 219 const int size = sizeof(timeStamp); |
220 ASSERT(cachedMetadata->size() == size); | 220 ASSERT(cachedMetadata->size() == size); |
221 memcpy(&timeStamp, cachedMetadata->data(), size); | 221 memcpy(&timeStamp, cachedMetadata->data(), size); |
222 return (WTF::currentTime() - timeStamp) < cacheWithinSeconds; | 222 return (WTF::currentTime() - timeStamp) < cacheWithinSeconds; |
223 } | 223 } |
224 | 224 |
225 // Final compile call for a streamed compilation. Most decisions have already | 225 // Final compile call for a streamed compilation. Most decisions have already |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
269 // This version isn't quite as smart as the real WTF::bind, though, so you | 269 // This version isn't quite as smart as the real WTF::bind, though, so you |
270 // sometimes may still have to call the original. | 270 // sometimes may still have to call the original. |
271 template<typename... A> | 271 template<typename... A> |
272 std::unique_ptr<CompileFn> bind(const A&... args) | 272 std::unique_ptr<CompileFn> bind(const A&... args) |
273 { | 273 { |
274 return WTF::bind(args...); | 274 return WTF::bind(args...); |
275 } | 275 } |
276 | 276 |
277 // Select a compile function from any of the above, mainly depending on | 277 // Select a compile function from any of the above, mainly depending on |
278 // cacheOptions. | 278 // cacheOptions. |
279 std::unique_ptr<CompileFn> selectCompileFunction(V8CacheOptions cacheOptions, Ca chedMetadataHandler* cacheHandler, v8::Local<v8::String> code, V8CompileHistogra m::Cacheability cacheabilityIfNoHandler) | 279 static std::unique_ptr<CompileFn> selectCompileFunction(V8CacheOptions cacheOpti ons, CachedMetadataHandler* cacheHandler, PassRefPtr<CachedMetadata> codeCache, v8::Local<v8::String> code, V8CompileHistogram::Cacheability cacheabilityIfNoHan dler) |
280 { | 280 { |
281 static const int minimalCodeLength = 1024; | 281 static const int minimalCodeLength = 1024; |
282 static const int hotHours = 72; | 282 static const int hotHours = 72; |
283 | 283 |
284 // Caching is not available in this case. | 284 // Caching is not available in this case. |
285 if (!cacheHandler) | 285 if (!cacheHandler) |
286 return bind(compileWithoutOptions, cacheabilityIfNoHandler); | 286 return bind(compileWithoutOptions, cacheabilityIfNoHandler); |
287 | 287 |
288 if (cacheOptions == V8CacheOptionsNone) | 288 if (cacheOptions == V8CacheOptionsNone) |
289 return bind(compileWithoutOptions, V8CompileHistogram::Cacheable); | 289 return bind(compileWithoutOptions, V8CompileHistogram::Cacheable); |
290 | 290 |
291 // Caching is not worthwhile for small scripts. Do not use caching | 291 // Caching is not worthwhile for small scripts. Do not use caching |
292 // unless explicitly expected, indicated by the cache option. | 292 // unless explicitly expected, indicated by the cache option. |
293 if (code->Length() < minimalCodeLength) | 293 if (code->Length() < minimalCodeLength) |
294 return bind(compileWithoutOptions, V8CompileHistogram::Cacheable); | 294 return bind(compileWithoutOptions, V8CompileHistogram::Cacheable); |
295 | 295 |
296 // The cacheOptions will guide our strategy: | 296 // The cacheOptions will guide our strategy: |
297 switch (cacheOptions) { | 297 switch (cacheOptions) { |
298 case V8CacheOptionsParse: | 298 case V8CacheOptionsParse: |
299 // Use parser-cache; in-memory only. | 299 // Use parser-cache; in-memory only. |
300 return bind(compileAndConsumeOrProduce, wrapPersistent(cacheHandler), ca cheTag(CacheTagParser, cacheHandler), v8::ScriptCompiler::kConsumeParserCache, v 8::ScriptCompiler::kProduceParserCache, CachedMetadataHandler::CacheLocally); | 300 return bind(compileAndConsumeOrProduce, wrapPersistent(cacheHandler), ca cheTag(CacheTagParser, cacheHandler), v8::ScriptCompiler::kConsumeParserCache, v 8::ScriptCompiler::kProduceParserCache, CachedMetadataHandler::CacheLocally); |
301 break; | 301 break; |
302 | 302 |
303 case V8CacheOptionsDefault: | 303 case V8CacheOptionsDefault: |
304 case V8CacheOptionsCode: | 304 case V8CacheOptionsCode: |
305 case V8CacheOptionsAlways: { | 305 case V8CacheOptionsAlways: { |
306 // Use code caching for recently seen resources. | 306 // Use code caching for recently seen resources. |
307 // Use compression depending on the cache option. | 307 // Use compression depending on the cache option. |
308 unsigned codeCacheTag = cacheTag(CacheTagCode, cacheHandler); | |
309 CachedMetadata* codeCache = cacheHandler->cachedMetadata(codeCacheTag); | |
310 if (codeCache) | 308 if (codeCache) |
311 return bind(compileAndConsumeCache, wrapPersistent(cacheHandler), co deCacheTag, v8::ScriptCompiler::kConsumeCodeCache); | 309 return bind(compileAndConsumeCache, wrapPersistent(cacheHandler), co deCache, v8::ScriptCompiler::kConsumeCodeCache); |
312 if (cacheOptions != V8CacheOptionsAlways && !isResourceHotForCaching(cac heHandler, hotHours)) { | 310 if (cacheOptions != V8CacheOptionsAlways && !isResourceHotForCaching(cac heHandler, hotHours)) { |
313 V8ScriptRunner::setCacheTimeStamp(cacheHandler); | 311 V8ScriptRunner::setCacheTimeStamp(cacheHandler); |
314 return bind(compileWithoutOptions, V8CompileHistogram::Cacheable); | 312 return bind(compileWithoutOptions, V8CompileHistogram::Cacheable); |
315 } | 313 } |
314 unsigned codeCacheTag = cacheTag(CacheTagCode, cacheHandler); | |
316 return bind(compileAndProduceCache, wrapPersistent(cacheHandler), codeCa cheTag, v8::ScriptCompiler::kProduceCodeCache, CachedMetadataHandler::SendToPlat form); | 315 return bind(compileAndProduceCache, wrapPersistent(cacheHandler), codeCa cheTag, v8::ScriptCompiler::kProduceCodeCache, CachedMetadataHandler::SendToPlat form); |
317 break; | 316 break; |
318 } | 317 } |
319 | 318 |
320 case V8CacheOptionsNone: | 319 case V8CacheOptionsNone: |
321 // Shouldn't happen, as this is handled above. | 320 // Shouldn't happen, as this is handled above. |
322 // Case is here so that compiler can check all cases are handled. | 321 // Case is here so that compiler can check all cases are handled. |
323 ASSERT_NOT_REACHED(); | 322 ASSERT_NOT_REACHED(); |
324 break; | 323 break; |
325 } | 324 } |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
378 v8Boolean(accessControlStatus == SharableCrossOrigin, isolate), | 377 v8Boolean(accessControlStatus == SharableCrossOrigin, isolate), |
379 v8::Local<v8::Integer>(), | 378 v8::Local<v8::Integer>(), |
380 v8Boolean(false, isolate), | 379 v8Boolean(false, isolate), |
381 v8String(isolate, sourceMapUrl), | 380 v8String(isolate, sourceMapUrl), |
382 v8Boolean(accessControlStatus == OpaqueResource, isolate)); | 381 v8Boolean(accessControlStatus == OpaqueResource, isolate)); |
383 | 382 |
384 V8CompileHistogram::Cacheability cacheabilityIfNoHandler = V8CompileHistogra m::Cacheability::Noncacheable; | 383 V8CompileHistogram::Cacheability cacheabilityIfNoHandler = V8CompileHistogra m::Cacheability::Noncacheable; |
385 if (!cacheHandler && (scriptStartPosition.m_line.zeroBasedInt() == 0) && (sc riptStartPosition.m_column.zeroBasedInt() == 0)) | 384 if (!cacheHandler && (scriptStartPosition.m_line.zeroBasedInt() == 0) && (sc riptStartPosition.m_column.zeroBasedInt() == 0)) |
386 cacheabilityIfNoHandler = V8CompileHistogram::Cacheability::InlineScript ; | 385 cacheabilityIfNoHandler = V8CompileHistogram::Cacheability::InlineScript ; |
387 | 386 |
387 RefPtr<CachedMetadata> codeCache(cacheHandler ? cacheHandler->cachedMetadata (cacheTag(CacheTagCode, cacheHandler)) : nullptr); | |
388 std::unique_ptr<CompileFn> compileFn = streamer | 388 std::unique_ptr<CompileFn> compileFn = streamer |
389 ? selectCompileFunction(cacheOptions, resource, streamer) | 389 ? selectCompileFunction(cacheOptions, resource, streamer) |
390 : selectCompileFunction(cacheOptions, cacheHandler, code, cacheabilityIf NoHandler); | 390 : selectCompileFunction(cacheOptions, cacheHandler, codeCache, code, cac heabilityIfNoHandler); |
391 | 391 |
392 return (*compileFn)(isolate, code, origin); | 392 return (*compileFn)(isolate, code, origin); |
393 } | 393 } |
394 | 394 |
395 v8::MaybeLocal<v8::Value> V8ScriptRunner::runCompiledScript(v8::Isolate* isolate , v8::Local<v8::Script> script, ExecutionContext* context) | 395 v8::MaybeLocal<v8::Value> V8ScriptRunner::runCompiledScript(v8::Isolate* isolate , v8::Local<v8::Script> script, ExecutionContext* context) |
396 { | 396 { |
397 ASSERT(!script.IsEmpty()); | 397 ASSERT(!script.IsEmpty()); |
398 ScopedFrameBlamer frameBlamer(context->isDocument() ? toDocument(context)->f rame() : nullptr); | 398 ScopedFrameBlamer frameBlamer(context->isDocument() ? toDocument(context)->f rame() : nullptr); |
399 TRACE_EVENT_SCOPED_SAMPLING_STATE("v8", "V8Execution"); | 399 TRACE_EVENT_SCOPED_SAMPLING_STATE("v8", "V8Execution"); |
400 TRACE_EVENT1("v8", "v8.run", "fileName", TRACE_STR_COPY(*v8::String::Utf8Val ue(script->GetUnboundScript()->GetScriptName()))); | 400 TRACE_EVENT1("v8", "v8.run", "fileName", TRACE_STR_COPY(*v8::String::Utf8Val ue(script->GetUnboundScript()->GetScriptName()))); |
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
604 V8CompileHistogram::Cacheability::Noncacheable, isolate, | 604 V8CompileHistogram::Cacheability::Noncacheable, isolate, |
605 v8AtomicString(isolate, "((e) => { throw e; })"), origin) | 605 v8AtomicString(isolate, "((e) => { throw e; })"), origin) |
606 .ToLocalChecked(); | 606 .ToLocalChecked(); |
607 v8::Local<v8::Function> thrower = runCompiledInternalScript(isolate, script) | 607 v8::Local<v8::Function> thrower = runCompiledInternalScript(isolate, script) |
608 .ToLocalChecked().As<v8::Function>(); | 608 .ToLocalChecked().As<v8::Function>(); |
609 v8::Local<v8::Value> args[] = { exception }; | 609 v8::Local<v8::Value> args[] = { exception }; |
610 callInternalFunction(thrower, thrower, WTF_ARRAY_LENGTH(args), args, isolate ); | 610 callInternalFunction(thrower, thrower, WTF_ARRAY_LENGTH(args), args, isolate ); |
611 } | 611 } |
612 | 612 |
613 } // namespace blink | 613 } // namespace blink |
OLD | NEW |