Off-main-thread Worker ScriptLoader
Categories
(Core :: DOM: Workers, enhancement, P2)
Tracking
()
People
(Reporter: edenchuang, Assigned: edenchuang)
References
(Depends on 1 open bug, Blocks 5 open bugs)
Details
Attachments
(4 files, 1 obsolete file)
This is part of bug 1672491. This bug is specific to making Worker ScriptLoader work off-main thread. To be more specific, fetching the script off-main thread (maybe through PFetch).
| Assignee | ||
Updated•1 year ago
|
Comment 1•1 year ago
|
||
Re: PFetch, Note that right now the ScriptLoader in the content-process when working for ServiceWorkers does a Cache API check then falls back to a network fetch. I think we could potentially restructure this to help reduce latency and ideally reduce complexity.
Specifically, we have 2 modes of ServiceWorker script loading:
- During the install phase, we need to go to the network and add things to the cache if they aren't there.
- Once installed, we never go to network and if something isn't in the cache, it's an error.
For the installed case, it seems reasonable that we could run a MatchAll() call once on the chrome namespace Cache where we have stored the script pieces. We can then hold these InternalResponses in a map and not have to do any dynamic lookups as the load requests come in. The caveat is that unless we also support dynamic lookups, we would need to hold onto all of the responses other than the root script once the evaluation phase completes because it is still possible to call importScripts() at runtime.
The installation phase could potentially then also use this rep where it would not do queries; we would just add responses to the map as we fetch them. For simplicity, I think this would suggest that we want to do the Cache API calls from the spawned worker rather than having the ServiceWorkerManager pre-emptively do something to cause the parent process to perform the MatchAll and send the results to the newly spawned worker. This is because that we we already have the Cache instance around for the install phase, but also because it doesn't create any new interesting edge-cases.
An interesting question is whether we could use Cache::Add{All} to simplify things, since it also internalizes the fetch; the answer is probably no because it explicitly sets the destination to subresource, but it's worth sanity checking the idea.
| Assignee | ||
Comment 4•7 months ago
|
||
| Assignee | ||
Comment 5•7 months ago
|
||
This patch introduces a new load handler PFetchLoadHandler for WorkerScriptLoader.
PFetchLoadHandler uses the PFetch to send the request to the parent process and create a channel on the parent process, then send the response back to the content process Worker thread.
Then saving the response into the cache if needed.
The basic workflow of PFetchLoadHandler is
- Extract the InternalRequest from WorkerScriptLoader and ThreadSafeRequsetHandle.
- Create the Promise and PFetch pairs and setup the corresponding FetchOpArgs for script fetching.
- Call FetchChild::DoFetchOp(FetchOpArgs) to parent process to perform the script fetching through FetchService/FetchInstance/FetchDriver.
- Once the the script fetching complete, the response will be propagated to the content process Worker thread by PFetch and resolve the created Promise in the step2.
- PFetchLoadHandler is the Promise's handler to setup WorkerScriptLoader with response's headers and pump with the response's body for reading.
- Once the stream reading is done, OnStreamComplete() will be called to setup for executing the script.
- In the end, call ThreadSafeRequestHandle::OnStreamComplete() to trigger the script executing on the Worker thread.
Depends on D250249
| Assignee | ||
Comment 6•7 months ago
|
||
This patch uses PFetchLoadHandler in WorkerScriptLoader to perform the script loading.
If the Worker is running on the parent thread, the script loading will fallback to use NetworkLoadHandler and CacheLoadHandler, this is because the channel must be created and opened on the parent process main thread.
Depends on D250250
Updated•7 months ago
|
Updated•7 months ago
|
Updated•7 months ago
|
Updated•6 months ago
|
Updated•6 months ago
|
| Assignee | ||
Comment 7•6 months ago
|
||
This patch introduces a new load handler PFetchLoadHandler for WorkerScriptLoader.
PFetchLoadHandler uses the PFetch to send the request to the parent process and create a channel on the parent process, then send the response back to the content process Worker thread.
The basic workflow of PFetchLoadHandler is
- Extract the InternalRequest from WorkerScriptLoader and ThreadSafeRequsetHandle.
- Create PFetch IPCs and setup the corresponding FetchOpArgs for script fetching.
- Call FetchChild::DoFetchOp(FetchOpArgs) to parent process to perform the script fetching through FetchService/FetchInstance/FetchDriver.
- Once the the script fetching complete, the response will be propagated to the content process Worker thread by PFetch and trigger the response handling.
- PFetchLoadHandler::ReceivedResponse() is called when response is avaiable, it update the WorkerScriptLoader and ThreadSafeRequestHandler according to response's headers and creates pump with the response's body for reading.
- Once the stream reading is done, PFetchLoadHandler::OnStreamComplete() is called to complete the script loading and try to execute the script.
Depends on D250251
| Assignee | ||
Comment 8•4 months ago
|
||
This patch introduces a new load handler PFetchLoadHandler for WorkerScriptLoader.
PFetchLoadHandler uses the PFetch to send the request to the parent process and create a channel on the parent process, then send the response back to the content process Worker thread.
The basic workflow of PFetchLoadHandler is
- Extract the InternalRequest from WorkerScriptLoader and ThreadSafeRequsetHandle.
- Create PFetch IPCs and setup the corresponding FetchOpArgs for script fetching.
- Call FetchChild::DoFetchOp(FetchOpArgs) to parent process to perform the script fetching through FetchService/FetchInstance/FetchDriver.
- Once the the script fetching complete, the response will be propagated to the content process Worker thread by PFetch and trigger the response handling.
- PFetchLoadHandler::ReceivedResponse() is called when response is avaiable, it update the WorkerScriptLoader and ThreadSafeRequestHandler according to response's headers and creates pump with the response's body for reading.
- Once the stream reading is done, PFetchLoadHandler::OnStreamComplete() is called to complete the script loading and try to execute the script.
Depends on D250251
Updated•4 months ago
|
Description
•