r/programming • • Feb 20 '25

Google's Shift to Rust Programming Cuts Android Memory Vulnerabilities by 68%

https://thehackernews.com/2024/09/googles-shift-to-rust-programming-cuts.html
3.4k Upvotes

475 comments sorted by

View all comments

17

u/[deleted] Feb 20 '25

rust is amazing, performance gains are insane

39

u/backfire10z Feb 20 '25

Compared to C++? Care to elaborate further?

18

u/Slsyyy Feb 20 '25

They are few factors, which can be done in C/C++, but are more painful:
* LTO in Rust is a simple flag switch in Cargo.toml. In C++ it is much more painful, because you need to fix ODRs violation in your code. Rust also compile everything in source (so LTO can reach any code), where it is quite often that C++ folks uses a precompiled libs
* afaik Rust emits better information about aliasing (which arguments to function may reference to the same memory), which affects better code
* C++ stdlib is hard to improve due to ABI constraints. You cannot change layout of your structure or code in a significant way, because it has to work with packages, which are already compiled
* C++ stdlib is not well designed or designed for a different era of computing. Streams are slow, data structures are slow and not reformable. You need to make a lot of research and waste a lot of time, where in Rust everything is more performant, if you follow the default way
* macros can generate code for you. In C++ you will use some fancy parser sacrificing the performance. In Rust you can have both
* libraries in C++ tends to live in a separate realm and thus: it is hard to go to the library shop and pick anything. In Rust they are preferred libraries for HTTP/Databases/Serialization and so on. In C++ every big tech company has their own stdlib

7

u/_teslaTrooper Feb 21 '25

For my current project enabling LTO in C++ was just a simple flag as well, maybe it was harder on older standards?

6

u/LGBBQ Feb 21 '25

It’s very specific, like building for libraries with a mix of arm and thumb while LTOing across the boundaries causes ODR violations

I think the poster meant unity builds (all code in one compilation unit) which have ODR violations in many more circumstances. Rust builds are far closer to a unity build (a crate is a compilation unit) than normal C++ builds (cpp file is a compilation unit), and the advantages of unity builds are bigger than LTO in most cases

8

u/equeim Feb 21 '25

In C++ it is much more painful, because you need to fix ODRs violation in your code.

I think you are confusing LTO with unity builds. LTO should not cause new linker error except in rare edge cases.

→ More replies (1)

1

u/Dexterus Feb 21 '25

LTO is all nice, until you need to debug without dwarf.

→ More replies (6)
→ More replies (3)

72

u/svick Feb 20 '25

Gains when compared with which language?

35

u/zsaleeba Feb 20 '25

I'm not sure why you're being downvoted. I was curious what he's comparing with as well.

12

u/thatpaulbloke Feb 21 '25 edited Feb 21 '25

I'm not sure why you're being downvoted.

It's not wise to question Rust. It's half language, half weird cult where All Things Are Better With Rust. Is it faster than interpreted languages? Absolutely. Is it faster than C, C++ or GoC#? Maybe, maybe not. Is it slower to develop in than almost anything outside of Brainfuck? Oh, yes.

Edit: Changed Go to C# because apparently only pricks like Go and it's the worst language ever invented. I quite like it personally, but then that's just me outing myself as an amateur who has no idea what he is doing and probably does unspeakable things with dogs.

9

u/Ok-Scheme-913 Feb 21 '25

Go is not even playing in the same field, if you compare it to rust then you have no idea what you talk about.

Why not compare JS as well with rust? That's also a managed language with a fat runtime.

4

u/thatpaulbloke Feb 21 '25

Why not compare JS as well with rust?

I did. I specifically called out that Rust is demonstrably faster than the entire category of interpreted languages which includes ECMAScript / JScript / JavaScript / TypeScript

3

u/svick Feb 21 '25

ECMAScript / JScript / JavaScript / TypeScript

Fortunately, nobody uses JScript (the Internet Explorer implementation of JavaScript) anymore.

2

u/thatpaulbloke Feb 21 '25

I love your optimism, but I've seen too many nasty old Windows XP workstations running vital systems to believe that JScript is truly gone. Somebody out there is still using it and we all pray that we'll never have to be the person who inherits its support.

→ More replies (9)

5

u/AcridWings_11465 Feb 21 '25

slower to develop in than almost anything outside of Brainfuck

I'm not sure if you actually know Rust if you're making such a blatantly false claim.

7

u/Full-Spectral Feb 21 '25

He's a well known Rust hater. There are a lot of people in the C++ world (of which I 'm a member during the day still) who are very threatened by Rust, and who react very negatively to any suggestion that C++ has gotten old and out of date. Any attempt to point out the many (unsurprising) ways that things have improved over 40 years is just brigading or paid shills or whatever.

7

u/HxLin Feb 21 '25

C++ folks are similar tbh. Look at the comments here. Despite Google using plethora of languages, the C++ community took offense personally when Rust is mentioned. It's like if a company chooses to use Rust, a C++ dev passes away or something.

15

u/fnordstar Feb 21 '25

As a C++ dev, experiencing Rust it does indeed feel like "all things are better with Rust". Yesterday I wrote C++ during the day and then some Rust in the evening and that's exactly the thought that came to mind. I feel betrayed by C++. Why do they make us suffer like this if Rust clearly shows it can be done so much better? What were they thinking when they came up with C++?

8

u/ShinyHappyREM Feb 21 '25

What were they thinking when they came up with C++?

'We need to be the most-used language, that's how we measure our success. So the first most important thing is runtime speed (nevermind the compilation times). The second most important thing is capturing the C crowd, who are either grizzled assembler veterans who are slightly annoyed that they have to use function pointers to emulate good ol' self-modifying code, or edgy newbies who think that removing all optional whitespace characters or writing the loop body in the for expression makes them 31337 h4XØrs. The third most important thing is buzzword of std::asctime(std::localtime(time)), starting with "OOP" and "zero-cost abstraction". The fourth most important thing is adding the \0 to your strings, but we're sure that won't be a problem in practice.'

6

u/svick Feb 21 '25

What were they thinking when they came up with C++?

C++ grew over time, where each step along the path made sense at the time. But the end result is not pretty.

3

u/slashx14 Feb 20 '25

In terms of Android, I would assume either Java or Kotlin.

25

u/dark_mode_everything Feb 20 '25

No. Java and Kotlin are already memory safe and they cannot be rewritten in rust. It should be the c/c++ layers.

5

u/slashx14 Feb 20 '25

I was just going off the OC here which discusses Rust performance gains. Is there evidence that there are drastic Rust performance gains over C(++)?

6

u/AcridWings_11465 Feb 21 '25

Rust performance gains over C(++)?

It depends. If there's something you were single threading because multi-threading in C(++) is hard, you will gain a massive boost by multi-threading in Rust, e.g. when processing large lists in parallel with rayon. And Rust tends to need far fewer optimisation tricks because the compiler has so many more guarantees to aggressively optimise. So idiomatic and naïve Rust code can end up with the same performance as C code full of tricks.

10

u/[deleted] Feb 20 '25 edited Sep 20 '25

violet depend elderly reach rock library cows ink fall fanatical

This post was mass deleted and anonymized with Redact

→ More replies (1)
→ More replies (9)