3 ms·
Imho, the language should abstract away the difference between threads and async.
by amelius 5y ago
Imho, the language should abstract away the difference between threads and async.
- chrismorgan 5y agoThis is quite impossible for a language in Rust’s space; it would violate Rust’s principle of only paying for what you use, because there is necessarily runtime overhead in providing such an abstraction.
- tjoff 5y agoThe compiler would know that in advance though? Thus you only pay for it if it is required.
- chrismorgan 5y agoThreads and async have different restrictions and costs. No shared abstraction is possible for a language like Rust without losing what makes it Rust. Minimal-cost threading support requires taking Send + Sync into account, essentially thread-safety: and the thread-safe primitives are more expensive than the thread-local primitives. Send + Sync matters become pervasive through the design of threaded code, even if they don’t need to be spelled out most of the time. Minimal-cost async requires compiling the code into a state machine, which means you’ve got to restrict borrowing across suspend points, or mess around with Pin. Threading and async are quite different in a language like Rust. They can even play quite nicely together. But you really can’t unify them. Sure, you could do simple cases like single-threaded blocking-or-async, e.g. `async? fn foo() { bar().await; }` compiling to something like strawman `fn sync#foo() { sync#bar(); } async fn async#foo() { async#bar().await; }`, but this falls apart completely as soon as you want to do anything beyond immediate .await.
- amelius 5y ago> it would violate Rust’s principle of only paying for what you use I guess I'm glad that Rust developers don't design CPUs, as we wouldn't have memory abstractions like cache coherent NUMA (which doesn't come for free).
- adwn 5y agoWho said anything about "free"? chrismorgan wrote "only paying for what you use". In NUMA systems, pretty much every program uses and benefits from cache coherency.
- amelius 5y agoAnd is it still free if you don't want to use it?
- adwn 5y agoWell, that depends on the details of the CPU architecture in question. Without knowing those details, I'd say probably yes, but userspace programs will need to ask the kernel to change the memory attributes of the mapped area they want to access non-coherently. Case in point: a CPU plus a discrete GPU is technically a NUMA system, and low-level APIs like Vulkan explicitly expose the ability to allocate non-coherent memory. I strongly assume that accessing that memory doesn't incur coherency overhead.
- amelius 5y agoIn short, CPU designers gave us the choice, whereas Rust only gives us "the Rust way or the highway".
- adwn 5y ago> Rust only gives us "the Rust way or the highway" What are you even talking about? Rust gives you plenty of choice, it even lets you create as many "non-free" abstractions as you'd like.
- amelius 5y agoBut not at the language level, which is what started this thread. As another example: compare "GC'd versus non-GC'd memory in Rust" to "coherent versus non-coherent memory in CPU architecture"; here Rust gives us just the bare machine, while CPU architects give us quite some abstraction plus a choice. It is true that you could theoretically write a GC in Rust, but using it from the language itself would probably break ergonomics so much that you'd be better off with another language.
- brabel 5y agoOne language that allows for this is Dart. The only way to run code in another Thread is by spawning an `Isolate`. But once you have one, you can expose an API that is indistinguishable from an usual async one.
- pure_simplicity 5y agoCan you point me to any resource that talks more about this ?
- brabel 5y agoYou can check this post about Dart event loop and Isolates (which also has a video explaining it): https://medium.com/dartlang/dart-asynchronous-programming-isolates-and-event-loops-bffc3e296a6a https://medium.com/dartlang/dart-asynchronous-programming-is... And the Dart 2.15 announcement (latest version as of today), which had a focus on making Isolates fast: https://medium.com/dartlang/dart-2-15-7e7a598e508a https://medium.com/dartlang/dart-2-15-7e7a598e508a EDIT: the main article about concurrency in Dart is actually this one: https://dart.dev/guides/language/concurrency https://dart.dev/guides/language/concurrency
- ihateolives 5y agoDart in general has made many sensible choices, it's a nice language to work with. I recently decided to give it a try and I had literally zero friction coming from Java background. I'm rewriting some of my tools with Dart now instead of current mishmash of scripts.
- 0x445442 5y agoYes I recently started programming with Dart and I find it incredible how little buzz there is around it. I’m using it for writing small scripts right now and it’s been so refreshing to not run into one road block. The installation process is simple, documentation is excellent, the core library makes sense, the defacto package repository and package management is clean. And WRT this thread, the async model is dead simple to pick up. As a scripting language it seems to be a perfect fit. But there is also a lot of first rate tooling available for Dart and I am starting to wonder if it wouldn’t be a good choice for larger projects as well.
- gspr 5y agoIt is, at least for me, not at all clear that that's possible to do in a good way within the expressive power of any mainstream language. And "good" in this case of Rust would include "without runtime overhead that could be avoided". Is it possible?
- dodobirdlord 5y agoOnce the compiler has computed the async graph, it could in theory fuse subgraphs of nodes (especially linear sequences) back together and re-express the execution flow in terms of spawning and joining threads. This would be simple and could even be a performance win for some graphs (i.e. embarrassingly parallel ones), and would be a complete trainwreck for many, probably most. I certainly wouldn’t want to be stuck reading the error messages produced by a compiler when it failed to find a satisfactory solution to a complex execution graph. Profile guided optimization might help I suppose, but still.
- Groxx 5y agoYou can run an async func synchronously, just push it onto an executor. Threads are basically orthogonal - you can do that in the same thread or on a dedicated thread.