Bug 1552979 - Templatize {Script,Module}ParseTask by Unit as a step toward enabling off-thread parsing of UTF-8 source. r=arai
authorJeff Walden <jwalden@mit.edu>
Sat, 18 May 2019 17:09:16 -0700
changeset 475402 38beafecef9b5074a7bc9dcacfc7a89ef919ad80
parent 475401 83e89748aaac69c8ca94ef64847d9fd1640cbd63
child 475403 41adfcfb70bf8e12c98eb15d11185c97dc6afabd
push id36061
push usercbrindusan@mozilla.com
push dateFri, 24 May 2019 21:49:59 +0000
treeherdermozilla-central@5d3e1ea77693 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersarai
bugs1552979
milestone69.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1552979 - Templatize {Script,Module}ParseTask by Unit as a step toward enabling off-thread parsing of UTF-8 source. r=arai Differential Revision: https://phabricator.services.mozilla.com/D31889
js/src/vm/HelperThreads.cpp
js/src/vm/HelperThreads.h
--- a/js/src/vm/HelperThreads.cpp
+++ b/js/src/vm/HelperThreads.cpp
@@ -4,16 +4,17 @@
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "vm/HelperThreads.h"
 
 #include "mozilla/Maybe.h"
 #include "mozilla/ScopeExit.h"
 #include "mozilla/Unused.h"
+#include "mozilla/Utf8.h"  // mozilla::Utf8Unit
 
 #include "builtin/Promise.h"
 #include "frontend/BytecodeCompilation.h"
 #include "gc/GCInternals.h"
 #include "jit/IonBuilder.h"
 #include "js/ContextOptions.h"  // JS::ContextOptions
 #include "js/SourceText.h"
 #include "js/UniquePtr.h"
@@ -36,16 +37,17 @@
 #include "vm/Realm-inl.h"
 
 using namespace js;
 
 using mozilla::Maybe;
 using mozilla::TimeDuration;
 using mozilla::TimeStamp;
 using mozilla::Unused;
