9 ms·
Ruby 3, Concurrency and the Ecosystem
- brandur 6y agoExcellent points in the article about "top" (booting off top-level workers in Ractors) versus "bottom" (parallelization at the edges of the code where not as much state needs to be shared) cases, with the top likely to take more time to widely manifest as more Ruby code becomes Ractor friendly. I know Ractors are still quite new, but I'm curious to see how much luck people will have in migrating existing parallel code over to them. Relying on a lot of shared state is going be the major blocker. Just on a token reading of the docs [1], it looks like you should be able to share quite a lot of state still as long as you're able to freeze it all into immutability. You could then use message passing around the edges where immutability is a non-starter. But this is probably easier said than done. --- [1] https://docs.ruby-lang.org/en/3.0.0/Ractor.html https://docs.ruby-lang.org/en/3.0.0/Ractor.html
- claudiojulio 6y agoI don't understand, actix-web uses async / await and is ultra fast. https://www.techempower.com/benchmarks/ https://www.techempower.com/benchmarks/ Does this have to do with the Tokio runtime that more efficiently coordinates the asynchronous code? Automatically translated.
- digikata 6y agoI think the area is a lot more complex than the relative latency and throughput of synchronous vs async. For the observations in the environments of Ruby and Python, I think the observations feel true, but switching out to different sections of processing to different technologies I suspect different generalizations would have to be made.
- ramchip 6y agoI would take this benchmark with a huge grain of salt: https://64.github.io/actix/#blazingly-fast-or-not https://64.github.io/actix/#blazingly-fast-or-not
- breatheoften 6y agoI'd expect any python codebase which tries to use async/await based co-operative multi-tasking for concurrency at a larger scale would inevitably run into long-tail latency walls -- not just once but repeatedly as the codebase grows/changes. The potential for a code change or addition to produce unexpected synchronous blocking (potentially with GIL held) _somewhere_ seems really high and I have to imagine it would be very hard to avoid in the context of a real application codebase ...
- zelly 6y agoYou would have the same problem in Rust/Tokio and many other languages. The compiler is not able to know which calls could block. Suppose you have this async state machine server on a single thread, super fast, but somewhere it opens a file synchronously, let's say on a network mount to make things worse. No way a purely static VM-free language can help against that. Go (see: goroutines) is able to automatically suspend at those points and continue execution elsewhere. It comes with a big runtime but you get what you pay for.
- heftig 6y agoWhat's Go going to do if some ffi library written in C does a synchronous syscall? It's in the same situation.
- zelly 6y agoGo discourages C FFI but it runs those in kernel threads just to be safe. If you are using a lot of C FFI calls that all block, then it could be a problem (all cores are blocked).
- _-___________-_ 6y agoYes, but there’s nothing akin to the GIL in Rust/Tokio, so the potential impact of blocking is lower, and you can use spawn_blocking to move any blocking code (that you cannot eliminate) off the main worker threads that are polling futures, or use other techniques like run the blocking work on another standard thread and await on a channel for the result.
- billllll 6y agoSomeone please correct me if I'm wrong, but Rust async/await isn't implemented with green threads, but with OS threads. Those can be pre-empted.
- steveklabnik 6y agoAsync/await does not use threads of any kind. It creates a state machine. Executors may execute those state machines on a single thread, or map them to many threads. The latter looks kinda like green threads depending on what your definition of “green thread” is.
- gameswithgo 6y agonothing about how it works is built into the language, its up to user code/libraries to decide how to do it. the popular libraries like tokio are using something akin to green threads, though people picky about definitions may not agree to that nomenclature.
- sudeepj 6y ago> nothing about how it works is built into the language I think the state machine required to yield, async & await is baked into the language. But yes, one can implement the Future trait to take finer control.
- zelly 6y agoIt's single threaded and completely userspace. Rust async/await is a fancy way to write poll/epoll. It can limit the amount of time your program is blocked on IO (insofar as the implementation is as you would expect it (not guaranteed by the language)), but it will not do parallelism. For example you wouldn't async/await a prime number calculation--you'd have to use OS threads.
- comex 6y agoIt’s not necessarily single threaded; Rust async runtimes typically schedule tasks on a pool of multiple OS threads, usually one thread per CPU core. In other words, it can be seen as a form of M:N threading. Regarding the parent’s question, tasks cannot be preempted from their threads. Threads can of course be preempted from their CPU cores by the OS, but if the number of threads equals the number of cores, this will only happen if other processes on the system are competing for CPU time.
- colonwqbang 6y agoI've a hard time seeing how this could work out. Admittedly it's been some time since I did ruby but as I recall there was a shocking amount of mutability going on in the core language and throughout the ecosystem. Mutable lists, tables and strings everywhere, even the "constants" of ruby were mutable by default which bit me more than once. I'm just not convinced that good parallellism is something you can successfully bolt on to a language 25 years down the road. The languages I've seen do a really good job all embraced parallelism as an important problem from the start and had a strong policy of immutability by default and good persistent data structures. Such a clear commitment permeates the ecosystem and you get libraries with clear parallelism stories . Hopefully I'm wrong and this will all work out great for ruby.
- deleted 6y ago[deleted]
- gameswithgo 6y agoI don't understand bolting concurrency onto something that is interpreted. A compiler would get you 10-100x speedup, without your users having to do the complex thing of writing safe parallel code.
- jashmatthews 6y agoCRuby has had JIT for over 3 years now and I think JRuby must be over a decade?
- gameswithgo 6y agoare those first class options now? or is it like python where you can kinda sorta sometimes use a jit?
- jashmatthews 6y agoFirst class option in Ruby 2.6 onwards.
- hosh 6y agoRuby has taught me a lot and launched my career as a dev. I enjoyed meeting the people I have met through it. I have gotten steady, good work for years. I really cut my teeth with working with DSLs, working with mixins, domain boundaries, designing for developer happiness, etc. ... but there's a reason I switched over to using Elixir instead. And that is being able to work with OTP.
- bestinterest 6y agoSome silly multi threaded questions incoming. I ran a quick benchmark here is the code require 'benchmark' require 'prime' Benchmark.bm do |x| x.report('single'){ 8.times do 10_000_000.times.each do |num| num.prime? end end } x.report('thread'){ 8.times.map do Thread.new do 10_000_000.times.each do |num| num.prime? end end end.each(&:join) } x.report('parallel'){ 8.times.map do Ractor.new do 10_000_000.times.each do |num| num.prime? end end end.each(&:take) } end results single -> 121.66s thread -> 122.14s parallel -> 52.84s The single and threaded version were the same and this is mostly expected since we don't have any I/O in the threads which would of given up the thread and scheduled another to be run. For prime calculations, we are just bound by pure CPU and limited by ruby's global interpreter lock I assume this would be the same case in Python. 1) A silly question and more language irrelevant, when looking at my CPU logical core usage during the single and fake threaded version I could see tiny spikes in all the cores. [0] No one core dominated the task how does this work? Does the CPU know to workshare single threaded tasks and branch prediction etc comes into play here? How are modern CPU's achieving this at a lower level? In my head this task would just spike a single core to do all the work. 2) Running the 10_00_000 primes just once completes in 15.3s shouldn't this be the same amount of time for 80_00_000 if the Ractor implementation was optimized across my 8 logical cores? Not a punch or anything at guys working on lang its freaking awesome to see my CPU being maxed out with my 8 logical processors being used in Ruby! [1] but just want to double check my understanding. [0] https://postimg.cc/rKjB9kQP https://postimg.cc/rKjB9kQP [1] https://postimg.cc/WtvpWswN https://postimg.cc/WtvpWswN
- chrisseaton 6y ago> when looking at my CPU logical core usage during the single and fake threaded version I could see tiny spikes in all the cores What do you mean by 'fake threaded'? Ruby threads were already fully concurrent OS threads. They're likely scheduled on different physical cores, so will spike your different cores each time they manage to get the GIL and run. > if the Ractor implementation was optimized across my 8 logical cores? There's synchronisation. Many VM services are global.