There is a lot in this proposal; at least two distinct features. it might be difficult to review as a single piece.
How is that supposed to happen, though? I see that you're adding a convert operation to Clock that converts an arbitrary instant of an arbitrary other clock into this clock, but it's really unclear how that could possibly work for a truly arbitrary clock. The idea seems to be that it looks at the clock traits and picks one of the system implementations, but that's effectively requiring all useful implementations of Clock to be essentially like one of the system clocks. And as an abstract operation on clocks, it doesn't really make much sense — there's no meaningful way to turn a future instant of a suspending clock into any other measure of time.
Rather than asking executors to do this somewhat heroic recognition of different kinds of clocks, it might make sense to add a new primitive scheduling operation to Clock. Clock.sleep() is already a scheduling operation, so this wouldn't be crossing any surprising boundaries. The only problem with Clock.sleep() as a scheduling operation is that it's so high-level that it doesn't give you primitive control, but that's fixable with a new operation, and of course the new operation can be (inefficiently) implemented in terms of the old.
I would suggest this:
protocol Clock {
/// Run the given job on an unspecified executor at some point
/// after the given instant.
func run(_ job: consuming ExecutorJob,
after: Instant, tolerance: Duration?)
}
SchedulableExecutor.enqueue can try to recognize specific clocks, but if it sees one it doesn't recognize, it can fall back on the above, passing a new job that immediately enqueues the original job on self. This is basically always fine as a fallback, and it'll usually be necessary anyway, since systems do not have a general ability to trigger timers on an arbitrary executor, and any optimized path for a particular clock + executor that does exist is necessarily specific to the pair.
Why does SchedulableExecutor offer both enqueue(_:after:tolerance:clock:) and enqueue(_:at:tolerance:clock) as requirements? Are there different clocks that prefer each of these for some reason? My perhaps-ignorant expectation is that allowing a duration to be specified is a user-facing convenience — a perfectly reasonable thing to offer as a protocol extension method — but that in the core implementation (and thus the generic entry-points) we'd always want to derive the target instant ASAP and then wait for that rather than passing the duration around.
Also, is it important to distinguish SchedulableExecutor from Executor? It seems to me that pulling this up to Executor and giving it a default implementation that does a double-dispatch would be perfectly fine.
I think if you do all this, you don't need clock instant/duration conversion or the clock traits.
The discussion of both executor-private data and LocalAllocator needs to be much clearer about when these things can be used. The private data must be invalidated, and all allocations on the allocator must be freed (in reverse order!), prior to running the job. As it stands, I think someone could easily come away from the proposal believing that you can allocate memory with the local allocator, and it'll just be automatically freed when the task is destroyed.
The underlying assumption is that you can convert an instant in clock A to an instant in clock B by asking for the duration between the instant in clock A and the current time in clock A, then adding that to the current time in clock B. This doesn't seem like an unreasonable assumption to make in general. The only time it would break is if you had a Clock that wasn't really time-based, which would be unusual.
The traits are there to allow executors that have support for more nuanced scheduling (for instance, the ability to wait on both a suspending and a continuous basis) to take more specific account of what the user has asked them to do. I don't think there needs to be a heroic effort made here — best effort would seem to me to be perfectly fine.
Some executors may find it easier to implement one of these rather than the other, depending on what they're built on top of. Always converting to an instant, or always converting to a duration, means that we'd be unable to take advantage, at a higher layer, of platform knowledge about which was more efficient, and we might sometimes be stuck doing something less efficient for no real reason.
I don't think we should make it a requirement that every executor is able to enqueue tasks after a delay or after an instant, and giving a default implementation based on a double-dispatch then begs questions about which executor we dispatch to and why.
I don't think that's really true. Imagine I want to implement a UTCClock that works with the existing executors on my platform. How would I go about that, without being able to convert the timestamp somehow? And how does it go about waiting when it implements the Clock.run() method you propose above? Something would need to convert the instant or duration, and something would need to somehow understand which underlying clocks are directly supported by some existing executor or other, as well as how to convert a time to something one of those clocks understands.
The proposal as it stands doesn't suffer from that. Any SchedulableExecutor can enqueue using any reasonable clock, and we don't need to hop to some other executor to do so; nor do Clocks need to know anything about executors.
The problem is not that this assumes that that all clocks are time-based (which is a totally reasonable assumption), but that it assumes that time advances in essentially the same way for all clocks. Effectively, it's saying that clocks only really differ in baseline and precision. If we're serious about supporting discontinuous clocks like SuspendingClock, that just seems wrong. The instant of now+60s on a discontinuous clock is potentially a radically different moment in time than now+60s on a continuous clock.
I'll admit that I'm assuming that computing now and adding offsets to instants is basically a negligible cost, and maybe that's not true. The proposal does already seem to be treating these as negligible costs when we need to translate clocks, though. But having multiple requirements is a real complexity and code-size cost of its own.
Well, every executor does have the ability to do this via a double-dispatch, though. The Clock.sleep() implementations you're imagining are, I take it, going to dynamically check for a SchedulableExecutor, use its facilities if available, and otherwise double-dispatch? Are you expecting that there's a use pattern where something would not just double-dispatch if presented with a non-schedulable executor?
At a low level, this is inherently going to be bound up with the clock implementation, and it's almost always going to be extremely inflexible because it's tied to e.g. system timer facilities that cannot possibly be integrated into an arbitrary executor's scheduling requirements. That's what I meant when I said this can fundamentally only be specialized for specific pairs of clock and executor.
But you're right to point out that some clocks can be converted into other clocks. Maybe my suggestion isn't the cleanest way of handling that. I think we need to break this down, though.
As I understand it, your proposal is that:
Clock.sleep gets the current executor, suspends the task, and constructs a resumption job that continues its execution. If the executor is schedulable, it just delegates to it; otherwise, it uses some clock-specific ability to trigger an event later to enqueue the resumption job (which is what we've been calling double-dispatch).
Schedulable executors (optionally) use the clock convert operation to try to convert the clock instant into something they know how to handle specially.
Otherwise, schedulable executors use duration math to translate the clock instant into something they know how to handle specially.
This inherently assumes that schedulable executors can basically handle all clocks equally well, which is load-bearing (as discussed above) on the idea that translating between clocks at worst introduces some small amount of noise. The clock basically loses all control over execution the second it delegates to a schedulable executor. This just seems weird to me, because (1) the clock already has to have some code to trigger a timer after a particular instant in order to support non-schedulable executors and (2) the clock is likely to be the most knowledgeable part of the system about how to trigger that timer. Overall, I think this very neatly handles clocks (like your UTCClock) that can be thought of as a simple translation on top of a clock that the executor knows how to handle (probably ContinuousClock), but it's very prone to bugs where individual executors just completely mishandle scheduling on other clocks (probably all non-continuous clocks)
My suggestion was:
Clock.sleep gets the current executor, suspends the task, and constructs a resumption job. It asks the executor to schedule the resumption job after the instant.
The executor (optionally) recognizes certain clocks and handles them specially.
Otherwise, the executor constructs an enqueue job that enqueues the resumption job on itself, then asks the clock to schedule the enqueue job (double-dispatch).
If all operations follow this flow, translation clocks can simply change step 1 to sleep on the target clock. (They can implement the schedule operation the same way.) This is generally the right thing for them to do anyway, and it transparently works great with any optimizations the executor might have for the target clock. However, if there are other calls to executor.enqueue(_:after:) that do not translate the clock first, they will miss any target-clock optimizations in the executor (assuming the executor does not know about the translating clock) and end up with a double-dispatch.
I think that might actually just be acceptable. enqueue(_:after:) is a low-level operation that takes an ExecutorJob, and high-level operations are likely to start with the clock and therefore can presumably be overridden to do translation first, and the double-dispatch is not likely to be horrifically bad for performance anyway.
However, I think there's a cleaner path that integrates my idea into yours. We can keep the currently-proposed instant-conversion operation to try to convert a clock instant into a specific clock, and executors can use this to try to get an instant in a specific clock they support. This is probably the cleanest way to implement my step 2 regardless. But the fallback path would continue to use double-dispatch by asking the clock to schedule a job rather than using duration math.
I think this would allow us to at least drop all of the duration conversion operations from the current proposal.
One advantage of that approach is that we could offer a high-performance default implementation of Clock.sleep in terms of the new primitive scheduling operation (and vice-versa, such that the user only has to provide one or the other). Clock.sleep is a pretty complex operation in both of our proposals, and implementing it outside of the standard library in cases where the clock isn't just a translation is a real pain.
I don't think it's a problem that the primitive scheduling operation I'm suggesting doesn't specify what executor it calls on; really, I think that's unavoidable. We should still be able to take advantage of it for common executors: there's no reason that e.g. the default task executor can't just resume the task on whatever executor the timer fires on, and similarly, default actors can try to acquire the actor lock exactly as if the task were already running and had just requested to switch to the actor.
Perhaps there should be a boolean flag to the schedule operation indicating heavyweight jobs like resuming a task. Clock implementations that naturally want to run timers on a constrained resource (e.g. a shared thread) would be able to elect to only run lightweight jobs directly with their resource. Otherwise, such implementations are likely forced to triple-dispatch:
An executor asks the clock to schedule a job that happens to be an enqueue job, because it has no way to directly make the clock run a job on it.
Enqueue jobs are lightweight enough that it's not problematic to run them with the constrained resource.
The clock knows that some jobs it receives this way are allowed to be heavyweight, and it doesn't know that this job isn't one of them, so it wraps the job in a second enqueue job out of caution.
As a result, the timer runs the second enqueue job on the constrained resource, which just enqueues the first enqueue job on some other executor (probably the default task executor), which itself just enqueues a job on the actual target executor. So we get an unnecessary hop.
In such a case, you would be responsible for calling the appropriate runtime function to create the executors from the executor factory, since you aren't going through an async@main . That's fine, but it will be fragile against other people trying to do the same thing.
What are the runtime APIs for hooking this up? I'm coming from Windows and the WinUI framework, where they are using DispatcherQueue and run their own Win32 message pump via Application.Start. The general workflow for a WinUI application is that you call Application.start in your main method, and it doesn't return until your application is done. I'd expect that inside that initialization callback is where we'd be able to hook up the swift main executor to the DispatcherQueue returned via DispatcherQueue.getForCurrentThread
What would the API usage look like here for that, assuming the applications don't use async main (which sounds fine to me)?
The same question applies to Gtk. It works quite similarly to WinUI, although @kabiroberai pointed out that it would be possible to invert things in Gtk’s situation and run its event loop on Swift’s main run loop (instead of the other way around) because Gtk’s event loop allows manual single stepping.
On another note;
The requirement for users of global executor-vending libraries to set the global executor via a cli argument each time that they compile their app makes this proposal a bit unappealing from the point of view of cross-platform UI frameworks. I maintain SwiftCrossUI, which generally uses the native UI framework for each platform, and a goal of the project (along with Swift Bundler) is for developers to be able to develop apps for multiple platforms with minimal platform-specific gotchas.
The aforementioned CLI argument would be a gotcha, particularly for people developing Mac-first or Windows-first when they compile their apps for Linux occasionally. And it’s not an obvious gotcha that gives you a compiler error or warning; your concurrent code would just mysteriously fail on Linux without further explanation.
Swift Bundler could of course detect the use of SwiftCrossUI with GtkBackend on Linux and insert the relevant CLI arguments, but I don’t want to hard code SwiftCrossUI stuff into Swift Bundler (that would be lock-in), and for all Swift Bundler knows, someone could import SwiftCrossUI but not use its app initialisation code (e.g. if they’re incrementally adopting SwiftCrossUI in an existing Gtk app) meaning that we’d clobber whatever executor solution their base framework is using.
I could alternatively make a custom executor that falls through when not needed so that you can use it on every platform with every backend, but that’s still a gotcha and still annoying. Imo building cross platform apps with Swift shouldn’t feel like an advanced use case, even if there’s advanced stuff (such as global executors) happening behind the scenes.
I guess what I’m asking for/suggesting is a way for libraries to vend a global executor that is at least the default for consumers of the library in the presence of no other global executor-vending libraries. Perhaps if multiple depended upon products vend default global executors, the user could just get a warning that there are multiple vended global executors and that they must select one (or none) via SwiftPM CLI arguments. This suggestion would also require a way for SwiftPM targets/products to specify which executor (if any) they’d like to vend as a default global executor. I believe that it’d be uncommon for libraries to vend default global executors, and even less likely for people to import two such libraries, but I could be mistaken.
This is no longer part of the proposal; instead, the program's main module will use a "magic" typealias to specify the executor factory to be used.
We did think about allowing libraries to set the executor, but the problem with that is that you could import multiple conflicting libraries, and indeed your dependencies could start importing some library that tried to set the executor, so that model doesn't really work. It feels right that it should be the main program in charge of which executor is in use.
Ah apologies, I saw that discussed but didn’t realise that it made it into the proposal. Is there a new version of the pitch somewhere with that change?
A similar issue still stands, although much less potent than with CLI arguments given that you only need to remember the typealias once.
I wonder, would it be possible to allow the user’s @main type to be checked for the magic typealias as well (or perhaps, instead, in the case of targets without top level code)? That would allow libraries to vend a protocol that provides a default typealias in an extension or something. In SwiftCrossUI’s case that would be an extension on the App protocol. With this approach, conflicting library-vended executor factories would be less likely (compared to letting libraries just declare a default global executor), and if they did occur the compiler would have to deal with it in some way anyway, because otherwise there’d be a type name conflict. I’m unfortunately not at my laptop so I haven’t checked what Swift’s behaviour is in the case of conflicting typealiases in protocol extensions when a type conforms to both protocols.
I scrolled through this thread quickly to find the proposed name for the typealias and couldn’t find it (on mobile) so I’ll just call it GlobalExecutorFactory for these examples. Here’s what my suggestion would look like in regular usage;
And here’s how my suggestion (hopefully) allows libraries to vend global executors and have them automatically used only when applicable, i.e. when the user’s main type conforms to the protocol and likely uses its default entry point.
protocol App {
… // may or may not need GlobalExecutorFactory as an associatedtype here. Not sure without checking (not at laptop)
}
extension App {
// Default global executor factory for anyone whose `@main` type conforms to App
typealias GlobalExecutorFactory = …
// Usual SwiftCrossUI entry point
static func main() { … }
}
I’m away from my laptop until the 11th of July so I can’t actually test the feasibility of those statements and provide more concrete examples. Please let me know if I’ve suggested anything impossible there (most likely type system wise).
For a Swift program that uses WinUI, I'd expect it to use asyncmain and then have its main module specify an appropriate executor that will work with WinUI. That executor would be running a normal Win32 event loop under the covers, with a couple of tweaks for WinUI on top, which is basically what Application.start ends up doing internally anyway.
I suspect the answer is yes, and I like your thinking regarding allowing libraries to gently push people towards a particular executor by way of a protocol conformance or inheritance. I'll have an experiment and see if I can get this to work in a reasonable manner.
I just had a read through the latest proposal (linked to by Pitch 3) and it seems to still use command line arguments for executor factory selection. It has an alternative considered for typealiases in @main structs, but doesn’t mention a top level typealias anywhere. Perhaps it was only half updated?