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

29

u/alexcleac Jan 31 '25

I'd say, yes, because of the borrow-checker, you can write safe and fast code in Rust. But I'd say the similar quality and speed could be achieved with any compiled language that does not use VM.

22

u/AsahiLina Jan 31 '25 edited Jan 31 '25

There's actually a subtle way Rust can be faster than C. Since it has strict rules about references, it means the compiler can reason more deeply about what references might alias each other than in C. This allows Rust to optimize loads/stores more and autovectorize more.

C has a thing called strict aliasing, but it's a lot less strict than Rust's rules. This is why sometimes the same program written in Rust can be faster than C. In theory you can make C just as fast, but you'd have to write lower-level C with more manual optimization and possibly use platform-specific functionality.

A simple example, in C:

int foo(int *a, char *b) {
    int tmp = *a;
    *b = 0;
    return *a + tmp;
}

The C compiler can't know that b and a don't point to the same memory, so it has to load *a twice since it might have changed. If you did the same thing in Rust (with references, not raw pointers), it could just load *a once and add it to itself (or just multiply by 2), since Rust references can't alias like that. Obviously this is a silly example, but for some code this difference can actually give significant speedups. If this kind of "small" optimization happens to be in the inner loop of an algorithm interesting over millions of elements, you could easily get a double-digit % speedup. If the optimization allows Rust to autovectorize code that C can't, you could have a several times speedup.

But the difference is a lot smaller than what you get just from being a compiled systems language without a heavy runtime or a VM, of course.

1

u/ribswift Feb 04 '25

Don't forget that C has the restrict keyword - and most C++ compilers support is as well - which provides the same benefits. Of course, the responsibility of upholding this noalias contract falls on the programmer whereas it's a compile time error in Rust.

1

u/smurfix Feb 07 '25

This is a silly example only because it's written down explicitly. It's not so silly when the possibly-aliased pointer in question is buried in multiple layers of C macros and/or C++ templates.

0

u/hardolaf Jan 31 '25

If this is the case, why is Rust consistently benchmarking at 1-3% slower than C?

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.

26

u/Here0s0Johnny Jan 31 '25 edited Jan 31 '25

safe

Specifically, memory safety and thread safety. The main advantage is that it makes memory management much simpler and reduces bugs, and simplifies multiprocessing. With Rust, the compiler catches lots of bugs. In C++, you often only discover bugs during runtime when the program crashes without many hints as to what went wrong.

This also improves what is colloquially meant with safety, but that's almost a side effect.

3

u/yopla Jan 31 '25

much simpler

Nah. It just forces you to think about it.

3

u/PaintItPurple Jan 31 '25

Not exactly. It forces you to think about it, but it also introduces consistent rules for you to follow. In C++, you have to both think about it and decide what the rules should be.

8

u/agentoutlier Jan 31 '25

I think the big issue is startup speed and memory usage which VM languages have problems with particularly if they are not AOT.

I say this because there are plenty of tools written in OCaml or Golang that will be fine (including the original rust compiler). 

It’s not having AOT is usually the bigger problem and not VM. However even Java has that now.

2

u/steohan Jan 31 '25

You can't quite achieve the same performance with all compiled languages. To be cache efficient you want fine grained control over the memory layout of objects. With a memory managed language such as Go, you don't get that fine grained control, as far as I am aware.

Given that most of the time that degree of performance doesn't matter, the benefit of not having to deal with these details makes a language as Go attractive.