C++ vs Rust: which is faster?

preview_player
Показать описание

00:00 What is Advent of Code?
00:48 Python vs Rust
01:19 Interpreters vs JITs
03:03 Buffered I/O
04:27 Day 18 Benchmarks
05:00 How does the code feel?
06:49 Day 19 Benchmarks
07:17 Profiling, disassembling & decompiling
08:14 x86 assembly crash course
12:44 Suddenly SIMD
15:52 Day 19 mystery solved
16:40 Register allocation is hard
18:08 Switching calling conventions
18:31 2x3 != 3x2
19:12 Thanks & sponsored segment

Рекомендации по теме
Комментарии
Автор


If someone's watching this going "I /know/ I can beat that by tweaking the C++ code", you should do it! I think there's a lot of good that can happen with some friendly competition between the C++ and Rust camps, as long as we both treat it from a "huh, neat that we can get compilers to do that!" perspective and not just shout at each other from opposite side of a virtual stadium.

I'll try to make a full write-up that goes a little deeper (maybe comparing emitted IR between clang and rustc), but first I have more research to do.

fasterthanlime
Автор

As soon as I saw the C++ solution I knew that it wasn't written by someone who knows C++ all that well so I decided to attempt my own solution. My solution ended up being around 4000x faster. C++ and Rust are close enough that the deciding factor for any non-trivial task is almost always going to be the algorithms used, not the language. So study your algorithms, folks. :P

orbital
Автор

I know **just** enough low level computer magic to understand this, but man was it work. This was very well presented, I've seen much simpler things explained with far less clarity.

willsterjohnson
Автор

"This has been an ongoing fight for years according to sources who are very tired"
I felt this in my soul

vanweapon
Автор

Love the video! Absolute madness at the end, LLVM is probably such mess at this point because of the optimization priorities that it takes a team of compiler engineers a couple of days to even trace a problem lol, but they are the real heroes

MKUSQ
Автор

This video is really great! I also did Advent of Code with Rust and it really helped.
It's really refreshing to listen about Assembly in a clear, non-scary way.

iilugs
Автор

The conclusion that compiler engineers need hugs and cats need petting are both true.

SWinxyTheCat
Автор

I spent weeks of my life shoving SIMD into an unreal engine module to leverage new cpu architectures and you managed to sum up basically 3 weeks of pain into 20 minutes and I still understood it better than all the formal sources I bled my eyes at. Compiler engineers deserve all of the love in the world and they are criminally under appreciated.

BarronKane
Автор

Your way of describing complex SIMD instructions was superb, congrats Amos!

carloscarral
Автор

I do think it’s interesting the difference in knowledge of the two languages and what is “easier”. I know C++ very well I would say and I’ve never even touched rust, but when he went over the operator overload functions and mentioned how C++ was difficult and rust was “just implementing a trait like any other”. This made me audibly laugh since for me C++ overload is super easy and clean, when I saw the rust code at 5:56 it looked insanely bloated and had characters that seemed absolutely useless.

I’m not trying to bash Rust or the knowledge Faster, just pointing out how coding in a language with little knowledge creates MASSIVE bias and situations like this where something is “simple” just because you know it.

FunkyMoneyMan
Автор

Just glancing over the C++ code it could definitely be improved by:

Removing the globals
Removing the horrific macros (for endl specifically)
The constants could be made into constexpr constants (i.e real constants, not text replacement)
Removing the using namespace std
Removing the `typedef` struct (this has never been a required thing in C++, this is pure C)
Replacing the remaining typedefs with using
Adding defaulted comparison operators for the structs that would essentially all be one liners without any user defined bodies (structs are just classes, they can have member functions, even though the operators would be friended and not technically members).


Not much about performance itself which could probably also be improved, just cleaning up the code immensely by following the c++ core guidelines.

valizeth
Автор

"If you see a compiler engineer in the wild, ask if they need a hug." 😂 Very interesting to see the differences in compilation between C++ and Rust, and the effects of Stack vs register allocation illustrated so well. Great video, tons of information packed in a very short space. Thank you.

BriceFernandes
Автор

Just a note, if you're using the same "backend" eg: llvm for rust and c++ the results should be similar, the reality is though while llvm is a generalized backend for multiple languages, it's history is primarily to support the clang frontend. Essentially you're comparing how well integrated one frontend is with another so don't be too surprised if rust falls behind c++.

The add_examples issue is due in part to padding. Your struct with 3 members is likely being handed off to the compiler and it sees a struct which isn't perfectly aligned to a power of 2. So while you're thinking you're dealing with 3 64 bit registers the compiler's going to treat that struct as 4 64 bit registers. While you're right that you're just adding 6 64 bit integers together, what you've likely done is trip up an optimization stage because the compiler sees 8 64 bit integers (where the 4th and 8th are essentially discarded) and that likely is enough for simd optimizations to kick in (where adding 6 may not be worth doing) and then confuse later optimization stages.

andersama
Автор

Great video. I love the low-level stuff, and compiler writers definitely deserve many hugs. I have my own toy compiler written in Rust, and even implementing the dumbest register allocator possible was already worth at least two hugs per day, because debugging even the tiniest of changes means reading screens of assembly code, interpreting that in your head and trying to keep up with tons of info, such as which variable should sit in which register, what registers are spilled at a particular point, etc. That's hard even given that I "cheated" and decided to generate code for RISC-V, which is much simpler than x86 or amd64, and I'm just playing around. I don't know if I am even able to write a more sophisticated one correctly with my ADHD and average intellectual abilities. How hard is what serious compiler devs do? Probably insanely hard.

The good thing is I have a cat too (btw she has the same pattern on her forehead as your cat). Whenever I feel overwhelmed with coding, she's always there for me. By that I mean she is sleeping nearby, not caring even a little bit about my struggle with my own inability to think clearly. But occasionally, she jumps on my lap and gently reminds me of things more important than code, such as scratching her cheeks 😁

bytefu
Автор

Well, that escalated quickly. Came for Rust, got to Assembler. I wish I learned this in the university, it is so fascinating. Makes me think how many decisions were taken for us in higher level languages. Thank you for the deep dive and clear explanation. I feel complete… Turing complete now 😅

mnthalone
Автор

This is the first video I have seen from you, and I have to say, it made me sure I want to see more from this channel. Such well present, such cat, such content.

You have a really nice style, keep it up.

kintrix
Автор

Love your blog posts. Keep doing what you're doing. It's great. 👍

mb
Автор

Incredible video. I am going to share it with everyone in the company I run! Keep up the ultra-nerdy-geeky subjects & explanations, you are REALLY good at it and makes this kind of high level stuff very accessible to whoever is listening.

pcost
Автор

The content was really great, super informative. It was like reading one of your blog posts but in video form.

I watched the day 18 stream and learned a ton.

Some of the assembly stuff went over my head but that's just because I'm not too familiar with it. But even so, it was explained well. I'll definitely be coming back to watch it again once I know more about assembly.

aleksandermirowsky
Автор

Matt Godbolt has a podcast (two's complement), and on the latest episode he quoted 'the first rule of profiling is that you're wrong' by which he means (I'm assuming) it is virtually impossible to guess the performance of a piece of code, or know which version will be faster as you illustrated here nicely.
So I'm guessing the answer to the question in the title is 'it depends' or just 'they are similar'

frydac