Comments (3)
Hi, @benjamingr, apologies for the lag.
@shaseley thank you for the response. I'm still not sure I understand entirely. I think a lot of these things you mentioned here (all of them?) have ad-hoc solutions (like isInputPending, requestPostAnimationFrame etc).
I am looking in particular for either:
I don't think it's an either or here, but maybe a little (arguably) from (a), and a good deal from (b) and (c).
The API exposes three priorities that are used for two purposes:
- Coordination between all the things running on the page — 1P, 1P library, framework, 3P script. I believe this matches your second point below.
- For the browser to use for scheduling decisions, i.e. when selecting the next task to run from its various task queues. I think this has some overlap with your first and third points below (more below).
And it provides one guarantee: that user-blocking tasks run before user-visible tasks, which in turn run before background tasks. There are no ordering guarantees between postTask
tasks and other tasks, although the priority should be used in decisions ((2) above).
- A capability that this API adds that is impossible without it. For example a use case that this addresses and mixing and matching isInputPending / requestPostAnimationFrame / requestIdleCallback / setTimeout doesn't address.
'user-blocking' priority is probably the closest thing to a (performance) capability. To elaborate, you’re right that much of postTask
in its current form can be polyfilled by these APIs:
-
postTask(foo, {priority: ‘background’})
maps pretty well torequestIdleCallback
, and preserves the guarantee mentioned above. -
postTask(foo, {priority: ‘user-visible’})
maps pretty well tosetTimeout(foo, 0)
, with the caveat thatsetTimeout
enforces a minimum delay after a certain nesting depth (which is why some developers have moved topostMessage
).
For 'user-blocking' tasks, to get the same ordering guarantees as postTask
, there are two options:
-
Multiplex
setTimeout
(or whatever) to run both 'user-visible' and 'user-blocking' tasks. This is pretty much what userspace schedulers do, but there isn't a performance benefit with regards to other queued tasks (the browser can't tell the difference between the differentsetTimeouts
). -
Use an API that has higher priority. The closest thing we have is to hijack rendering and use
requestAnimationFrame
, orrequestPostAnimationFrame
if it ships. But there are no ordering guarantees between rendering and non-rendering tasks (in Chromium, we’ve been working on increasing the priority of rendering, but there are trade-offs). These can also incur unnecessary overhead if not actually rendering, and they don't run in the background, forcing devs to change scheduling methods based on page visibility (both of which probably fall in the category of your third point).
There are also plans for building features on top of postTask which might be considered capabilities.
-
Propagating scheduler context. The alternative is plumbing the signal everywhere.
-
Integrating async APIs like fetch and IDB with TaskSignal (for priorities, etc.). The alternative now is to run short handlers that just queue tasks, but this doesn't help gain a higher priority.
-
Controlling 3P script (very, very early stages).
- An acknowledgement that this doesn't address any additional use cases but allows for mixing and matching different priorities in a way that requires "globality" - like an app embedded in another page and they need the scheduler to come from the platform so they can share priorities).
- An acknowledgement that this doesn't actually add a capability but makes code a lot nicer to write since writing schedulers correctly is incredibly challenging (I've maintained the hacks we do at bluebird and it was pretty challenging at times).
I think both of these are important and are goals of the API. I'll acknowledge that the API is very polyfillable, but that performance characteristics will likely vary because of no direct mapping for 'user-blocking' (something we're trying to determine in testing / origin trial).
To be clear, I am not objecting to this API in either of these three cases. I think this sort of API makes sense for the web platform anyway and I've found it incredibly useful in platforms that had it (like asp.net webapi servers). This sort of API (and controller) would also enable things like "schedule things off request stories" and actually can help quite a bit with seemingly unrelated things because it binds the tasks to a context (so for example the platform could use this for debugging info, context and scope).
from scheduling-apis.
Thanks for the suggestions, I agree that we should add those things.
One of the main use cases we're targeting with postTask
is existing userspace schedulers. Many of them have a shape similar to the following:
- Accumulate tasks in a (userspace) priority queue
- Queue a scheduler macrotask with the browser via
postMessage
,setTimeout
, etc. that will:- Run some number of tasks out of the priority queue
- Decide to yield and reschedule the macrotask
- Repeat
A couple important bits here are the yielding and scheduling of the macrotask. Apps and frameworks want to yield to keep the page responsive (input and rendering) and also not starve out other high priority work, e.g. I/O.
postTask
aims to replace the "queue a macrotask" step. The priority in this case helps the browser make decisions about which tasks it should run before running the next iteration of the scheduler task. For example, if the scheduler is processing work associated with a user input — but still chunking work to keep the UI responsive and MT animations smooth — they could use 'user-blocking'
priority to indicate to the browser that the user is waiting for this work to finish. The browser could then use that to schedule other work accordingly (e.g. after yielding to rendering, resume the postTask
task instead of servicing timers).
Of course other non-userspace script can use this API (setTimeout
is often used to yield back to the event loop) — often in conjunction with an underlying scheduler (e.g. in the framework). In this case, the priorities help coordinate.
It's also worth noting that we're considering exposing the priorities to async APIs like fetch
and IDB so they can be scheduled as part of the same system.
I'll work on adding use cases and example code to better illustrate these benefits in practice.
from scheduling-apis.
@shaseley thank you for the response. I'm still not sure I understand entirely. I think a lot of these things you mentioned here (all of them?) have ad-hoc solutions (like isInputPending, requestPostAnimationFrame etc).
I am looking in particular for either:
- A capability that this API adds that is impossible without it. For example a use case that this addresses and mixing and matching isInputPending / requestPostAnimationFrame / requestIdleCallback / setTimeout doesn't address.
- An acknowledgement that this doesn't address any additional use cases but allows for mixing and matching different priorities in a way that requires "globality" - like an app embedded in another page and they need the scheduler to come from the platform so they can share priorities).
- An acknowledgement that this doesn't actually add a capability but makes code a lot nicer to write since writing schedulers correctly is incredibly challenging (I've maintained the hacks we do at bluebird and it was pretty challenging at times).
To be clear, I am not objecting to this API in either of these three cases. I think this sort of API makes sense for the web platform anyway and I've found it incredibly useful in platforms that had it (like asp.net webapi servers). This sort of API (and controller) would also enable things like "schedule things off request stories" and actually can help quite a bit with seemingly unrelated things because it binds the tasks to a context (so for example the platform could use this for debugging info, context and scope).
from scheduling-apis.
Related Issues (20)
- TaskController constructor should probably take a dictionary instead of an optional priority HOT 2
- Fill in security and privacy considerations
- Link to the spec from appropriate places HOT 1
- Document minimum version for local testing of TaskPriorityChangeEvent and previousPriority
- Publish postTask Polyfill HOT 1
- schedule a postTask task doesn't always return a promise
- Questions around this proposal HOT 6
- [question] The relationship between the scheduling APIs and built-in functions. HOT 2
- Standardization HOT 2
- [Suggestion] isTaskPending
- https://wicg.github.io/scheduling-apis/#sec-scheduler-alg-scheduling-tasks doesn't explain what happens if the associated document isn't fully active HOT 2
- self.scheduler should be [[Replaceable]] HOT 2
- Aborting tasks with async hops HOT 9
- How do we measure the effectiveness of this API? HOT 9
- Offering my help (from the perspective of building a userspace scheduler)
- Broken references in Prioritized Task Scheduling
- Specify event loop integration / priority intent HOT 6
- scheduler.yield seems to supersede image loading operations HOT 4
- Structured concurrency and cancellation
- Need to remove the entry from the task queue maps when the queue is empty HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from scheduling-apis.