6 ms·
JIT-optimized Ruby can outperform a C extension
- pmontra 3y agoTL;DR This is about pure Ruby code optimized by JIT vs manual optimization by writing C extensions (native code). There is Ruby code around those extensions but > The JIT cannot know what the native code will do to parameters (possibly mutate them), it cannot know what type of Ruby object will be returned, if the native code will look at the stack, or if it will raise an exception, etc. Thus the JIT is forced to have a conservative approach when dealing with native code, essentially forgetting much of what it had learned, and falling back to the most conservative implementation possible. Hence the JIT often makes pure Ruby code run faster than the code with C extensions. I think I heard the same argument many times with other JITed languages, interpreted or compiled.
- deleted 3y ago[deleted]
- xxs 3y agoAny managed language - java and go included, pays for trips to external, non-intrinsics calls. The standard way to deal is large portions/buffers to amortize the costs. This has been the case since the late '90s
- andrewstuart 3y ago"X outperforms C/C++/Rust/Golang". Only if the code design in C/C++/Rust/Golang is bad.
- nvy 3y agoFor the time being. But C on x86 hasn't been a good model of what the CPU is actually doing for a while now and eventually we'll have a better way to harness the computing power available to us.
- anonymoushn 3y agoimmintrin.h seems tough to beat.
- starbugs 3y agoC was never intended to be a model of what the CPU is actually doing. This is purely an optimization issue and the article compares apples to oranges for clicks.
- throwaway17_17 3y agoI want to be optimistic and agree that ‘we’ll have a better way to harness the computing power available to us’, but at this point it’s been years or maybe decades of propping up the x86 ISA while actual operation of the computing device drifts further away. I certainly understand the desires of processor manufacturers to want to be able to maintain an ISA and then be able to implement a machine that can change underneath it to realize performance gains. However, this has left the status quo utterly entrenched and I can not see what would make them change this behavior. I really would get onboard with an alternative ISA that was more in line with actual architectural features of modern processors and I often wonder why we could not have an extra compilation (jit or aot) step from x86 to some other format. But the trend seems to be going the opposite direction for all the large scale processor manufacturer/designers toward a more abstracted interface at the lowest level. I’ve seen various comments that basically boil down to “you can’t change the ISA because then C/C++/Rust programs would loose performance in benchmarks in the short term”, and this resistance only reinforces maintaining current trends. So… what are you assuming happens that prompts the shift toward a better way to harness computing power?
- anonymoushn 3y agoWhat architectural features would you like to surface that are currently papered over in x86?
- throwaway17_17 3y agoMy particular interests would see me appreciating access to cache structure, cache behavior, prefetching, cache coherency, memory management unit control, instruction pipelining, and maybe register renaming control. I am not particularly qualified to try and design a better ISA, I just know there are some things I would like the option to control at some times (or at least I imagine I would). A lot of the list above is about exposing more of the architecture to user control as a general principle. However, I’d rather see some areas of hardware design change to facilitate something other than coding paradigms and practices that stretch back nearly 40 years. Chisnall’s ‘C is not a Low Level Language’ article discusses several architectural directions I would like to see happen, but also talks about how as long as the ISA remains the same moving to new programming styles/philosophies continues to be a difficult proposition.
- anonymoushn 3y agoGolang's compiler is not great. The main way to achieve acceptable performance in golang is to use their weird asm dialect.
- umvi 3y agoGolang's compiler is so fast though that I can iterate much faster so I'm willing to accept slightly worse performance than C++. C++ I waste so much time just waiting (esp. if I'm using a lot of templatized header only libraries)
- john_the_writer 3y agoBut by that logic ruby is faster still since the compile time is 0. Less than zero really because it can be modified in flight.
- fredrikholm 3y agoThe trade offs between the two are huge. Go programs compile in less than a second, or several seconds for huge projects, where idiomatic Go gets you within the same order of magnitude to that of C(++), with some memory overhead. Concurrency is first class, GC pauses exist but are seldom an issue. Idiomatic Ruby runs several orders of magnitude slower, with a huge memory overhead, and is rife with runtime gymnastics. Concurrency is an expensive hack, with stop-the-world GC.
- Mawr 3y agoOnly if the compilation speed difference is worth the loss of performance. That seems unlikely for most use cases because the speed gain is small while the performance loss is severe.
- Kiuhrly1 3y agoSomehow I can't find a source for this but it's well known that Go prefers compilation speed over runtime performance. Doesn't help that they implement the entire compiler stack themselves so it definitely misses optimisations that other compilers can already do. Also, I've never heard of anyone using assembly for performance optimisations. It would never happen where Go in commonly used (server programming), maybe if you're doing graphics or something nonstandard?
- josephg 3y ago(Edit: my mistake, I misread the comment I was replying to assuming it claimed these languages themselves were equivalent.) Why would the performance of these languages be identical? These languages aren’t the same in lots of ways. Golang has a GC and gives you much less control over memory layout than any of the other languages you mentioned. It also has a very different concurrency model, which will vastly change the performance characteristics of multithreaded code. Rust is limited to llvm, while C can be compiled with many other compilers - some of which produce better code in many cases. (Eg icc) Rust has much better noalias analysis - which allows the compiler to make optimizations that aren’t available in general purpose C or C++ code. (Since nobody adds noalias to all their variables in C, for a variety of reasons). Unlike the others, C doesn’t support monomorphization (without disgusting macro tricks). Monomorphization cuts both ways with performance - since it bloats executables (and thus hurts the branch predictor and cache) while generating more efficient code. These languages aren’t the same. There’s lots of reasons they will perform differently both at the limits of what we can optimize and in the average case of everyday code. It’s wild that you would consider them to all be the same.
- PartiallyTyped 3y ago> Rust is limited to llvm, while C can be compiled with many other compilers - some of which produce better code in many cases. (Eg icc) There's a rust front-end for gcc as well.
- josephg 3y agoIs it ready for use? Last I saw it was far from feature complete.
- PartiallyTyped 3y agoAfaik the borrow-checker is missing - please correct me if I am wrong - but that doesn't stop you from using it. You could very well use `rustc` for the borrow-checker and compile with GCC on any other backend. The borrow-checker only expresses that your code is sound according the rust spec.
- deleted 3y ago[deleted]
- starbugs 3y agoI think this is a somewhat misleading title. It does not outperform C. This article appears to compare a suboptimal native C implementation to a JIT optimized version of some similar Ruby code. The JIT only outperforms that particular hand written C implementation in this case. Still, great that the Riby JIT can do this now, but the title would be better phrased as ‘Ruby JIT in some cases outperforms suboptimal hand written C code‘. That wouldn’t be good clickbait though.
- deleted 3y ago[deleted]
- fasterik 3y agoYeah, it appears that in the original benchmark an off the shelf C parser outperformed an off the shelf Ruby parser. Then they wrote their own parser in Ruby, which was faster. But for a fair comparison, you should also write the same parser in C and compare it to that. It's possible that writing it in Ruby is preferable if it avoids FFI overhead and can do more aggressive inlining. But this doesn't seem like an apples to apples comparison.
- kazinator 3y agoRuby outperforms C that is a Ruby extension, solving a problem by wrangling with Ruby types. When you write a run-time for a dynamic, high level language, and then library functions in C which use the API's and objects of that run time, the resulting code is reasonably fast, but never as fast as solving the problem using direct C idioms. There are ways to make improvements in that type of code, but they tend to be tedious to code. For instance, you can use split the code into cases by type, and then use faster, more type-specific routines. Sometimes it's possible to get all the inputs out of the objects, work with lower, less encapsulated level C representations to do the bulk of the problem, and then put results back into the run time's objects again. Or do that partially at certain steps. In the TXR Lisp mapcar implementation, alloca is used to create a native C array of stack-allocated iterator objects. (For the parallel iteration over multiple sequences: mapcar is an N-ary function!). The element values are pulled from all these iterators and stuffed into a stack allocated args structure destructively; that same structure is re-used for each call to the projection function. That's not as fast as it could be. Beyond that it would be possible to separately handle the case when there is only one sequence argument, and to split that by type, and other such approaches. It's faster, though, than if it allocated a dynamic vector of dynamic iterators, and/or if it built a dynamic list of arguments in each iteration which were applied to the function as a list.
- VeejayRampay 3y agoHacker News is going to nitpick at the bold claim of the title instead of focusing on the fact that it's a well written technical article that underlines some neat tricks that allow a rather slow, highly dynamic language such as ruby to provide great performance under some circumstances, which is all that engineers should care about
- thepostman0 3y agoClickbait titles gets criticism because some people won't read beyond the title
- starbugs 3y agoClickbait title gets criticism, because it makes bold claims drawing the attention of people who when reading the article find that their time was unfairly wasted, because said claims are a pile of ... Excuse my French.
- anonymoushn 3y agoThe fastest parser in the post works at 35MB/s which is ~100x slower than necessary, improved from a baseline of ~1000x slower than necessary.
- starbugs 3y agoCritizising the title is not exactly nitpicking. The title is important. And it’s misleading. I disagree that this article is well written. It is hard to identify what is compared exactly, at least as someone not deeply familiar with the ruby ecosystem. The article doesn’t do a great job of explaining the caveats and limitations of the comparison either.
- coldtea 3y ago>Critizising the title is not exactly nitpicking. The title is important. Only if one doesn't bother to read the article. Else the title is just an insignificant detail, or, at worse, a redundant piece of promotional material attached to the article.
- asah 3y ago"faster than C" - TBH, I kinda dread these clickbait posts because faster than C doesn't matter in 2023, where speed is dictated by multi-core and GPU. Python is abysmally slow, but provides access to GPU and (soon) multi-core.
- deleted 3y ago[deleted]
- mgaunard 3y agoWithout control, power is nothing. C gives you control.
- ahoka 3y agoIt doesn’t though.
- dvhh 3y agoWell it gives you the belief that you are in control, because undefined behavior are implementation dependent, and that "modern processor" not only predict, but also can decide of the execution order of your nice code. That said, save for the Basic language, C is probably the lowest common denominator for system programming and for all the programming languages to interface with each other (with relative efficiency).
- ahoka 3y agoEvery time you would have direct control over the hardware it’s considered UB and the compiler throws the code away.
- maccard 3y agoDid you see the post yesterday about the people who deployed a kubernetes cluster because it took 24 hours to parse a few GB of CSVs in JavaScript?
- 3y ago
- john_the_writer 3y agoI don't understand why we have to pretend Ruby could ever be as fast as C. Or faster. That's like saying a jockey is faster than their horse. Ruby is riding on C, so it cannot be faster. Also why even bother. Ruby is fast enough for the job it does. It's higher level and makes things that are "harder" in C, easier. Dev time is less, and thats where we win. We take the win, and accept what it is.
- deleted 3y ago[deleted]
- michaelteter 3y agoI think the point of the essay is that we shouldn’t immediately assume that going to C is the best way to speed up part of a Ruby system. It might be conventional wisdom to break out to C for the speed gain, but a purpose made, hand written piece of Ruby, with JIT optimizations, and be a better/faster solution.
- vidarh 3y agoSome Ruby implementations are "riding on C". Others are not, e.g. Truffle Ruby. While beating C overall would be very hard because of the dynamism of Ruby, most Ruby code is not very dynamic after a short warmup (loading for most code; connecting a database etc. for code using a dynamic ORM), and at least on specific code it's at least theoretically possible for a Ruby JIT to beat C by optimising to the actual runtime configuration and evaluate out all the dynamism that costs Ruby performance. TruffleRuby goes part-way there, in terms of guarded section that makes assumptions about types but where the guards shunts code that breaks those assumptions onto a slow path, but a Ruby JIT or a combination of a bytecode compiler and JIT, can at least in theory go much further. E.g. a whole lot of the Ruby code library contains type checks that a compiler could hoist and/or elide. Yjit is a baby step, but a good start.
- rolae 3y agoPeople say the title of the article "Ruby Outperforms C: Breaking the Catch-22" is misleading, which is true, this is about Ruby code optimized by JIT outperforming a extension written in C. But to give some context: the author Aaron Patterson is a Ruby and a Rails core team member. The article and headline is clearly targeting the ruby community, where this article has been very well received. I think it's a good title for the intended audience. The post clarifies in the first section: > In this post I’d like to present one data point in favor of maintaining a pure Ruby codebase, and then discuss some challenges and downsides of writing native extensions. Finally we’ll look at YJIT optimizations and why they don’t work as well with native code in the mix. edit: added original title of the hackernews post / article
- wonnage 3y agoThis is specifically about breaking the myth that performing expensive self-contained operations (e.g, parsing GraphQL) in a native extension (C, Rust, etc.) is always faster than the interpreted language. The JS ecosystem has the same problem, people think rewriting everything in Rust will be a magic fix. In practice, there's always the problem highlighted in the post (transitioning is expensive, causes optimization bailouts), as well as the cost of actually getting the results back into Node-land. This is why SWC abandoned the JS API for writing plugins - constantly bouncing back and forth while traversing AST nodes was even slower than Babel (e.g https://github.com/swc-project/swc/issues/1392#issuecomment-877628316 https://github.com/swc-project/swc/issues/1392#issuecomment-...)
- mananaysiempre 3y agoInteresting that both of the points you state completely contradict my experience with LuaJIT. Parsing has always been one of the things its tracing JIT struggled with; it is still faster than the (already fairly fast) interpreter, but in this kind of branch- and allocation-heavy code it gets nowhere near the famed 1.25x to 1.5x of GCC (or so) that you can get by carefully tailoring inner-loopy code. (But a tracing JIT like LuaJIT is a different from a BBV JIT like YJIT, even if I haven’t yet grokked the latter.) LuaJIT’s FFI calls, on the other hand, are very very fast. They are still slower than not going through the boundary at all, naturally, but that’s about it. On the other hand, going through the Lua/C API inherited from the original, interpreted implementation—which sounds similar to what the Ruby blog post is comparing pure-Ruby code to—can be quite slow. The SWC situation I can’t understand quickly, but apart from the WASM overhead it sounds to me like they have a syntax tree that the JS plugin side really wants to be GCed in the GC’s memory but the Rust-on-WASM host side really wants to be refcounted in WASM memory, and that is indeed not a good situation to be in. It took a decade or more for DOM manipulation in JS to not suck, and there the native-code side was operating with deep (and unsafe) hooks into the VM and GC infrastructure as opposed to the WASM straitjacket. Hopefully it’ll become easier when the WASM GC proposal finally materializes and people figure out how to make Rust target it. In any case, it annoys me how hard it is in just about any low-level language to cheaply integrate with a GC. Getting a stack map out of a compiler in order to know where the references to GC-land are and when they are alive is like pulling teeth. I don’t think it should be that way.
- chucke 3y agoThis article outlines well the paradox that JITs require to be truly more efficient: if more of the target language is available to optimize, it'll get waaay more optimized, compared to dropping down to the layer below and try to hand-stitch it. Of course, there is massive overhead in doing so. Just look at go, which had to rewrite practically everything already available in go, and must always require a native implementation (protobuf for example shares the underlying interface across ruby, python, php... but then has a full separate implementation in go, and java I think). And they have the budget for it at least, Google won't let go die under the overhead it created for itself. So definitely, write more ruby, enough of those "fast-C gem - rewritten as C extension", but still keep using low level libraries like libpq.
- grncdr 3y agoIt’s funny you mention libpq, because my first thought upon reading the article was “I wonder if a pure Ruby implementation of the postgres wire protocol could possibly lead to performance improvements?
- danmur 3y agoThis pure Python library claims quite fabulous performance: https://github.com/MagicStack/asyncpg https://github.com/MagicStack/asyncpg I believe it because that team have done lots of great stuff but I haven't used it, I just remembered thinking it was interesting the performance was so good. Not sure how related it is to running on the asyncio loop (or which loop they used for benchmarks).
- chucke 3y agoI'd assume that the benchmark is showcasing the gains of the async model, more than making a point that native python is faster than C. The issue is ultimately: how much of the functionality available through libpq is not yet backported to asyncpg (i imagine it's nonzero, but Idon'tknow the answer). Which is why the pragmatic in me still prefer a middle layer like libpq in between.
- whizzter 3y ago
- iamgopal 3y agocan numba and typed python will have similar effect ?
- KeplerBoy 3y agosure, in cases where Numba works well, it certainly is faster than a badly written C extension. It can however not be faster than a well written C extension. They're all just LLVM frontends.
- eutropia 3y agoThe point here is simplifying the codebase by removing an extra language. Rubyists like writing ruby, not C extensions. And using the JIT allows us to simplify and keep enough performance.