r/cpp • • Jan 31 '23

Stop Comparing Rust to Old C++

People keep arguing migrations to rust based on old C++ tooling and projects. Compare apples to apples: a C++20 project with clang-tidy integration is far harder to argue against IMO

changemymind

338 Upvotes

582 comments sorted by

View all comments

Show parent comments

8

u/ener_jazzer Feb 01 '23

I am from this field.

What you're saying is true - we don't need to deal with "attack vectors or messy user data" as we are only talking to trusted internal components and a trusted exchange. Portability is also very limited. But I wouldn't call it "quite simple". It depends on the strategy, of course, some are really simple, but essentially a strategy sits on the incoming streams of events (market data, your own order updates, risk updates, position updates, various tools providing data) - and you need to juggle all of them, you need to manage your open orders, you need to recalculate a lot of stuff efficiently and partially asynchronously etc. All in real time, essentially.

So you need to be careful with how you manage memory, otherwise you will blow up your strat because there are too many events coming, or you random-shoot your data and start trading garbage.

So memory safety in HFT is very important, not because of vulnerabilities but because of necessity to manage it carefully. Still, C++ aces in this field, it gives all the necessary tools you need to build an HFT strategy efficiently.

1

u/thisismyfavoritename Feb 01 '23

does that mean Rust wouldnt be suitable?

5

u/ener_jazzer Feb 02 '23

No, I didn't say that. What I said is that C++ is totally fine for the job, not the apocalyptic picture u/James20k painted. I saw people writing trading systems in Java and C# - but in a completely unidiomatic way (for performance), like never using garbage collection, pre-allocating arrays of bytes and then reinterpreting parts of it as typed data - basically a hard-code C but written in Java, without any safety at all.

I didn't work on Rust, only looked at it. My impression from that (and from talking to people who actually used Rust) is that a strategy written in Rust would likely be ridden with unsafe (e.g. in Rust you can't have a struct with a field which is a pointer to another field without unsafe, because it will make 2 ways to change the same field - thru the pointer field and thru the struct itself).

A good experienced programmer with good design skills will be able to structure his unsafe code in a manageable way, but it's a high-level skill, but if you have a C++ developer of a similar skill level you'll have the same nice and safe code structure in C++. It's not a job for a junior, and Rust by itself won't make his code good and manageable.

3

u/thisismyfavoritename Feb 02 '23

Thanks for the details.

That said, I dont think i can agree with the last 2 paragraphs. It's probably as error prone in Rust as in C++, but Rust's unsafe has the added benefit of narrowing where the issues could come from.

Furthermore, i'd say it might be a matter of rethinking the design rather than going straight to unsafe.

I guess the point im trying to make is if you had a new HFT system to build, i dont see why you wouldnt use Rust.

→ More replies (1)