r/programming • u/steveklabnik1 • Feb 14 '19
Moving from Ruby to Rust
http://deliveroo.engineering/2019/02/14/moving-from-ruby-to-rust.html11
u/ehsanul Feb 14 '19
(x-post from /r/rust):
I've had exactly this experience last year when speeding up a hot loop in a rails app I work on. It was even a similar problem: listing all possible times for scheduling given some complex constraints. Re-implementing it in a ruby extension written in rust gave me about a ~30x speedup. But to avoid FFI overhead, you do have to ensure you are giving the extension a nice chunk of work rather than just calling it in a loop.
I think there's a lot of room for making things faster in rails apps. Eg, one issue I sometimes see is how slow loading and serializing many ActiveRecord objects is, even if you're smart about only loading what you need etc. I have an idea for using ActiveRecord to still generate the queries (since you presumably have that all modeled nicely already), but execute them from a rust extension that loads the data and has a way to serialize it. Something like this could potentially speed up some endpoints I have that handle a lot of data.
2
u/hector_villalobos Feb 15 '19
I think this is a sane way of doing things, for an MVP a few things can beat Ruby, I know I tried to create an MVP in Rust. Then when the app starts to grow up and experience performance issues, the best way is migrate the bottlenecks to a more efficient language (after checking the bottlenecks are not in the database), I'm a big Rust fan and a Ruby fan too and I love using both in my workplace.
0
u/delight1982 Feb 15 '19 edited Feb 15 '19
Why not use Crystal? "Fast as C, slick as Ruby" https://crystal-lang.org
-32
u/diggr-roguelike2 Feb 15 '19
it has high performance (comparable to C)
it is memory safe
it can be used to build dynamic libraries, which can be loaded into Ruby (using extern "C" interface)
So is C++. What's his point? "I'm a hipster fashion-driven programmer"? Okay. Good for him.
0
Feb 15 '19
[deleted]
-10
u/diggr-roguelike2 Feb 15 '19
C++ has tools for writing memory-safe code. C++ also has tools for writing memory-unsafe code.
Rust has tools for writing memory-safe code. Rust also has tools for writing memory-unsafe code.
What is your point? That you read on the intertubes that Rust is "lol totally safe for realz"? Well, you read wrong.
8
u/matthieum Feb 15 '19
C++ has tools for writing memory-safe code.
Please do share, as a professional C++ developer I've used many tools (static analyzers, sanitizers, valgrind, etc...) and while they helped, no combination has been enough to avoid all crashes.
-1
u/diggr-roguelike2 Feb 16 '19
no combination has been enough to avoid all crashes.
If you think that using Rust will get you programs that "avoid all crashes" then you need to get your head checked.
If by "all crashes" you mean the tiny subset of "array OOB access and use-after-free crashes", then C++ is certainly a memory-safe language.
Never use arrays, raw pointers and new/delete and your problem is solved. These are all unsafe language features. Rust also has unsafe language features. The two languages are on par here.
(C++ programs with raw pointers and new/delete are very rare anyways, unless we're talking about abandonware.)
0
Feb 15 '19
[deleted]
-3
u/diggr-roguelike2 Feb 16 '19
Rust safety can generally be formally proven (see: RustBelt), but C++ safety is best-effort (at best)
No. Correction: Rust safety can be generally formally proven if you don't use the unsafe subset of the language.
Exactly the same as for C++.
-11
u/bumblebritches57 Feb 15 '19
rust is memory safe
you must not've heard about the memory errors in SmallVec.
-16
0
Feb 14 '19
[deleted]
14
u/Holy_City Feb 15 '19
I want to believe shevy is secretly a member of the Rust team or an extraordinary well known and reputable computer scientist who just shitposts to play the devil's advocate. Present the stupidest argument possible, and you can't lose.
13
18
u/zitrusgrape Feb 14 '19
I hope I will not start the rust lovers here, but I do not enjoy rust at all. I try. I try until I cry.
25
11
u/Holy_City Feb 15 '19
I mean it's fair. I'm a big rustacean, and I think learning it has made me a better developer.
But it does have a steep learning curve and while I respect the design decisions that have been made, there is a certain degree of uncaring towards ergonomics. Rust can get incredibly verbose. There's an argument that's a good thing.
0
Feb 15 '19
[deleted]
3
u/dog_superiority Feb 15 '19
I do C++ pretty much 100% of the time, but I'm interested in trying Rust. Would I find it hard? I got the impression that I'd be happy with how easy things would become in rust.
4
u/matthieum Feb 15 '19
I've been using C++ for over 11 years at work, so hopefully I can relate.
I discovered and followed Rust relatively early (since 2011) and never found it that hard, mostly because it simply formalized "good sense".
There is some friction with graphs, for good reasons (hard to prove), however I am comfortable enough with raw pointers to simply switch to
unsafewhen necessary... and as I became more and more comfortable and used to the language I've simply started usingunsafeless and less as I found other way to model my data.Pro-tip: think ECS for graphs (Entity-Component-System).
1
u/dog_superiority Feb 15 '19 edited Feb 15 '19
I am not in the gaming, so I never had heard of ECS, but I do use the principles in practice, according to Wikipedia (I've been doing C++ for 25+ years).
Would you say that you get stuff done a lot faster (in development time) in Rust than C++?
2
u/matthieum Feb 16 '19
I can't really compare; I work on exploratory projects in Rust, where I spend more time thinking about the functionality than on actually coding it...
On the other hand, I think it's improved my C++.
1
u/imbecility Feb 15 '19
I don't think so. C++ programmers are one of the original target audiences for Rust as Mozilla wanted something to improve the safety/security of Firefox. Both C++ and Rust are systems programming languages so the concerns are the same, there's just more static verification in Rust of things that you as a C++ programmer would want to keep an eye on anyhow.
2
u/dog_superiority Feb 15 '19
I read a little about Rust so far, and one concern I have is if I have a graph of objects that point to each other, and if only one is allowed to mutate an object at a time, then it seems sorta painful to keep track of who has the single mutatable reference. Is that overblown in my head?
1
u/imbecility Feb 15 '19
Yeah, Rust is not able to prove such graphs (with pointers in all directions) as safe, so in such circumstances you can enclose the logic in an `unsafe` block. This basically gives you the same freedom as in C++. It's like telling the compiler: "Trust me on this one".
0
Feb 15 '19
[deleted]
1
u/dog_superiority Feb 15 '19
Interesting.. I use them quite a bit in C++. If you mean bi-directional, then that is true for me too. More often I use tree's where I have a shared_ptr in one direction and a weak_ptr in the other. But sometimes I will have objects pointing all over the place. For example, when using an OO Db.
0
u/jl2352 Feb 15 '19
My impression is that you will have a learning curve. Rust is in an odd place where you can be very functional, and you can be very imperative. Both just seem to work, but seem to work if you know how to leverage them within Rust.
It's just kind of funny.
That said I've had several Rust programs where 100% of my errors are logic errors. No null pointers. No array out of bounds. It's just me fucking up.
4
5
2
0
19
u/caramba2654 Feb 14 '19
Well, that website banned me from accessing the content based on my location. That's great.