Bug 1662273 Comment 1 Edit History

Note: The actual edited comment in the bug view page will always show the original commenter’s name and original timestamp.

Bug 1660940 will add:
  * same-thread compilation to stencil (`JS::CompileToStencil` and `JS::CompileToStencilForNonSyntacticScope`)
  * same-thread stencil instantiation (`JS::InstantiateScript`)

In this case `CompilationStencil` is passed back to the API consumer.

   

And bug 1658631 added support for:
  * off-thread compilation to stencil
  * stencil instantiation on main-thread after off-thread compilation to stencil

In this case `CompilationStencil` is hidden from the API consumer
(started by `JS::CompileOffThread`, `JS::CompileOffThreadModule`, finished by `JS::FinishOffThreadScript` and `JS::FinishOffThreadModule`).

   

To support stencil XDR in public API used by ScriptLoader, missing parts are:
  * a) XDR encode after same-thread compilation
  * b) XDR encode after off-thread compilation
  * c) same-thread XDR decode
  * d) off-thread XDR decode

   

(a) happens in `nsJSUtils::ExecutionContext::InternalCompile`, after calling `JS::Compile` and `JS::CompileForNonSyntacticScope`.

https://searchfox.org/mozilla-central/rev/b2716c233e9b4398fc5923cbe150e7f83c7c6c5b/dom/base/nsJSUtils.cpp#228
```
  if (mEncodeBytecode && !StartIncrementalEncoding(mCx, mScript)) {
```

and collected by `ScriptLoader::EncodeRequestBytecode`

https://searchfox.org/mozilla-central/rev/b2716c233e9b4398fc5923cbe150e7f83c7c6c5b/dom/script/ScriptLoader.cpp#3135
```
  if (!JS::FinishIncrementalEncoding(aCx, script, aRequest->mScriptBytecode)) {
```

In this case, this can use:
  1. `CompileToStencil`
  2. "XDR encode stencil" (this will be same-thread encode for now)
  3. `InstantiateScript`

inside `nsJSUtils::ExecutionContext::InternalCompile`, and not using off-thread encoding.

   

(b) happens in `nsJSUtils::ExecutionContext::JoinCompile` after calling `JS::FinishOffThreadScript`.

https://searchfox.org/mozilla-central/rev/b2716c233e9b4398fc5923cbe150e7f83c7c6c5b/dom/base/nsJSUtils.cpp#195
```
  if (mEncodeBytecode && !StartIncrementalEncoding(mCx, mScript)) {
```

and also collected by `ScriptLoader::EncodeRequestBytecode`.

In this case, `CompilationStencil` is hidden in JSAPI internal, so `JS::FinishOffThreadScript` needs refactoring.

something like:
  1. Pass the result of off-thread compilation (`CompilationStencil`) to JSAPI consumer
  2. "XDR encode stencil"
  3. `JS::InstantiateScript`

or add single API for those 3 steps.

This case also uses same-thread encode for now.

   

(c) happens in `nsJSUtils::ExecutionContext::Decode`

https://searchfox.org/mozilla-central/rev/b2716c233e9b4398fc5923cbe150e7f83c7c6c5b/dom/base/nsJSUtils.cpp#273-274
```
  JS::TranscodeResult tr =
      JS::DecodeScript(mCx, aBytecodeBuf, &mScript, aBytecodeIndex);
```

This can be:
  * "XDR decode stencil"
  * `JS::InstantiateScript`

or single API with those 2 steps.

   

(d) happens in `ScriptLoader::AttemptAsyncScriptCompile`

https://searchfox.org/mozilla-central/rev/b2716c233e9b4398fc5923cbe150e7f83c7c6c5b/dom/script/ScriptLoader.cpp#2322
```
    if (!JS::DecodeOffThreadScript(
            cx, options, aRequest->mScriptBytecode, aRequest->mBytecodeOffset,
            OffThreadScriptLoaderCallback, static_cast<void*>(runnable))) {
```

and collected by `nsJSUtils::ExecutionContext::JoinDecode`

https://searchfox.org/mozilla-central/rev/b2716c233e9b4398fc5923cbe150e7f83c7c6c5b/dom/base/nsJSUtils.cpp#297
```
  mScript.set(JS::FinishOffThreadScriptDecoder(mCx, *aOffThreadToken));
```