+using mozilla::Utf8Unit;
 
 using JS::CompileOptions;
 using JS::ReadOnlyCompileOptions;
 
 namespace js {
 
 GlobalHelperThreadState* gHelperThreadState = nullptr;
 
@@ -503,24 +505,35 @@ void ParseTask::runTask() {
   parse(cx);
 
   MOZ_ASSERT(cx->tempLifoAlloc().isEmpty());
   cx->tempLifoAlloc().freeAll();
   cx->frontendCollectionPool().purge();
   cx->atomsZoneFreeLists().clear();
 }
 
-ScriptParseTask::ScriptParseTask(JSContext* cx,
-                                 JS::SourceText<char16_t>& srcBuf,
-                                 JS::OffThreadCompileCallback callback,
-                                 void* callbackData)
+template <typename Unit>
+struct ScriptParseTask : public ParseTask {
+  JS::SourceText<Unit> data;
+
+  ScriptParseTask(JSContext* cx, JS::SourceText<Unit>& srcBuf,
+                  JS::OffThreadCompileCallback callback, void* callbackData);
+  void parse(JSContext* cx) override;
+};
+
+template <typename Unit>
+ScriptParseTask<Unit>::ScriptParseTask(JSContext* cx,
+                                       JS::SourceText<Unit>& srcBuf,
+                                       JS::OffThreadCompileCallback callback,
+                                       void* callbackData)
     : ParseTask(ParseTaskKind::Script, cx, callback, callbackData),
       data(std::move(srcBuf)) {}
 
-void ScriptParseTask::parse(JSContext* cx) {
+template <typename Unit>
+void ScriptParseTask<Unit>::parse(JSContext* cx) {
   MOZ_ASSERT(cx->helperThread());
 
   JSScript* script;
   Rooted<ScriptSourceObject*> sourceObject(cx);
 
   {
     ScopeKind scopeKind =
         options.nonSyntacticScope ? ScopeKind::NonSyntactic : ScopeKind::Global;
@@ -538,24 +551,35 @@ void ScriptParseTask::parse(JSContext* c
   // we must finish initializing the SSO.  This is because there may be valid
   // inner scripts observable by the debugger which reference the partially-
   // initialized SSO.
   if (sourceObject) {
     sourceObjects.infallibleAppend(sourceObject);
   }
 }
 
-ModuleParseTask::ModuleParseTask(JSContext* cx,
-                                 JS::SourceText<char16_t>& srcBuf,
-                                 JS::OffThreadCompileCallback callback,
-                                 void* callbackData)
+template <typename Unit>
+struct ModuleParseTask : public ParseTask {
+  JS::SourceText<Unit> data;
+
+  ModuleParseTask(JSContext* cx, JS::SourceText<Unit>& srcBuf,
+                  JS::OffThreadCompileCallback callback, void* callbackData);
+  void parse(JSContext* cx) override;
+};
+
+template <typename Unit>
+ModuleParseTask<Unit>::ModuleParseTask(JSContext* cx,
+                                       JS::SourceText<Unit>& srcBuf,
+                                       JS::OffThreadCompileCallback callback,
+                                       void* callbackData)
     : ParseTask(ParseTaskKind::Module, cx, callback, callbackData),
       data(std::move(srcBuf)) {}
 
-void ModuleParseTask::parse(JSContext* cx) {
+template <typename Unit>
+void ModuleParseTask<Unit>::parse(JSContext* cx) {
   MOZ_ASSERT(cx->helperThread());
 
   Rooted<ScriptSourceObject*> sourceObject(cx);
 
   ModuleObject* module =
       frontend::CompileModule(cx, options, data, &sourceObject.get());
   if (module) {
     scripts.infallibleAppend(module->script());
@@ -872,33 +896,33 @@ bool StartOffThreadParseTask(JSContext* 
   return true;
 }
 
 bool js::StartOffThreadParseScript(JSContext* cx,
                                    const ReadOnlyCompileOptions& options,
                                    JS::SourceText<char16_t>& srcBuf,
                                    JS::OffThreadCompileCallback callback,
                                    void* callbackData) {
-  auto task =
-      cx->make_unique<ScriptParseTask>(cx, srcBuf, callback, callbackData);
+  auto task = cx->make_unique<ScriptParseTask<char16_t>>(cx, srcBuf, callback,
+                                                         callbackData);
   if (!task || !StartOffThreadParseTask(cx, task.get(), options)) {
     return false;
   }
 
   Unused << task.release();
   return true;
 }
 
 bool js::StartOffThreadParseModule(JSContext* cx,
                                    const ReadOnlyCompileOptions& options,
                                    JS::SourceText<char16_t>& srcBuf,
                                    JS::OffThreadCompileCallback callback,
                                    void* callbackData) {
-  auto task =
-      cx->make_unique<ModuleParseTask>(cx, srcBuf, callback, callbackData);
+  auto task = cx->make_unique<ModuleParseTask<char16_t>>(cx, srcBuf, callback,
+                                                         callbackData);
   if (!task || !StartOffThreadParseTask(cx, task.get(), options)) {
     return false;
   }
 
   Unused << task.release();
   return true;
 }
 
--- a/js/src/vm/HelperThreads.h
+++ b/js/src/vm/HelperThreads.h
@@ -738,32 +738,16 @@ struct ParseTask : public mozilla::Linke
   size_t sizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) const;
   size_t sizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf) const {
     return mallocSizeOf(this) + sizeOfExcludingThis(mallocSizeOf);
   }
 
   void runTask() override;
 };
 
-struct ScriptParseTask : public ParseTask {
-  JS::SourceText<char16_t> data;
-
-  ScriptParseTask(JSContext* cx, JS::SourceText<char16_t>& srcBuf,
-                  JS::OffThreadCompileCallback callback, void* callbackData);
-  void parse(JSContext* cx) override;
-};
-
-struct ModuleParseTask : public ParseTask {
-  JS::SourceText<char16_t> data;
-
-  ModuleParseTask(JSContext* cx, JS::SourceText<char16_t>& srcBuf,
-                  JS::OffThreadCompileCallback callback, void* callbackData);
-  void parse(JSContext* cx) override;
-};
-
 struct ScriptDecodeTask : public ParseTask {
   const JS::TranscodeRange range;
 
   ScriptDecodeTask(JSContext* cx, const JS::TranscodeRange& range,
                    JS::OffThreadCompileCallback callback, void* callbackData);
   void parse(JSContext* cx) override;
 };