9 ms·
Our Experience Porting the YJIT Ruby Compiler to Rust
- ncmncm 5y agoThe author claims to know C++, but remarks suggest she has not learned much, if anything, of it since 1998. It is possible that her team is similarly not aware, so that it wouldn't matter if she was.
- IAmEveryone 5y ago
- metadat 5y agoAre people only allowed to claim they know a language when they keep up with the bleeding edge and every new revision as they are released? Your post comes across to me as more a thinly veiled insult than any sort of useful insight.
- snovv_crash 5y agoC++ had a major change of direction with C++11, to the extent that a lot of previous design patterns and constraints are now completely obsolete. C++98 compared to C++20 is probably equivalent to Python 2.2 vs 3.9, and someone wouldn't claim to "know python" if they only used 2.2.
- dureuill 5y agoWhile true, I believe it doesn't significantly change the tradeoff between Rust and C++. Also, nothing in the post lets us infer that the author doesn't know C++11 and later.
- ncmncm 5y agoYou can believe whatever you like, but the fact is that changes in C++11 and C++20 change the tradeoff between C++ and Rust in fundamental ways. The only way anyone could believe otherwise would be by knowing precious little about the topic.
- dureuill 5y agoWhile I did not use C++20, I did follow its new feature set, and did not see anything able to address the issues of C++: - no way to express "lifetime relationships", they remain in comments or force you to work around the problem by using copied objects or worse, shared pointers. This resulted in very real bugs in our code due to a library returning structs that had to live shorter than some other object, without it being clear from the name of the struct. Coincidentally, this same library has a rust implementation, where keeping instances of this struct for too long is a compiler error. - no blessed way to express thread safety in the type system - terrible build system without a blessed package manager, which is not about to change because: - they are still headers everywhere. While modules are technically in C++20, tooling is not ready today (save for MSVC). The module implementation is also dubious in that it seems to cater to any niche use case in existence which adds a lot of complexity (and new opportunities for ODR violations...), instead of going "clean slate" and providing a simple system (preventing the split between declaration and definition and not being orthogonal to namespaces would have been a good start) - no ergonomic sum types. Sum types are very important to model business logic and it is hard to go back to languages lacking them. There are discussions of pattern matching for C++ but it's not in C++20. Several of these issues are impossible to fix in an existing language without breaking an unreasonable amount of existing code, so I don't see future versions of C++ reversing the statu quo. I also don't really trust the committee to achieve simple, ergonomic solutions, when I look to eg what was done with modules or coroutines (here I heard that the solution is to use third party libraries, but while it is tractable in Rust it is much less so in C++ due to the lack of satisfying tooling). You're welcome to elaborate if you disagree though.
- ncmncm 5y agoIn modern C++, you get the same benefits as are claimed for "lifetime annotations" by operating at a higher level. In effect, a library may adopt responsibility for what the Rust compiler would have enforced, and where it matters, they routinely do. This is no substantial burden. As a result, it has been a very long time since I last encountered a lifetime problem in my C++ code. Sum types work fine in C++. Pattern matching is an extra notational convenience on top of them. Rust has many nice convenience features, but lacks numerous other powerful features C++ provides. But this is not a good place to go into detail. If you care, there is plenty of material online. The point remains that comparisons, if done at all, should be done based on knowledge about current facts, not suppositions and hearsay.
- ncmncm 5y agoYes. People are "only allowed to claim" to know about the merits of a language that they actually know. C++20 is the C++ to compare against, unless she wants to compare against, say, Rust as it was in 2014. C++98 is simply not an honest basis for comparison. I don't need "thinly veiled insults" when the facts speak for themselves. The author had no need to mention C++ at all. Then, she would not have found herself making ignorant comparisons.
- tialaramex 5y ago> C++20 is the C++ to compare against Admitting that each ISO standard is a distinct language (so, C++ 20 is distinct from C++ 17 and C++ 03 and so on) and insisting upon naming them this way would of course satisfy this concern. But doing so would also highlight the uncomfortable reality that C++ 20 isn't a real language programmers can use yet - compiler vendors are still implementing this standard and won't be done for a while. It would also highlight the fragmentation, you would need to pick one of these different C++ languages to write some software, and in doing so rule out using the others. And that's a pretty poor alternative to Rust, which is one real language Maxime and her team were able to use to write this software.
- metadat 5y agoncmncm, what is your specific definition for "actually" knowing a programming language? If it's something like "mastery over most or all facets and nuances", perhaps there are a few thousand human beings in total who meet this qualification. Across all programming languages across the entire Earth.
- ncmncm 5y agoTrolling is generally unwelcome here.
- tialaramex 5y agoWhich remarks in particular made you think this?
- dureuill 5y agoI find your remark strange: the author is mainly comparing the scalability of Rust vs C. C++ does provide tools to build scalable software, and was already doing so in 1998 (few new features on this front since 98 I guess? Maybe modules, but using them for portable development in 2022 looks difficult given their state in compilers that aren't MSVC). I don't think this is a reason to prefer C++ over Rust (if anything, the tools for encapsulation and modularization provided by C++ are way less convenient and efficient than Rust's--speaking from experience of C++14 and Rust here). Also C++ has the massive drawbacks of lacking ADT, forcing headers on users (which is elaborated in the post I believe) and not offering a "safe mode" like Rust does (the tooling is also massively better, but somehow cargo felt like a hindrance to the author of that post, so there's that). It did surprise me that the author wouldn't elaborate on why C++ was not chosen, when her argument made it look like it could provide some of the advantages she found in Rust. However, she doesn't *have to* elaborate on every possible subject in that post, and implying she doesn't know the language (anymore) because of her not focusing on the reasons she didn't use it looks like a dubious stretch.
- ncmncm 5y agoThe author does, in fact, bring up C++ herself. It is very strange to have dismissed it while knowing so little about its current status, and so worth bringing up. She didn't have to mention C++ at all. If you don't see C++20 as a very, very different language from C++98, it can only be because you know practically nothing about the topic. That is totally allowed, but expressing opinions based on knowing nothing produces worthless opinions.
- faitswulff 5y agoI wonder what the author would have preferred to the visual litter of `unsafe` blocks? Can you use `unsafe` to mark off entire scopes to avoid having to redeclare it? Seems unavoidable in a project that is meant to heavily interoperate with a C99 code base like Ruby. Cargo having to phone home all the time sounds annoying, though. I hope that gets improved, though I know the cargo team is swamped right now.
- steveklabnik 5y ago> Can you use `unsafe` to mark off entire scopes to avoid having to redeclare it? Yes. > Cargo having to phone home all the time sounds annoying, though. It does not have to phone home, ever. They for some reason didn't seem to want to use the right workflow, but that workflow does exist, and is an important and well-supported use-case. (It seems from the bug report that they did hit a confusing error message, which is unfortunate.)
- faitswulff 5y agoThanks for the clarifications!
- steveklabnik 5y agoNo problem. And I am serious about the "for some reason" bit, I don't know why `cargo vendor` isn't acceptable for them, but they know their requirements better than I do.
- tptacek 5y agoShe gave the reason in the piece, and linked to the ticket she filed: https://github.com/rust-lang/cargo/issues/10352 https://github.com/rust-lang/cargo/issues/10352
- steveklabnik 5y agoI read the piece and the ticket. I still don’t understand. That’s fine, it’s not my responsibility to anymore. I’m glad they found a solution they’re happy with. Specifically, cargo vendor was suggested in the ticket, but there was no response as to why it wasn’t adequate for this case, just that it doesn’t “feel” good. I’m probably missing something.
- VWWHFSfQ 5y ago> Unlike C, Rust won’t automatically promote integer types to wider types. It forces you to manually cast any mismatching integer types for every operation > Rust's insistence on manual casting everywhere encourages people to write inefficient code, because writing verbose code feels uncomfortable and adds friction. This reads a lot to me like "we like how easy it is to create bugs in C. We'd like to be able to do that in Rust, too." But maybe I'm misunderstanding. I'm sure these people know a lot more about the consequences of implicit integer casting than I do.
- colejohnson66 5y agoYes, but: > ..there's no reason why you couldn’t safely promote a u8, u16, or u32 into a usize, and asking programmers to manually cast integers everywhere makes the code more noisy and verbose. They have a valid point. In C#, numerical casts that won't lose precision are implicit, and any that could are explicit. I think that’s a nice compromise. So, int to uint requires a cast, but int/uint to long doesn't: int myInt = /* ... */; long myLong = myInt; uint myUInt = myInt; // error CS0266: Cannot implicitly convert type 'int' to 'uint'. An explicit conversion exists (are you missing a cast?) ulong myULong = myInt; // error CS0266: Cannot implicitly convert type 'int' to 'ulong'. An explicit conversion exists (are you missing a cast?) uint myUInt = /* ... */; int myInt = myUInt; // error CS0266: Cannot implicitly convert type 'uint' to 'int'. An explicit conversion exists (are you missing a cast?) long myLong = myUInt; ulont myULong = myUInt;
- stonemetal12 5y agou8 max value + 1 means something as a u8. u8 max value + 1 means something different as a u16. Implicitly changing behavior is never cool.
- colejohnson66 5y agoThat wouldn’t be an issue. The whole claim is that this: let a: u8 = /* */; let b: u16 = a; push_u16(a); can easily be inferred into: (no pun intended) let a: u8 = /* */; let b: u16 = a.into(); push_u16(a.into()); But the Rust language has decided that implicit upconversion that is already apparent is a bad idea because “bugs”. And I’d agree with them, but it’s stupid in this case. The issue at hand isn’t asking for ‘a’ to be upconverted to a u16 in the middle of an expression like this: let b: u16 = a + 1; …because you’d be left with the question of when to upconvert (“implicitly changing behavior” as you call it). No, it would only be when it shouldn’t need to be said such as a lone assignment or function call. And if the Rust language’s argument is that it’d be unclear when it’s allowed? Because I’ve seen many RFCs where someone does what you just did: “what about this weird corner case where ‘u8 max + 1’? Should that be a u8 or u16? Maybe we should toss the whole proposal!” To that I’d say: start off where it’s clear (like my examples above). It’s not a backwards incompatible change. /rant
- epage 5y agoGlad to see an explanation for why they bypassed cargo. I had wondered about that when the PR was posted as it didn't contain any documentation for why they were going off the beaten path. However, I'm a bit confused at the reasoning. Because a solution felt sub-optimal (vendoring an optional dependency), they went the route of invoking rustc directly? That seems a bit scorched earth especially when they later talk about needing another dependency offline and instead of vendoring both they instead worked around that in a different way.
- Thaxll 5y agoOnly 3month for such a project is impressive, how big was the original C code base?
- vlang1dot0 5y agoAccording to the article: > YJIT is a relatively simple JIT compiler that totals about 11,000 lines of C code.
- SemanticStrengh 5y agothe rust port is 16K LOCs despite the crazy C verbosity?? (I'm reffering to the github addition count)
- ori_b 5y agoSounds about right. Well written C just isn't very verbose. You can make it verbose by adding a ton of patterns that you don't need, like the gobject code in gnome. But you don't need to.
- SemanticStrengh 5y agostrong disagree, C is a very poor language that does not provide a proper stdlib to reuse, collections/containers/functors. No OOP means no efficient code reuse, zero syntax sugars, manual memory noise, etc etc C toy programs can be short though.
- ori_b 5y agoDisagree all you like. The rust port apparently didn't hear you, because the line count grew by nearly 50%, and I can post other similar comparisons with some other codebases and languages too.
- vlang1dot0 5y ago
- ibraheemdev 5y agoThe rewrite commit is here for anyone interested: https://github.com/ruby/ruby/pull/5826 https://github.com/ruby/ruby/pull/5826
- steveklabnik 5y agoI've been tickled pink to watch this work happen. A wild fusion of my past and my present. And of course, much cleaner and properly done, as opposed to fun experiments I used to like to do, like https://github.com/steveklabnik/ruby/tree/rust https://github.com/steveklabnik/ruby/tree/rust
- rubyskills 5y agoHaha, love it. Both of your passions melded together! (We met in Michigan at our mutual rust conf btw)
- throwaway38311 5y ago
- avgcorrection 5y agoYou might recognize the author from https://pointersgonewild.com/ https://pointersgonewild.com/
- SemanticStrengh 5y agoI hope shopify will keep investing in the disruptive truffleruby
- onebot 5y agoI honestly love Ruby and Ruby on Rails, but I can't understand why companies like Shopify and Github go through so much effort to scale Ruby especially at their size. Maybe I am wrong, but couldn't this effort be put to rewriting parts of it in a more performant language like Go or Rust? One has to imagine that they have a large code base, how much developer time is spent writing Tests for Ruby? How much time was spent debugging odd monkey patching gems over the life of the codebase? I do get that developer time was/is more expensive than servers. But I am not so sure at some level of scale. When you need 100 servers vs 5, and need to spend so much testing effort dealing with dynamic language, etc. And then you build custom compilers, special tools for tracing, entire architectures to deal with single threaded model, etc. Between Github & Shopify alone, they could have probably build a very Ruby on Rails like framework on a language more suited to the size and scale of these platforms.
- IAmEveryone 5y agoThe "parts of it" in your question might be the clue to an answer: you start implementing some often-used code path only to realize that they share code with all the rarely used code paths (permissions/authentication come to mind), So you'd need to reimplement a whole bunch of your codebase, at which point you'll have two versions of everything that you need to keep in sync.
- ta988 5y agoThat's exactly why most COBOL rewriting projects in banking have failed miserably. It is much more complex than just rewriting a complex CRUD app. It is not just about the main program, it is also everything that connects to it and expect it to behave in super specific ways.
- petercooper 5y agoThis isn't a counterargument as such, but you could apply similar logic to, say, banks and COBOL? I'm not in banking, but I imagine there are many good reasons to keep writing and maintaining COBOL, and these companies may have similar stories to tell about Ruby.
- newbye2 5y agoPerhaps this is a silly reasoning, but my first impression is that a that a good jit compiler requires a fast compiler, so golang meet that requirement better than Rust, obviously Rust has a more advanced/featured compiler but in a jit compiler for ruby the optimizations are going to be mostly for short parts of code (no heavy math or long loops), so the jit could try to find places where the code use vectors, maps or list of same type elements to avoid having to determine the dynamic type. That is, my impression is that in web related tasks the greater benefit comes from detecting parts of code that can be transformed in static type code and then apply a fast compiler to that part. But perhaps Rust was chosen by its security model rather than by its performance, in which case I agree. But I think that a variant of go using borrow/owner var semantic could be the best track to construct a great YJIT.
- carlmr 5y agoAs far as I understand it the JIT compiler is written in Rust, that doesn't mean that it produces Rust. So the output of the JIT can be in assembly, C or something else that compiles fast and runs fast.
- pizza234 5y agoI haven't had a look at the YJIT design, but your reasoning: > detecting parts of code [...] and then apply a fast compiler to that part applies to the previous JIT (MJIT) design, which created C snippets and compiled them on the fly. I don't think this is the case for YJIT, although I'm not 100% sure. Surely this isn't the case, for example, for TenderJIT, which, for the bytecode instructions, builds assembly code and (optionally) invokes the original interpreter functions.
- deleted 5y ago[deleted]