Why Rust Can’t Replace C++
https://www.youtube.com/watch?v=QNPwKMOQIKM
无作者
Everyone’s talking about how Rust is replacing everything.Well, at least this guy is.I mean, he’s made a video about Rust replacing Java,Rust replacing Zig, Rust replacing everything.But you know what?Rust isn’t replacing C++.Wait, Rust is replacing C++.Well, only in some cases.Because the fact of the matter is C++ is just simply better than Rust, at some things,which is what this video is all about.I mean,there’s a reason why C++, is the language of choice for some of the fastest code in the world,or why it’s the language used in fighter jets,even if they do ban 90% of C++ features,and why it’s still thriving in industries it’s best at,despite government pressure and public campaigns from big tech,urging the tech industry to abandon C++ because it’s memory unsafe.So, we’re going to dive into a few of these instances,as well as a little fight.C++ is having did have kind of with itself.Hey man, before making this video,I forgot how much I loved C++.
Anyway, here’s a quick word from today’s sponsor,Macroscope.Look, there’s no secret that more code is being committed than ever before.I mean, I made a whole video about it,and it’s true for both open and closed source repos alike,which is why code review is more important than ever before.And an AI tool like Macroscope makes code review easier, better,and more accurate, all while saving you time.And they have a feature that I really think you should be taking advantage of.Check this out.It’s called approvability,where Macroscope can now approve your PRs automatically.And I know that sounds crazy at first, but hear me out.How many times do you come in, here to review a PR and all it, is is an update to some userfacing, copy or fixing a broken link?That type of change doesn’t need a human review.It just needs checks.That’s the beauty of the approvability, check or any of Macroscopes check run agents for that matter.And whether you use Macroscope’s.
default eligibility guidelines or add your own policy,it’s really good.I mean,there’s a reason why Macroscope, is already auto approving roughly 40% of PRs on average.But plenty of PRs, should have genuine, human review with an AI code review tool being a second set of eyes.And Macroscop’s new code review is better and more accurate than ever before.I mean, just look at these numbers.So, if you want to check out Macroscope,just click the link in the top of the description or go to jetty.2/mroscope.So, when I say some of the fastest code to ever exist,I’m talking about finance, highfrequency trading to be exact.Juan Ald of Citadel Securities,who is also a voting member of the C++ standards committee,just made a video all about this, which is a very good video.I highly recommend you watch it.But they’ve gotten this down to a science.To do a trade, you only have 30 microsconds.
A microsecond is a unit of time in the International System of Units (SI) equal to one-millionth (10⁻⁶ or 0.000001) of a second
Now, this is an example number because of lawyers,go figure, but gives us the idea.
Once something hits your pipeline, you have two microsconds, to decide what to do,five to check risk, three to fire the order,and 20 microsconds to travel the network,which at the speed of light through, fiber is assuming you’re within, 2 and 1/2 miles of the exchange.Which is why this industry spends, hundreds of millions of dollars, building as close as possible, to the data centers that match, these trades, as well as specifically, engineering, ultra-, low latency networks because if you miss by even 10 microconds,someone else gets to the fill, and you get nothing which means, you start losing money or you, just aren’t making money.And this isn’t just a one-off edge case.This is consistently how highfrequency trading software must perform.And as Mr.All they says, >> there are very few languages that can give this to you and one constant, is that in those situations, the choice of computing language is not academic.It’s not an experiment.It is practical and it has to, work all the time and that is.
why most people go to C++.But why exactly C++?Well,let’s go about building this system requirement, by requirement to see which languages actually hold up.Requirement one, your program,your hot path to be specific, is never allowed to stop.Okay,so that immediately becomes a problem for every, GC language out there because imagine it’s 11:00 a.m.An economic, number just released and then all of a sudden the garbage collector fires for, 5 milliseconds, right in the middle of your hot path.Well, remember your budget is 30 microsconds.So if your GC takes a nap for 5 milliseconds, that’s 5,000 microsconds.Let’s just say you’re not going, to be hitting that trade and, now you’re out millions of dollars.So that more or less eliminates Java, C,Go, Python,all those GC languages.I’m not saying Elmax, doesn’t exist or other algorithms, that use Java or some of these other languages,but I guess there’s an exception to every rule.But we still have Rust and C and C++.And if you’re asking what about Zigg, well,
they’re not even on version one yet.So I don’t think personally that they belong in, the conversation with these other more established languages.Not because it’s not good,but because they’re still very much figuring things out.They overhauled their IO twice recently with some very breaking changes.Also, there’s not many Zigg devs out there,especially when you compare them to C++ devs.So, with all this being said,it’s just not a safe bet when billions of dollars are at play.Now, about Rust surviving that cut here,I want to get into the weaves a little bit because Rust and C++, can be at performance parody.Rust C and C++‘s clang both compile through LLVM.So the optimizer working under the hood is exactly the same.For example, take an algorithm,give it to an expert to write in both languages.After all said and done, you can get basically what’s the same instructions.There’s no argument about the C++, binary being inherently faster than the Rust binary.
But the designs you can write in each language are what come into play here.Which brings us to requirement two, the designs.And that’s my broad way of putting it.Here’s what all day says they build.>> Concurrency, was always left at the mercy of the gods of like networking and threading, and whatnot.And we all have seen experts that for many years had their own recipes for this.Uh throw a couple of threads here, a couple of mute success,a lot of cues.Now we have lock for Q.So everything is a lock for Q.So the hot path of a trading system is lock free code.Picture a ring buffer sitting between the network and your trading logic.One thread is writing market, data into it while another thread, is reading that data out of it.Both of them are working in the, same buffer at the same time, coordinating through atomics, and there’s not a single lock, because a lock means waiting, and again waiting is the one, thing we cannot do.Same idea with intrusive data, structures where one object lives.
inside multiple lists at once.Everything holding pointers into everything else.Now think about what rust is.The borrow checker is built to prevent this exact kind of thing.multiple places mutating shared memory without something coordinating them.Now to be fair, Rust’s atomics are safe Rust.So a lock free ring buffer on its own, Rust can build that.But the gnarier these designs get, the intrusive data structures,the memory reclamation,the pointer relationships the compiler just can’t prove safe.Eventually, you’re bound to reach for raw pointers and unsafe blocks,which is what the Rust lock free libraries actually do under the hood.And that doesn’t defeat the purpose of Rust.It’s actually an intended pattern.You put a small dangerous part behind a safe abstraction,audit the heck out of it, and the rest of the program keeps Rust’s guarantees.But look at what you’d actually be doing here.Some of your hottest, most important code,the code that makes the money, can end up being atomics and.
raw pointers and audited unsafe, cores anyway.So at that point, you’re effectively writing C++ style code,but in a language that’s fighting you on that code.So when you have another language C++ that doesn’t fight you on it,that exact style is just a normal program in that language as well as you know, it it helps having the entire infrastructure, built around this language.Well, that doesn’t knock Rust off of this list,but it makes C++ make a whole lot more sense.And if that wasn’t enough to convince you, well,C++, lets you build an object at the exact memory address you choose, via placement new,which is how these systems do arena allocators and cache line aligned pools.You can see in all day slide here, it says you own the data path.And another caveat, yes,Rust can do arenas too,but its per container custom allocator API has been sitting unstable for, checks notes, over a decade.So that part of Russ allocation, deal, it just isn’t as mature as one would want for code that’s obsessed with exactly.
where its data lives, since that’s controlling cache, behavior which is where you can, definitely lose some nanoseconds, at least.So after just these two requirements what’s really left is C and C++, and the reason C++ is a choice over C well is kind of the whole reason C++, as a programming language exists in the first place.you get the control of C and the abstraction that C++ provides.So that way you don’t have to roll your own data structures,dispatch type machinery, all of that over and over again.And that abstraction can get compiled away.So it doesn’t have to cost you anything at runtime.And it also helps that C++, just standardize some of this industry’s lock free toolkit and C++, 26 with hazard pointers,rcu, and other things these firms have been handrolling for decades.And since these firms employ people that are on the C++ standards committee,well, now they have say over the language’s direction,so they can entice it to improve specifically for their use case.
But finance isn’t the only place C++ is best.And this one isn’t even about speed.It is about the tool chain.Because we also got to look at AI, but not the LLMs,what the LLMs run on and are trained on, which are mostly Nvidia GPUs.And the way we program those GPUs is CUDA, which is a set of C++ extensions,and the libraries on top of it are, words that I never know how to properly pronounce,but I’ll try my best.Klass for Matrix Math, KDNN for neural net ops,which I used to make that Nerf gun shoot me in the face when I got distracted.If you remember that project from years ago, Tensor RT,PyTorch’s Core, all of it’s built around C++ and CUDA.And here’s the thing,people are actively working on Rust for GPUs like Rust Cuda.And Nvidia’s own research arm just published something called Qile Rust,where their Rust implementation of matrix multiplication hit 96%, of Qblast on a B200.So, I can’t sit here and tell you that Rust can’t be fast on a GPU.It clearly can,
but for now that’s a research paper and it’s not quite as good as C++.In C++, well,that’s in production and completely part of the stack.CUDA’s production stack, the compiler,the profiler, the debugger,the decades of tuned kernels, all of that is built around C++.And that’s not changing anytime soon.So when you really think about it, yes,every single time any of these AI models port a codebase from C++ to Rust,C++ did the math.I swear you can’t make this stuff up.And sometimes it’s not even about C++ itself, but the ecosystem around it.You see,I’ve avoided talking about legacy code bases staying a C++, because there’s potential now for those code bases to be ported, if there truly is a better language for that use case.That’s really what my entire, video was about when talking, about all of these different, companies, switching to Rust.But the ecosystem being built around the language, isn’t exactly the same thing because a codebase has, one owner who can decide to port it.
But an ecosystem has thousands, the engine makers,the middleware vendors, the plugins authors.Let’s not forget the hiring pool.Way more C++ devs than Rust devs.And that’s exactly what the video games industry is.Now, is C++ the best language for this?I’ll leave that up to the game devs.But C++ having the best ecosystem for game development is hardly up for debate.You may code with C and Unity,but Unity itself is a C++ engine with a C scripting layer.Unreal Engine is C++ to the core.And Rust has been tried here.a video game studio called Embark Studios,which you may know them as the, team that shipped Ark Raiders, with a founding team coming from, Battlefield.Well, they declared Rust as their primary language.But when they shipped Arc Raiders, one of the biggest games of last year,even they built it on Unreal Engine 5 with C++ as part of the core stack,which makes sense.The SDK, the middleware,the plugins, their own personal experience,it’s all C++.
And a lot of gameplay code is throwaway code.And working with C++, to test out a mechanic just to find out it sucks, is a whole lot easier, than using Rust fighting the, borrow checker just to see how, the mechanic performs in the, first place to realize, it then sucks.Now that’s not to say over the next 5 to 10 years the Rust flagship, engine Bevy won’t start taking, more and more market share and, build out its own ecosystem.But for now it’s C++ and forever it may be C++.But if you’re a gamedev, I’d love to hear more.Please share your thoughts on this one.Now,something I didn’t mention here is that Imbark Studios, does still use Rust for their tools,things outside of the games they’re building.They’re actually the ones who started Rust GPU, to make Rust an actual first class GPU language.And you see things like this, happening a lot where in the case studies we just went over,Rust is being used in one way or another, just outside that primary use case like the parsers,the services,
the everyday systems code and it’s less about Rust and more about C++.Memory safety is the biggest reason Rust is taking any ground and C++, knows this.The problem is when it came time for C++ to propose a solution,they started fighting with themselves.It started back when the government, urged companies, to publish memory safety road, maps by January 1st of this year, and the committee had two major, approaches.One was safe C++, from a guy named Shawn Baxter which brought rust style borrow checking into C++, itself and it was an option Bter already had working in his own C++ compiler.And the other was profiles, pushed by people like Bj Strup, and Herb Sutter which are basically, modes you flip on without changing, your existing code.And just a few months before the January deadline in September of 2025,the committee voted to prioritize, profiles, which mean I understand it safe C++ can’t protect the billions of lines of C++, code out there which means you would have to migrate, that code into its safe model.
And if you’re doing that kind of migration anyway,that’s when people start to look for alternatives.Well, maybe we will rewrite the whole thing in Rust,which just would further add to the problem.But the biggest issue here is that there is no solution yet.Remember, safe C++ already had a working prototype,whereas profiles, it was targeted for C++ 26,which is already out, but it didn’t make it.It got pushed to C++ 29, which if you didn’t know,the naming conventions,that means it’s not going to be here until the year 2029.And with these wave of rewrites being bigger than ever now,because it’s cheaper and faster and easier to port from one language to another,well, this isn’t very good timing.It sure would have been nice if C++ could have gotten profiles into C++ 26,but it happens.And the numbers, they agree with all this.According to Herb Sutter, who is one of the utmost C++ experts,C++, added roughly as many new developers in one year as Rust has developers, in total.
So needless to say, C++ is far from dying.So what seems to really be happening is Rust is just taking ground in areas, that C++, was never best at.And if the C++ 29 safety work actually lands,some of that ground may be taken back.But today, when it comes to some of the fastest code in the world,well, that’s staying C++.Now,the smart thing for me to do, would be to tell you to go watch, this video because it’s mine.But I mentioned how fighter jets use C++, in the intro and Lori Wired made a video all about that,which is awesome.So, I recommend you go watch that video.Just maybe maybe you’ll consider subscribing before you do.But that’s all I got.I’ll see you in the next one.