6 ms·
> It's trivial to turn async into sync in Rust. You can use ".poll", "executor::block_on", et cetera. Is it 0-cost abstraction? I mean, is `sync_read` will com
by vbezhenar 7y ago
> It's trivial to turn async into sync in Rust. You can use ".poll", "executor::block_on", et cetera.
Is it 0-cost abstraction? I mean, is `sync_read` will compile to the same code like `async_read.poll`? Because turning sync into async is kind of trivial as well: just spawn new thread for that sync block.
- oconnor663 7y agoThat's a tricky question. My understanding is that while it's (in theory, modulo compiler bugs and features) a 0-cost abstraction over different underlying system APIs, those different underlying system APIs aren't necessarily the same cost. For example, if I'm trying to read from a socket in the synchronous world, I just issue the `read` system call. But in the async world I'm going to do quite a bit more: - Create an epoll descriptor. - Add my socket to that descriptor. - Poll the descriptor for a readiness notification. - Read the descriptor. Those first three system calls weren't required in the synchronous version, and unless the read is large enough to overshadow them, they represent some additional cost. But that cost is required by the OS itself, not by Rust's abstractions. Someone with more experience writing Mio code might want to jump in and correct me here though.
- Argorak 7y agoYou're pretty much correct, there is a tradeoff here. There's reasons why many high-performance systems like databases are mixed systems.
- necubi 7y agoSpawning a new thread for an operation is not async in the sense people typically mean. For an async IO library, you would expect it to be using async IO primitives like epoll, not just wrapping blocking operations in a thread.
- schrodinger 7y agoThat’s what I like about Go. You write sync code, but because Go routines aren’t OS threads they operate with the efficency of async code.
- pcwalton 7y ago> You write sync code, but because Go routines aren’t OS threads they operate with the efficency of async code. No, they don't. Goroutines have stacks, while Rust async code does not. Go has to start stacks small and copy and grow them dynamically because it doesn't statically know how deep your call stack is going to get, while async/await compiles to a state machine, which allows for up-front allocation. Furthermore, Go's M:N scheduling imposes significant costs in other places, such as FFI. Besides, for the vast majority of apps, OS threads are not significantly different from goroutines in terms of efficiency. Rust doesn't have a GIL and per-thread startup time and memory usage are very low. It only starts to matter once you have a lot of threads—as in, tens of thousands of clients per second—and in that case it's mostly stack size that is the limiting factor.
- lossolo 7y ago> OS threads are not significantly different from goroutines in terms of efficiency This is not true for a use case with a lot of connections, additionally context switch cost a lot more now with all side channel attack mitigations on.
- pcwalton 7y agoPeople are happily running Rust servers in production using thousands of concurrent threads per second.
- lossolo 7y agoI don't doubt that but apparently we have different definitions of a lot. Additionally latency and hardware also matters, I can say that C is doing n things a second while PHP is doing the same but C is running on EC2 micro instance and PHP is running on 2x Intel Xeon Platinum 9282 dedicated machine. C10K problem was not solved by 1:1 model and this is an old problem. C100K+ is what I see in some of production systems I work on.
- monocasa 7y agoAFAIK, that's just for file I/O, which doesn't really work well with epoll.
- Argorak 7y agoCorrect. It's also common practice.
- heavenlyblue 7y agoIf you're writing sync code the last thing you're thinking about is costs associated with these APIs. Sync is a rudiment of our close past. We use it when we need to shave off development costs.
- Argorak 7y ago0-cost abstraction was summarised by Stroustrup as: > What you don’t use, you don’t pay for. And further: What you do use, you couldn’t hand code any better. In that mind-set, it is completely okay that `sync_read` and `async_read.await` can totally compile to something different, as they abstract different things. Boats has some more thoughts on this here: https://boats.gitlab.io/blog/post/zero-cost-abstractions/ https://boats.gitlab.io/blog/post/zero-cost-abstractions/
- deleted 7y ago[deleted]