Bug 1672493 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.

:edenchuang and :jesup and I just had a quick talk on this and another potential means of taking the main thread out of latency was identified.

### PRemoteWorker direct to Worker thread for non-lifecycle operations
- Currently PRemoteWorker is used to connect the RemoteWorkerManager in the IPDL Background thread to the RemoteWorkerService in the Worker Launcher thread on a content process.  As operations are received in the Worker Launcher thread, they are dispatched by way of the main thread to the worker thread proper.
  - This provides a homogenous data path for the lifecycle operations of spawning the worker and terminating the worker as well as the non-lifecyle operations like "fetch" events, "message" events, etc.
- Instead, the PRemoteWorker connections could be established for non-lifecycle events directly to the worker, allowing "fetch" and "message" events to be dispatched directly to the worker.
- This would mean that lifecycle events would need to be split into their own protocol.  The existing PRemoteWorkerService could be used for this purpose.  This would cover both spawning and termination.

The main logistical complication is that non-lifecycle operations could be directly sent over PRemoteWorker as soon as it was constructed as part of the lifecycle action of spawning the ServiceWorker.  But under this new split-protocol scheme, the operations would not be able to be sent until PRemoteWorker has registered itself with the parent process (and this would potentially require a new rendezvous via identifier mechanism unless IPC enhancements related to endpoints allow the worker spawning to pass an endpoint down over PRemoteWorkerService to be used to provide a pre-named/pre-allocated RemoteWorkerParent to be established from a different thread in the content process and thereby via a different PBackground parent/child pair).

#### Benefits
- This would allow us to side-step bug 1672491 as a blocking dependency.  Until bug 1672491 is fixed, there would of course potentially be some amount of main-thread contention experienced when spawning the ServiceWorker, but the steady state operation would not involve the main thread.
- Thread hopping elimination:
  - Relative to the current state of the tree: this eliminates the hop from the worker launcher thread to the main thread and then to the worker.
  - Relative to the proposal from comment 0: this eliminates the hop from the worker launcher thread to the worker.  This is less a performance win, as the worker launcher thread should never be contended, but would represent a meaningful simplification in implementation and conceptual complexity as all non-lifecycle events would only be dealing with a single thread.
:edenchuang and :jesup and I just had a quick talk on this and another potential means of taking the main thread out of latency was identified.

### PRemoteWorker direct to Worker thread for non-lifecycle operations
- Currently PRemoteWorker is used to connect the RemoteWorkerManager in the IPDL Background thread to the RemoteWorkerService in the Worker Launcher thread on a content process.  As operations are received in the Worker Launcher thread, they are dispatched by way of the main thread to the worker thread proper.
  - This provides a homogenous data path for the lifecycle operations of spawning the worker and terminating the worker as well as the non-lifecyle operations like "fetch" events, "message" events, etc.
- Instead, the PRemoteWorker connections could be established for non-lifecycle events directly to the worker, allowing "fetch" and "message" events to be dispatched directly to the worker.
- This would mean that lifecycle events would need to be split into their own protocol.  The existing PRemoteWorkerService could be used for this purpose.  This would cover both spawning and termination.
  - Arguably there are advantages to having an actor whose lifecycle corresponds to the length of the worker (versus passing identifiers back and forth).  So it might make sense to create a PRemoteWorkerParent which could be managed by PRemoteWorkerService for additional clarity.  (I'm not 100% clear on why PRemoteWorker wasn't already managed by PRemoteWorkerService at this moment.)

The main logistical complication is that non-lifecycle operations could be directly sent over PRemoteWorker as soon as it was constructed as part of the lifecycle action of spawning the ServiceWorker.  But under this new split-protocol scheme, the operations would not be able to be sent until PRemoteWorker has registered itself with the parent process (and this would potentially require a new rendezvous via identifier mechanism unless IPC enhancements related to endpoints allow the worker spawning to pass an endpoint down over PRemoteWorkerService to be used to provide a pre-named/pre-allocated RemoteWorkerParent to be established from a different thread in the content process and thereby via a different PBackground parent/child pair).

#### Benefits
- This would allow us to side-step bug 1672491 as a blocking dependency.  Until bug 1672491 is fixed, there would of course potentially be some amount of main-thread contention experienced when spawning the ServiceWorker, but the steady state operation would not involve the main thread.
- Thread hopping elimination:
  - Relative to the current state of the tree: this eliminates the hop from the worker launcher thread to the main thread and then to the worker.
  - Relative to the proposal from comment 0: this eliminates the hop from the worker launcher thread to the worker.  This is less a performance win, as the worker launcher thread should never be contended, but would represent a meaningful simplification in implementation and conceptual complexity as all non-lifecycle events would only be dealing with a single thread.

Back to Bug 1672493 Comment 1