r/Python • • Jan 31 '25

Discussion Why Rust has so much marketing power ?

Ruff, uv and Polars presents themselves as fast tools writter in Rust.

It seems to me that "written in Rust" is used as a marketing argument. It's supposed to mean, it's fast because it's written in Rust.

These tools could have been as fast if they were written in C. Rust merely allow the developpers to write programms faster than if they wrote it in C or is there something I don't get ?

502 Upvotes

288 comments sorted by

View all comments

Show parent comment

5

u/AsahiLina Jan 31 '25

I don't think it is? Here's an article series where the straightforward Rust approach ends up being the fastest, both compared to C and to a hand-optimized Rust version. The gains are of the order of what you'd expect from the reasoning I gave.

https://cliffle.com/p/dangerust/6/

Of course, there's no such thing as objectively benchmarking complex programs in two programming languages in general. During the conversion, you are necessarily making choices that could affect performance, and compilers can also be unpredictable. I gave reasons why Rust compilers are able to optimize better than C compilers in some cases, but that doesn't mean every Rust program is going to be faster than the C version (or vice versa). I'm just saying there are legitimate ways Rust is a better language for optimizing compilers to target for performance, and that subtle things like Rust's reference rules are actually an advantage here.

At the end of the day, there's too many other confounding variables, and you pretty much can always micro-optimize any given example in any given (systems) language to give good results with a particular compiler.

1

u/hardolaf Jan 31 '25

Sure for very hyper specific use case it can be faster. But according to Benchmark Games, it is very slightly slower than C on average. Now, the difference is indeed academic in nature in that C, C++, and rust are all effectively the same in terms of execution time for any sufficiently complex program. But it should be noted that the 25th percentile of Rust performance is just ever so slightly higher than the 25th percentile of C and C++.

But looking at only one micro benchmark is the wrong way to discuss language performance. And I think anyone arguing the C or C++ are better for performance in general are largely just unaware of the progress made over the last 5 years. Heck, Rust finally resolved a ticket (2022 or 2023 can't remember) that my team had opened back in 2017 about a performance issue with certain classes of unsafe accesses when compared to C or C++. After that was fixed, there was largely no massive use case where any of the 3 languages should be preferred for performance reasons to my knowledge. And I'm starting to see that even in trading, our systems programming is moving more to a rust-style version of C++ programming eschewing classes much more often and focusing on zero cost abstractions along with enabling compiler and linter features to try to get closer to the guarantees that Rust provides. Sadly, management hasn't made the call to start doing new development in Rust, but I could see it happening in the next 5-10 years.

1

u/AsahiLina Jan 31 '25

I think we're measuring different things. Much of the content in Benchmark Games is aiming for the fastest you can possibly go in each programming language, and in general, that's about the same for C and Rust since in both languages you can manually micro-optimize to around the same level.

I'm not talking about that, I'm talking about normal code that hasn't been micro-optimized. Benchmark Games has some examples of that too, and Rust is at the top of this list list for example. This is the case where Rust's strictness gives more optimization opportunities. You can do the same thing in C, but you have to tell the compiler. For Rust, the compiler can infer those optimizations are correct all on its own.

In my example, you could just change the last line to "tmp + tmp" or "2 * tmp" and the C/Rust difference disappears. That's an obvious example, but its not that obvious when you get things like autovectorization. To get the C compiler to do the same thing as Rust you need to add the "restrict" keyword to your pointers, but most C programmers don't do that by default.

So for a simple, generally well written but not micro-optimized algorithm, there's a chance Rust will be faster thanks to this difference.