This case, `CompilationStencil` is hidden behind JSAPI.
helper thread just needs to support "XDR decode stencil",
and `frontend::InstantiateStencil` when finishing the task,
just like compilation (parse) task.
~~Bug 1660940 will add~~:
  * ~~same-thread compilation to stencil (`JS::CompileToStencil` and `JS::CompileToStencilForNonSyntacticScope`)~~
  * ~~same-thread stencil instantiation (`JS::InstantiateScript`)~~

~~In this case `CompilationStencil` is passed back to the API consumer.~~

&nbsp;  

And bug 1658631 added support for:
  * off-thread compilation to stencil
  * stencil instantiation on main-thread after off-thread compilation to stencil

In this case `CompilationStencil` is hidden from the API consumer
(started by `JS::CompileOffThread`, `JS::CompileOffThreadModule`, finished by `JS::FinishOffThreadScript` and `JS::FinishOffThreadModule`).

&nbsp;  

To support stencil XDR in public API used by ScriptLoader, missing parts are:
  * a) XDR encode after same-thread compilation
  * b) XDR encode after off-thread compilation
  * c) same-thread XDR decode
  * d) off-thread XDR decode

&nbsp;  

(a) happens in `nsJSUtils::ExecutionContext::InternalCompile`, after calling `JS::Compile` and `JS::CompileForNonSyntacticScope`.

https://searchfox.org/mozilla-central/rev/b2716c233e9b4398fc5923cbe150e7f83c7c6c5b/dom/base/nsJSUtils.cpp#228
```
  if (mEncodeBytecode && !StartIncrementalEncoding(mCx, mScript)) {
```

and collected by `ScriptLoader::EncodeRequestBytecode`

https://searchfox.org/mozilla-central/rev/b2716c233e9b4398fc5923cbe150e7f83c7c6c5b/dom/script/ScriptLoader.cpp#3135
```
  if (!JS::FinishIncrementalEncoding(aCx, script, aRequest->mScriptBytecode)) {
```

In this case, this can use:
  1. `CompileToStencil`
  2. "XDR encode stencil" (this will be same-thread encode for now)
  3. `InstantiateScript`

inside `nsJSUtils::ExecutionContext::InternalCompile`, and not using off-thread encoding.

&nbsp;  

(b) happens in `nsJSUtils::ExecutionContext::JoinCompile` after calling `JS::FinishOffThreadScript`.

https://searchfox.org/mozilla-central/rev/b2716c233e9b4398fc5923cbe150e7f83c7c6c5b/dom/base/nsJSUtils.cpp#195
```
  if (mEncodeBytecode && !StartIncrementalEncoding(mCx, mScript)) {
```

and also collected by `ScriptLoader::EncodeRequestBytecode`.

In this case, `CompilationStencil` is hidden in JSAPI internal, so `JS::FinishOffThreadScript` needs refactoring.

something like:
  1. Pass the result of off-thread compilation (`CompilationStencil`) to JSAPI consumer
  2. "XDR encode stencil"
  3. `JS::InstantiateScript`

or add single API for those 3 steps.

This case also uses same-thread encode for now.

&nbsp;  

(c) happens in `nsJSUtils::ExecutionContext::Decode`

https://searchfox.org/mozilla-central/rev/b2716c233e9b4398fc5923cbe150e7f83c7c6c5b/dom/base/nsJSUtils.cpp#273-274
```
  JS::TranscodeResult tr =
      JS::DecodeScript(mCx, aBytecodeBuf, &mScript, aBytecodeIndex);
```

This can be:
  * "XDR decode stencil"
  * `JS::InstantiateScript`

or single API with those 2 steps.

&nbsp;  

(d) happens in `ScriptLoader::AttemptAsyncScriptCompile`

https://searchfox.org/mozilla-central/rev/b2716c233e9b4398fc5923cbe150e7f83c7c6c5b/dom/script/ScriptLoader.cpp#2322
```
    if (!JS::DecodeOffThreadScript(
            cx, options, aRequest->mScriptBytecode, aRequest->mBytecodeOffset,
            OffThreadScriptLoaderCallback, static_cast<void*>(runnable))) {
```

and collected by `nsJSUtils::ExecutionContext::JoinDecode`

https://searchfox.org/mozilla-central/rev/b2716c233e9b4398fc5923cbe150e7f83c7c6c5b/dom/base/nsJSUtils.cpp#297
```
  mScript.set(JS::FinishOffThreadScriptDecoder(mCx, *aOffThreadToken));
```

This case, `CompilationStencil` is hidden behind JSAPI.
helper thread just needs to support "XDR decode stencil",
and `frontend::InstantiateStencil` when finishing the task,
just like compilation (parse) task.

Back to Bug 1662273 Comment 1