3 ms·
Thanks! Much more substantial. We're definitely aware of all of this. Tokio is made by two Rust core team members and the person who wrote the most widely used
by steveklabnik 10y ago
Thanks! Much more substantial.
We're definitely aware of all of this. Tokio is made by two Rust core team members and the person who wrote the most widely used async io tool; it's virtually all but provided by Rust itself. And the ecosystem is aware of this too; the other people who were working on AIO have backed tokio as well, and the Rust community in general is interested in not having this split.
> Synchronous code can not use asyn code either because, well, in order to get anything from async code you have to be async yourself.
This is solvable through a threadpool, which tokio provides. In other words, it lets you make a blue function red.
> But they are not the only way to avoid these costs
These do not avoid all costs. For example, they pay the overhead of green threads, which means that you can't interoperate with C code at zero cost. That's a price a language like Rust cannot pay.
- fooyc 10y ago> the Rust community in general is interested in not having this split How will you avoid having two variants of each and every lib ? E.g. redis-rs, sync, and redis-tokio, async ? > you can't interoperate with C code at zero cost Go can't because it has different a memory layout and calling conventions. Other than that, what are you thinking about exactly ?
- steveklabnik 10y ago> How will you avoid having two variants of each and every lib ? E.g. redis-rs, sync, and redis-tokio, async ? If the async version is easy to use, why would you use an explicitly synchronous one? > Other than that, what are you thinking about exactly ? Green threads. To call into C, you need to switch stacks. This is (one of the big reasons) why we removed green threads from Rust.
- fooyc 10y ago> If the async version is easy to use Having a sync interface would make it easy to use in sync code, yes. I feel that we are far from zero cost abstractions now, though. > switch stacks Is it because the stacks in green threads is smaller than what C would expect ? An interesting approach taken by Go here is to avoid calling C as much as possible. They don't call the libc for system calls, for instance. This is also what allows them to switch the execution to an other goroutine just before the syscall.
- steveklabnik 10y ago> I feel that we are far from zero cost abstractions now, though. Why? > Is it because the stacks in green threads is smaller than what C would expect ? Yes. > An interesting approach taken by Go here is to avoid calling C as much as possible. Right, so this is a cost that they can pay. Rust cannot.
- fooyc 10y agoAbout the cost of calling an async function synchronously: You had a call to read(). Now you have a thread, an event loop, a pooling mechanism, and a synchronization with the thread. That's much more system calls and cpu cycles, for an synchronous async read()
- pimeys 10y agoWell no, if you don't explicitly run it in a thread pool?
- fooyc 10y agoCan you explain how you run an async function synchronously, and how it has no overhead compared to calling a synchronous implementation of the same function ?
- Rusky 10y agoAs far as the kernel-side implementation goes, IO is always asynchronous. The CPU is not involved in the actual movement of data between memory and the network interface. When you make a synchronous syscall, the kernel initiates the operation, saves the state of your thread, and starts another one. When the network interface is done, it signals the kernel, which then marks your thread as runnable and schedules it for execution. When you make an asynchronous syscall, the kernel initiates the operation but does not block your thread. This is usually done in the context of an event loop, which makes a synchronous syscall (like epoll_wait) when it runs out of tasks to run. Thus, converting a single async syscall to a sync one means two syscalls: initiate the operation, then wait for its result. The extra round trip between user and kernel mode is basically free in this case because you're blocking on IO, and any logic it implements has to happen in the synchronous case anyway.
- Rusky 10y agoGo has different memory layout and calling conventions in part because of its green threads implementation. It has to switch stacks to call C code because the Go stacks are small and relocatable, to make growing more efficient, which is only possible because of the GC.
- dbaupp 10y ago> How will you avoid having two variants of each and every lib ? E.g. redis-rs, sync, and redis-tokio, async ? By having one, the async one. If someone doesn't care about asynchronicity, there's always some sort of "wait until completion" functionality. (This is practically what the "normal" synchronous IO functions are doing anyway, just internally.)
- zzzcpan 10y agoThis is actually what Go got very wrong. They implemented net completely synchronously, instead of doing it in an event loop and providing synchronous wrappers that communicate with that event loop for those who need them.
- fooyc 10y agoMy point is exactly that this is what they got right
- bluetech 10y ago> How will you avoid having two variants of each and every lib ? The python community is also struggling with this problem. The emerging approach, which seems to me to be the right approach, is to write the libraries such that they do not do any IO; then you can use them anywhere, with some integration. So basically it's just the principle of separation of concerns. See more here: https://sans-io.readthedocs.io/how-to-sans-io.html https://sans-io.readthedocs.io/how-to-sans-io.html
- steveklabnik 10y agoIncidentally, this is sort of one of the design goals of tokio/finagle: that you can write your code in a transport-agnostic way. A timeout future works no matter what protocol you want to implement a timeout for, etc.
- Manishearth 10y ago> This is solvable through a threadpool, which tokio provides. In other words, it lets you make a blue function red. I'll add that once you have this, the async/sync distinction (functions that return Future and those which don't) in Rust becomes exactly the same as fallible/infallible (functions that return Result or don't) and gets handled pretty much the same way.
- fooyc 10y agoCan you "match" on the result to destructure it as well ? Without blocking the event loop ?
- Manishearth 10y agoFutures aren't enums. I meant that you have the ability to handle it there and then (block on it via threadpool), or defer handling (chain to the next async calls in your async function). You have this same pair of abilities with Option, which bridges the basically nonexistant gap between fallible and infallible functions.
- fooyc 10y agoIsn't this adding a lot of overhead compared to calling a synchronous implementation of the same code ?
- Manishearth 10y agoYou'd have to block on I/O somehow. You can avoid the threadpool costs to block, too, IIRC (but that's worse)
- fooyc 10y agoThat's the point. Calling a blocking system call is cheaper that artificially blocking on a non-blocking operation.