8 ms·
Speeding up Ruby by rewriting C in Ruby
- ksec 2y ago>This got me thinking that it would be interesting to see a kind of “YJIT standard library” emerge, where core ruby functionality run in C could be swapped out for Ruby implementations for use by people using YJIT. This actually makes me feel sad because it reminded me of Chris Seaton. The idea isn't new and Chris has been promoting it during his time working on TruffleRuby. I think the idea goes back even further to Rubinius. It is also nice to see TruffleRuby being very fast and YJIT still has lots of headroom to grow. I remember one obstacle with it running rails was memory usage. I wonder if that is still the case.
- Asmod4n 2y agoOne of the amazing things truffle ruby does is handle c extensions like ruby code, meaning C is interpreted and not compiled in a traditional sense. This makes way for jitting c code to make it way faster than the author has written it.
- pantulis 2y agoAmazing indeed!
- deleted 2y ago[deleted]
- 0x457 2y agoYup, Rubinius was probably the most widely known implementation of Ruby's standard library in Ruby. Too bad it was slower than MRI.
- e12e 2y agoI thought maybe mruby had a mostly ruby stdlib - but I guess it's c ported over from mri?
- Lio 2y agoI think jRuby takes a similar approach. It’s possible to write gems which will use underlying C on MRI or Java when running on jRuby. It would be interesting to know if a “pure” would also help jRuby too.
- Imustaskforhelp 2y agosuper interesting , actually I am also a contributer of the https://github.com/bddicken/languages https://github.com/bddicken/languages and after I had tried to create a lua approach , I started to think of truffleruby as it was mentioned somewhere but unfortunately when I had run the code of main.rb , there was virtually no significant difference b/w truffleruby and main.rb (sometimes normal ruby was faster than truffleruby) I am not sure if the benchmark that you had provided showing the speed of truffleruby were made after the changes that you have made. I would really appreciate it if I could verify the benchmark and maybe try to add it to the main https://github.com/bddicken/languages https://github.com/bddicken/languages as a commit as well , because the truffleruby implementation actually is faster than the node js and goes close to bun or even golang for that matter which is nuts. This was a fun post to skim through , definitely bookmarking it.
- cutler 2y agoWith TruffleRuby you'll need to account for startup time and time to max. performance which vary with the native and JVM runtime configurations. See https://github.com/oracle/truffleruby https://github.com/oracle/truffleruby
- pentestercrab 2y ago[flagged]
- jeremy_k 2y agoSuper interesting. I didn't know that YJIT was written in Rust.
- riffraff 2y agoIt was initially written in C then ported to rust[0], which seems like it was a good idea. The downside is that it may not be enabled at build time if you don't have the right toolchain/platform, but that seems a good trade off. 0: https://shopify.engineering/porting-yjit-ruby-compiler-to-rust https://shopify.engineering/porting-yjit-ruby-compiler-to-ru...
- jerf 2y ago"In most ways, these types of benchmarks are meaningless. Python was the slowest language in the benchmark, and yet at the same time it’s the most used language on Github as of October 2024." First, this indicates some sort of deep confusion about the purpose of benchmarks in the first place. Benchmarks are performance tests, not popularity tests. And I don't think I'm just jumping on a bit of bad wording, because I see this idea in its various forms a lot poking out in a lot of conversations. Python is popular because there are many aspects to it, among which is the fact that yes, it really is a rather slow language, but the positives outweigh it for many purposes. They don't cancel it. Python's other positive aspects do not speed it up; indeed, they're actually critically tied to why it is slow in the first place. If they were not, Python would not be slow. It has had a lot of work done on it over the years, after all. Secondly, I think people sort of chant "microbenchmarks are useless", but they aren't useless. I find that microbenchmark actually represents some fairly realistic representation of the relative performance of those various languages. What they are not is totally determinative. You can't divide one language's microbenchmark on this test by another to get a "Python is 160x slower than C". This is, in fact, not an accurate assessment; if you want a single unified number, 40-50 is much closer. But "useless" is way too strong. No language is so wonderful on all other dimensions that it can have something as basic as a function call be dozens of times slower than some other language and yet keep up with that other language in general. (Assuming both languages have had production-quality optimizations applied to them and one of them isn't some very very young language.) It is a real fact about these languages, it is not a huge outlier, and it is a problem I've encountered in real codebases before when I needed to literally optimize out function calls in a dynamic scripting language to speed up certain code to acceptable levels, because function calls in dynamic scripting languages really are expensive in a way that really can matter. It shouldn't be overestimated and used to derive silly "x times faster/slower" values, but at the same time, if you're dismissing these sorts of things, you're throwing away real data. There are no languages that are just as fast as C, except gee golly they just happen to have this one thing where function calls are 1000 times slower for no reason even though everything else is C-speed. These performance differences are reasonably correlated.
- deleted 2y ago[deleted]
- davidw 2y agoIt seems like it's been a while since I've seen one of these language benchmark things. https://benchmarksgame-team.pages.debian.net/benchmarksgame/index.html https://benchmarksgame-team.pages.debian.net/benchmarksgame/... seems like the latest iteration of what used to be a pretty popular one, now with fewer languages and more self-deprecation.
- deleted 2y ago[deleted]
- igouy 2y ago> fewer languages Maybe you've only noticed the dozen in-your-face on the home page? The charts have shown ~27 for a decade or so. There's another half-dozen more in the site map.
- deleted 2y ago[deleted]
- deleted 2y ago[deleted]
- igouy 2y ago> a fun visualization of each language’s performance The effect is similar to dragging a string past a cat: complete distraction — unable to avoid focusing on the movement — unable to extract any information from the movement. To understand the measurements, cover the "fun visualization" and read the numbers in the single column data table. (Unfortunately we aren't able to scan down the column of numbers, because the language implementation name is shown first.) Previously: <blink> https://developer.mozilla.org/en-US/docs/Glossary/blink_element https://developer.mozilla.org/en-US/docs/Glossary/blink_elem...
- chikere232 2y agoIt does visualise how big the difference is though
- igouy 2y agoCover up the single column of lang/secs and then try to read how big the difference is between java and php from the moving circles. You would have no problem doing that with a [typo histogram should say bar chart].
- MeetingsBrowser 2y agoCover the labels on the histogram and try to read how big the difference is between java and php....
- igouy 2y agoWe can read the relative difference from the length of the bars because the bars are stable.
- MeetingsBrowser 2y agoI can see the relative difference in speed between the two balls.
- tiffanyh 2y agoDart - I see it mentioned (and perf looks impressive), but is it widely adopted? Also, would have loved to see LuaJIT (interpreted lang) & Crystal (static Ruby like language) included just for comparison sake.
- igouy 2y agofwiw https://benchmarksgame-team.pages.debian.net/benchmarksgame/fastest/dartexe-dartjit.html https://benchmarksgame-team.pages.debian.net/benchmarksgame/...
- suby 2y agoIt looks like a more complete breakdown is here. Crystal ranks just below Dart at 0.5413 (Dart was 0.5295). Luajit was 0.8056. I'm surprised Luajit does worse than Dart. Actually I am surprised Dart is beating out languages like C# too. http://benjdd.com/languages2 http://benjdd.com/languages2
- igouy 2y agoMaybe that dozen lines of code isn't sufficient to characterize performance differences? Nearly 25 years ago, nested loops and fibs. https://web.archive.org/web/20010424150558/http://www.bagley.org/~doug/shootout/bench/nestedloop/ https://web.archive.org/web/20010424150558/http://www.bagley... https://web.archive.org/web/20010124092800/http://www.bagley.org/~doug/shootout/bench/fibo/ https://web.archive.org/web/20010124092800/http://www.bagley... It's been a long time since the benchmarks game showed those.
- saurik 2y agoDart's VM was designed by the team (I think not just the one guy, but maybe I'm wrong on that and it really is just Lars Bak) that designed most of the truly notable VMs that have ever existed: Self, Smalltalk Strongtalk, Java Hotspot, and JavaScript V8. It also features an ahead-of-time compiler mode in addition to a world-class JIT and interpreter, allowing for hot reload during development. https://en.m.wikipedia.org/wiki/Lars_Bak_(computer_programmer) https://en.m.wikipedia.org/wiki/Lars_Bak_(computer_programme... It was stuck with a bad rep for being the language that was never going to replace JavaScript in the browser, and then was merely a transpiler no one was going to use, before it found a new life as the language for Flutter, which has driven a lot of its syntax and semantics improvements since, with built-in VM support for extremely efficient object templating (used by the reactive UI framework).
- coliveira 2y agoThis kind of benchmark doesn't make sense for Python because it is measuring the speed of pure code written in the language. However, and here is the important point, most python code rely on compiled libraries to run fast. The heavy lifting in ML code is done in C, and Python is used only as a glue language. Even for web development this is also the case, Python is only calling a bunch of libraries, many of those being written in C.
- igouy 2y agoaka Python is as fast as C when it is C.
- knowitnone 2y agoI don't know. Ruby is able to call C too so it's a wash?
- chucke 2y agoThat's not true. Sure, many hot path functions dealing with tensor calculations are done in numpy functions, but etl and args/results are python objects and functions. And most web development libs are pure python (flask, django, etc)
- coliveira 2y agoFor performance, hot paths are the only ones that matter.
- IshKebab 2y agoSure, but only a small subset of problems have a hot path. You can easily offload huge tensor operations to C. That's the best possible case. More usually the "hot path" is fairly evenly distributed through your entire codebase. If you offload the hot path to C you'll end up rewriting the whole thing in C.
- coliveira 2y ago
- Alifatisk 2y agoWoah, Ruby has become fast, like really fast. What's even more impressive is TruffleRuby, damn!
- knowitnone 2y agoIt's Oracle https://github.com/oracle/truffleruby https://github.com/oracle/truffleruby Double Damn!
- Twirrim 2y agoIt's open source under Eclipse Public License version 2.0, GNU General Public License version 2, or GNU Lesser General Public License version 2.1. Making it easily fork-able should Oracle choose to do something users dislike.
- ksec 2y agoHoly! I know TruffleRuby is Open Source but I somehow always thought Graal ( Which TruffleRuby is based on ) wasn't open sourced.
- tiffanyh 2y agoNote that Rails doesn't work on Truffle and from what I understand, won't anytime soon. Which is disappointing since it has the highest likelihood of making the biggest impact to Ruby perf.
- uamgeoalsk 2y agoHuh, what exactly doesn't work? Their own readme says "TruffleRuby runs Rails and is compatible with many gems, including C extensions." (https://github.com/oracle/truffleruby https://github.com/oracle/truffleruby)
- tiffanyh 2y agoTruffle: TruffleRuby is not 100% compatible with MRI 3.2 yet Rails: Rails 8 will require Ruby 3.2.0 or newer https://github.com/oracle/truffleruby https://github.com/oracle/truffleruby https://rubyonrails.org/2024/9/27/this-week-in-rails https://rubyonrails.org/2024/9/27/this-week-in-rails
- Lammy 2y ago> There was a PR to improve the performance of `Integer#succ` in early 2024, which helped me understand why anyone would ever use it: “We use `Integer#succ` when we rewrite loop methods in Ruby (e.g. `Integer#times` and `Array#each`) because `opt_succ (i = i.succ)` is faster to dispatch on the interpreter than `putobject 1; opt_plus (i += 1)`.” I find myself using `#succ` most often for readability reasons, not just for performance. Here's an example where I use it twice in my UUID library's `#bytes` method to keep my brain in “bit slicing mode” when reading the code. I need to loop 16 times (`0xF.succ`) and then within that loop divide things by 256 (`0xFF.succ`): https://github.com/okeeblow/DistorteD/blob/ba48d10/Globe%20Glitter/lib/globeglitter/inner_spirit.rb#L215-L229 https://github.com/okeeblow/DistorteD/blob/ba48d10/Globe%20G...
- knowitnone 2y agoI'm a little surprised that Node is beating Deno. Interesting that Java would be faster than Kotlin since both run on jvm.
- entropicdrifter 2y agoI mean, the JVM's been optimized specifically for Java since the Bronze Ages at this point, it's not that surprising
- pjmlp 2y agoThat is one of the differences between a platform systems language, and guest languages. You only have to check the additional bytecode that gets generated, to work around the features not natively supported.
- wiseowise 2y ago“Faster”. > Ran each three times and used the lowest timing for each. Timings taken on an M3 Macbook pro with 16 gb RAM using the /usr/bin/time command. Input value of 40 given to each. Not even using JMH. I highly doubt accuracy of the “benchmark”.
- Someone 2y agoFTA: The loop example iterates 1 billion times, utilizing a nested loop: u = ARGV[0].to_i r = rand(10_000) a = Array.new(10_000, 0) (0...10_000).each do |i| (0...100_000).each do |j| a[i] += j % u end a[i] += r end puts a[r] Weird benchmark. Hand-optimized, I guess this benchmark will spend over 99% of its time in the first two lines. If you do liveliness analysis on array elements you’ll discover that it is possible to remove the entire outer loop, turning the program into: u = ARGV[0].to_i r = rand(10_000) (0...100_000).each do |j| a += j % u end a += r puts a Are there compilers that do this kind of analysis? Even though u isn’t known at compile time, that inner loop can be replaced by a few instructions, too, but that’s a more standard optimization that, I suspect, the likes of clang may be close to making.
- IshKebab 2y agoCompilers don't do liveness analysis on individual array elements. It's too much data to keep track of and would probably only be useful in incorrect code like this. I used to work on an AI compiler where liveness analysis of individual tensor elements actually would have been useful. We still didn't do it because the compilation time/memory requirements would be insane.
- hatthew 2y agoClosed form that works for most cases: result = ((u * (u - 1)) / 2 * (100000/u)) + (100000%u * (100000%u - 1) / 2) + r)
- smileson2 2y agoGame changing for my advent of code solutions which look surprisingly similar
- entelechy0 2y ago[dead]
- coliveira 2y agoAlthough being slow, Python has a saving grace: it doesn't have a huge virtual machine like Java, so it can in many situations provide a better experience.
- igouy 2y agoDoes JavaME have a "huge virtual machine" ? https://www.oracle.com/java/technologies/javameoverview.html https://www.oracle.com/java/technologies/javameoverview.html Do you mean CPython or PyPy or MicroPython or ?
- coliveira 2y ago> Does JavaME have a "huge virtual machine" Yes, compared to Python. > Do you mean CPython or PyPy Python standard virtual machine is called CPython, just look at the official web page.
- igouy 2y agoI imagine we need a nuts and bolts definition of "virtual machine" before we can make a comparison.
- acmj 2y ago[flagged]
- wly_cdgr 2y ago[flagged]
- kristianp 2y agoThe article refers to upcoming versions of Ruby. For the curious, looks[1] like ruby 3.4.0 will be released this Christmas, and ruby 3.5.0 next Christmas. Also, I'm wondering what effect Python's minimal JIT [2] has coming for this type of loop. Python 3.13 needs to be built with the JIT enabled, so it would be interesting if someone who has built it runs the benchmarks. [1] https://www.ruby-lang.org/en/downloads/releases/ https://www.ruby-lang.org/en/downloads/releases/ [2] https://drew.silcock.dev/blog/everything-you-need-to-know-about-python-3-13/#jit-just-in-time-compiler https://drew.silcock.dev/blog/everything-you-need-to-know-ab...
- riffraff 2y agoRuby is always released on Christmas, it's a predictable and cute schedule. But perf improvements can and do drop in point releases too, afair.
- tgmatt 2y agoAnother language comparison repo that's been going for longer with more languages https://github.com/niklas-heer/speed-comparison https://github.com/niklas-heer/speed-comparison.
- igouy 2y agoAnother language comparison repo with hard-to-read presentation. The chart axis labels and bar labels overlap each other, and there are no vertical grid lines. Oh for a simple HTML table!
- block_dagger 2y agoAfter all these years, I still love Ruby. Thank you Matz!
- resonious 2y ago> Python was the slowest language in the benchmark, and yet at the same time it’s the most used language on Github as of October 2024. Interesting that there seems to be a correlation between a language being slow and it being popular.
- remedan 2y agoDoes that correlation hold if you look at let's say the top 20 popular languages?
- sigzero 2y agoNo, because Java is #2, C++ is #4, C# is #7. People just really like Python for what it brings to the table.
- jb1991 2y agoSlower languages are higher level thus easier to use.
- igouy 2y agohttps://benchmarksgame-team.pages.debian.net/benchmarksgame/fastest/java-javaxint.html https://benchmarksgame-team.pages.debian.net/benchmarksgame/... Program performance is associated with the specific programming language implementation and the specific program implementation.
- jb1991 2y agoNo one takes that site too seriously when judging real world programming, for a variety of reasons.
- igouy 2y agoDo you really not understand that the exact same Java programs are likely to be 10x slower without JIT? Different language implementation, so different performance. > too seriously Take those measurements just seriously enough.
- ribadeo 2y agoIt seems odd to willfulky ignore Crystal language when discussing Ruby and speeding it up. Granted, macro semantics mean something else, more like c macros, but the general syntax and flow of Crystal is basically Ruby. https://crystal-lang.org/ https://crystal-lang.org/ Amber and Lucky are 2 mature frameworks to give Rails a run for their money, and Kemal is your Sinatra. https://docs.amberframework.org/amber https://docs.amberframework.org/amber https://luckyframework.org/ https://luckyframework.org/ https://kemalcr.com/ https://kemalcr.com/
- Alifatisk 2y agoWill these Crystal frameworks allow me to share a single standalone binary with peers that allows them run the web application locally?
- hamandcheese 2y agoCrystal is not Ruby. Full stop. It is not useful to anyone with an existing Ruby code base. Mentioning Crystal would be odd since it has nothing to do with the article.
- deleted 2y ago[deleted]