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

Unified Diff: Source/bindings/core/v8/V8ScriptRunner.cpp

Issue 368283002: Stream scripts to V8 as they load - Blink side. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: renaming Created 6 years, 4 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 side-by-side diff with in-line comments
Download patch
Index: Source/bindings/core/v8/V8ScriptRunner.cpp
diff --git a/Source/bindings/core/v8/V8ScriptRunner.cpp b/Source/bindings/core/v8/V8ScriptRunner.cpp
index 5c0f1d1765b34b9aff1fb147ec5be179092db8ea..b61c8c98db58bd5b229f0cf327ebfa7d7dfbfb95 100644
--- a/Source/bindings/core/v8/V8ScriptRunner.cpp
+++ b/Source/bindings/core/v8/V8ScriptRunner.cpp
@@ -30,6 +30,7 @@
#include "bindings/core/v8/V8Binding.h"
#include "bindings/core/v8/V8GCController.h"
#include "bindings/core/v8/V8RecursionScope.h"
+#include "bindings/core/v8/V8ScriptStreamer.h"
#include "bindings/core/v8/V8ThrowException.h"
#include "core/dom/ExecutionContext.h"
#include "core/fetch/CachedMetadata.h"
@@ -62,49 +63,14 @@ v8::Local<v8::Value> throwStackOverflowExceptionIfNeeded(v8::Isolate* isolate)
return result;
}
-// Make a decision on whether we want to use V8 caching and how.
-// dataType, produceOption, consumeOption are out parameters.
-bool CacheDecider(
- const v8::Handle<v8::String> code,
- const ScriptResource* resource,
- V8CacheOptions cacheOptions,
- unsigned* dataType,
- v8::ScriptCompiler::CompileOptions* compileOption,
- bool* produce)
-{
- if (!resource || !resource->url().protocolIsInHTTPFamily() || code->Length() < 1024)
- cacheOptions = V8CacheOptionsOff;
-
- bool useCache = false;
- switch (cacheOptions) {
- case V8CacheOptionsOff:
- *compileOption = v8::ScriptCompiler::kNoCompileOptions;
- useCache = false;
- break;
- case V8CacheOptionsParse:
- *dataType = StringHash::hash(v8::V8::GetVersion()) * 2;
- *produce = !resource->cachedMetadata(*dataType);
- *compileOption = *produce ? v8::ScriptCompiler::kProduceParserCache : v8::ScriptCompiler::kConsumeParserCache;
- useCache = true;
- break;
- case V8CacheOptionsCode:
- *dataType = StringHash::hash(v8::V8::GetVersion()) * 2 + 1;
- *produce = !resource->cachedMetadata(*dataType);
- *compileOption = *produce ? v8::ScriptCompiler::kProduceCodeCache : v8::ScriptCompiler::kConsumeCodeCache;
- useCache = true;
- break;
- }
- return useCache;
-}
-
} // namespace
v8::Local<v8::Script> V8ScriptRunner::compileScript(const ScriptSourceCode& source, v8::Isolate* isolate, AccessControlStatus corsStatus, V8CacheOptions cacheOptions)
{
- return compileScript(v8String(isolate, source.source()), source.url(), source.startPosition(), source.resource(), isolate, corsStatus, cacheOptions);
+ return compileScript(v8String(isolate, source.source()), source.url(), source.startPosition(), source.resource(), source.streamer(), isolate, corsStatus, cacheOptions);
}
-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)
+v8::Local<v8::Script> V8ScriptRunner::compileScript(v8::Handle<v8::String> code, const String& fileName, const TextPosition& scriptStartPosition, ScriptResource* resource, V8ScriptStreamer* streamer, v8::Isolate* isolate, AccessControlStatus corsStatus, V8CacheOptions cacheOptions)
{
TRACE_EVENT1("v8", "v8.compile", "fileName", fileName.utf8());
TRACE_EVENT_SCOPED_SAMPLING_STATE("v8", "V8Compile");
@@ -123,7 +89,19 @@ v8::Local<v8::Script> V8ScriptRunner::compileScript(v8::Handle<v8::String> code,
v8::ScriptCompiler::CompileOptions compileOption = v8::ScriptCompiler::kNoCompileOptions;
bool produce;
v8::Local<v8::Script> script;
- if (CacheDecider(code, resource, cacheOptions, &dataTypeID, &compileOption, &produce)) {
+ if (streamer) {
+ // We don't stream scripts which don't have a Resource.
+ ASSERT(resource);
+ // Failed resources should never get this far.
+ ASSERT(!resource->errorOccurred());
+ script = v8::ScriptCompiler::Compile(isolate, streamer->source(), code, origin);
+ // Whether to produce the cached data or not is decided when the
+ // streamer is started. Here we only need to get the data out.
+ const v8::ScriptCompiler::CachedData* newCachedData = streamer->source()->GetCachedData();
+ if (newCachedData) {
+ resource->setCachedMetadata(streamer->cachedDataDataType(), reinterpret_cast<const char*>(newCachedData->data), newCachedData->length);
haraken 2014/08/17 16:05:25 Don't you need to call resource->clearCachedMetada
marja 2014/08/20 11:45:54 Done.
+ }
+ } else if (CacheDecider(code->Length(), resource, cacheOptions, &dataTypeID, &compileOption, &produce)) {
if (produce) {
// Produce new cache data:
v8::ScriptCompiler::Source source(code, origin);
@@ -185,7 +163,7 @@ v8::Local<v8::Value> V8ScriptRunner::compileAndRunInternalScript(v8::Handle<v8::
{
TRACE_EVENT0("v8", "v8.run");
TRACE_EVENT_SCOPED_SAMPLING_STATE("v8", "V8Execution");
- v8::Handle<v8::Script> script = V8ScriptRunner::compileScript(source, fileName, scriptStartPosition, 0, isolate);
+ v8::Handle<v8::Script> script = V8ScriptRunner::compileScript(source, fileName, scriptStartPosition, 0, 0, isolate);
if (script.IsEmpty())
return v8::Local<v8::Value>();
@@ -264,4 +242,39 @@ v8::Local<v8::Object> V8ScriptRunner::instantiateObjectInDocument(v8::Isolate* i
return result;
}
+bool V8ScriptRunner::CacheDecider(
+ unsigned codeLength,
+ const ScriptResource* resource,
+ V8CacheOptions cacheOptions,
+ unsigned* dataType,
+ v8::ScriptCompiler::CompileOptions* compileOption,
+ bool* produce)
+{
+ // Code length 0 means that we don't know it yet; don't make decisions based on it.
+ if (!resource || !resource->url().protocolIsInHTTPFamily() || (codeLength > 0 && codeLength < 1024))
+ cacheOptions = V8CacheOptionsOff;
+
+ bool useCache = false;
+ switch (cacheOptions) {
+ case V8CacheOptionsOff:
+ *compileOption = v8::ScriptCompiler::kNoCompileOptions;
+ useCache = false;
+ break;
+ case V8CacheOptionsParse:
+ *dataType = StringHash::hash(v8::V8::GetVersion()) * 2;
+ *produce = !resource->cachedMetadata(*dataType);
+ *compileOption = *produce ? v8::ScriptCompiler::kProduceParserCache : v8::ScriptCompiler::kConsumeParserCache;
+ useCache = true;
+ break;
+ case V8CacheOptionsCode:
+ *dataType = StringHash::hash(v8::V8::GetVersion()) * 2 + 1;
+ *produce = !resource->cachedMetadata(*dataType);
+ *compileOption = *produce ? v8::ScriptCompiler::kProduceCodeCache : v8::ScriptCompiler::kConsumeCodeCache;
+ useCache = true;
+ break;
+ }
+ return useCache;
+}
+
+
} // namespace blink

Powered by Google App Engine
This is Rietveld 408576698