News for hackers. Stuff that matters.
Re:C and C++ Prioritize Performance over Correctness by kiratp on Friday August 18, 2023 @04:11PM (#37181451)
Rust is not a GC language. It can and does achieve the same perf as any modem C++ with the latest flavor of the C++ safety guardrails.

The set of reasons to start a new project in C/C++ are few and the list is shrinking by the day.

Re:C and C++ Prioritize Performance over Correctness by MarkMarine on Friday August 18, 2023 @04:22PM (#37181572)
I love Rust, but as a daily C++ user (embedded microcontrollers, not supported by Rust yet, and then native Linux tooling to interface with them) what I find most frustrating about Rust is the number of crates that need to be knit together to make working software. Often these crates are from a single dev who last committed in 2020 or something. I really wish there was something like go’s standard library in Rust. That has been my barrier to adoption, and believe me I WANT to use it at work and I don’t want to juggle chainsaws anymore.
Re:C and C++ Prioritize Performance over Correctness by Ar-Curunir on Friday August 18, 2023 @04:36PM (#37181756)
[delayed]
Re:C and C++ Prioritize Performance over Correctness by xedrac on Friday August 18, 2023 @05:03PM (#37182076)
In C++, you'll often see projects just use boost, which is a big monolith of useful libraries.
Re:C and C++ Prioritize Performance over Correctness by tumdum_ on Friday August 18, 2023 @05:19PM (#37182273)
Embedded microcontrollers are not the place where boost is used.
Re:C and C++ Prioritize Performance over Correctness by oasisaimlessly on Saturday August 19, 2023 @01:56AM (#37185954)
Counterpoint: I use Boost on embedded microcontrollers. (Albeit only header-only components of Boost.)
Re:C and C++ Prioritize Performance over Correctness by MarkMarine on Friday August 18, 2023 @05:43PM (#37182555)
I suppose that wasn’t clear. My mistake. I use C/C++ for micro controller code every day, because Rust doesn’t support my microcontroller, doesn’t have a real RTOS, and I’m making real hardware. Something that needs to run in the field for 10+ years and I can’t get out there and ensure it’s updated, and my company doesn’t want to invest millions of dollars on beta testing embedded rust on. So embedded is out for now but I’m looking forward to when it’s supported fully and out of beta. Most embedded controllers come with some RTOS and libraries they support, written in C.

For tooling, I can context switch out of C++ which does have boost, into [rust, go, python… etc] and deal with that switch, or just be lazy and write it in C++. I’ve tried to write three tools in Rust so far, and the pain of not having a good stdlib, of essentially searching the internet for a blog post that solves my issue then finding the blog post was written by one dev 4 years ago to pump up their library that was written 4 years ago and then never supported after that… it’s a bit exhausting.

Again, before ya’ll attack. This is from the perspective of a willing, excited customer. I want to use Rust at work and advocate for it. Just saying, it’s not easy in the state it’s in.

Re:C and C++ Prioritize Performance over Correctness by seabass-labrax on Friday August 18, 2023 @05:57PM (#37182710)
Which microcontroller are you using? Rust support for embedded targets is slowly improving, so there might be a beta build for your chip.
Re:C and C++ Prioritize Performance over Correctness by MarkMarine on Saturday August 19, 2023 @11:16AM (#37189606)
ESP-32, the Rust support is not mature enough to ship commercially.

C-410 from Qualcomm, which is a whole different world, I can’t even look at the source code.

Re:C and C++ Prioritize Performance over Correctness by dgacmu on Friday August 18, 2023 @05:45PM (#37182564)
I'm sympathetic. I'm partway into an embedded project on an stm32 / m0 for which there _is_ good rust support through Embassy and it's utterly magical. And at the same time, trying to do something for which there isn't a good no-std crate is, er, anti-magical to the point of forcing me to change the design. The ecosystem isn't as comprehensive yet.

But when it works, wow. This has been the most fun embedded project I've ever done with the least debugging.

Re:C and C++ Prioritize Performance over Correctness by oll3 on Friday August 18, 2023 @05:55PM (#37182683)
I have replaced my use of C (and C++) in embedded with Rust for the last couple of years. Sure, some parts are still missing or immature, but overall also very promising. And I do enjoy it so much more than the MCU manufacturer's please-insert-your-c-code-between-these-comments-and-pray kind of development.
Re:C and C++ Prioritize Performance over Correctness by stjohnswarts on Saturday August 19, 2023 @01:55AM (#37185948)
Are you doing this on personal projects or commercial projects? I'm pretty happy with c++17 on all my embedded commercial projects
Re:C and C++ Prioritize Performance over Correctness by oll3 on Saturday August 19, 2023 @04:02PM (#37192559)
Strictly commercial. But if one is happy with other language I guess there is little reason to switch.
Re:C and C++ Prioritize Performance over Correctness by hdjfkfbfbr on Friday August 18, 2023 @10:02PM (#37184723)
The people behind the esp32 are writing a rust compiler.... I have played with it only a little, but it worked

Edit:spelling

Re:C and C++ Prioritize Performance over Correctness by stjohnswarts on Saturday August 19, 2023 @01:56AM (#37185953)
A full rust compiler instead of use the "de facto" one? on a language as complicated as rust that sounds scary unless they have a lot of financial backing and significant amount of compiler devs.
Re:C and C++ Prioritize Performance over Correctness by jacquesm on Saturday August 19, 2023 @06:45AM (#37187413)
Why would they not just adapt the code generator for the reference compiler? Rust is a moving target, committing to a new compiler at this point is a massive investment.
Re:C and C++ Prioritize Performance over Correctness by MarkMarine on Saturday August 19, 2023 @12:54PM (#37190715)
Not a rust compiler, they provide crates to interface with their C code in ESP-IDF, which is essentially a FreeRTOS port that supports the dual core Espressif chips, which vanilla FreeRTOS does not support. Also some of their own libraries for things like MQTT, which I found unfortunately subpar in comparison to the vanilla FreeRTOS code.

It’s all beta software, but here is what they list in the docs:

Services like Wi-Fi, HTTP client/server, MQTT, OTA updates, logging etc. are exposed via Espressif's open source IoT Development Framework, ESP-IDF. It is mostly written in C and as such is exposed to Rust in the canonical split crate style:

a sys crate to provide the actual unsafe bindings (esp-idf-sys) a higher level crate offering safe and comfortable Rust abstractions (esp-idf-svc) The final piece of the puzzle is low-level hardware access, which is again provided in a split fashion:

esp-idf-hal implements the hardware-independent embedded-hal traits like analog/digital conversion, digital I/O pins, or SPI communication - as the name suggests, it also uses ESP-IDF as a foundation if direct register manipulation is required, esp32c3 provides the peripheral access crate generated by svd2rust. More information is available in the ecosystem chapter of The Rust on ESP Book.

Re:C and C++ Prioritize Performance over Correctness by overgard on Friday August 18, 2023 @04:22PM (#37181569)
I phrased it poorly, I didn't mean to imply Rust is GC'd. I mean that it's still niche. C++ very much is not niche, which is my point. The other non-niche languages generally are GC'd (java, C#, javascript, etc.)

> The set of reasons to start a new project in C/C++ are few and the list is shrinking by the day.

That'd be true if every project started from scratch with only the language standard library. And yet..... almost any project you start is going to be dependent on a large chunk of code you didn't write, even on greenfield projects.

Re:C and C++ Prioritize Performance over Correctness by haberman on Friday August 18, 2023 @04:34PM (#37181716)
> That'd be true if every project started from scratch with only the language standard library. And yet..... almost any project you start is going to be dependent on a large chunk of code you didn't write, even on greenfield projects.

I think this is true, and I'd refine my original statement accordingly. My original comment was thinking more from first principles, not as much about pragmatic considerations of ecosystem support.

If we were to disregard the momentum of existing ecosystems, I think C/C++ would be niche choices today: very important for certain, focused use cases, but not meant for the masses. Taking into account the momentum of existing ecosystems however, they still play a large role in many domains.

Re:C and C++ Prioritize Performance over Correctness by bayindirh on Friday August 18, 2023 @04:52PM (#37181946)
However, Rust doesn't have a "I know what I'm doing, let me be" switch. No, "unsafe" is not that.

I have a couple of languages in my belt C, C++, Go, Python, Java are the primary ones. C and C++ are reserved for "power use". For power use, I mean scientific, HPC stuff or any code I need the full capacity of the processor.

To get that, sometimes you need -ffmast-math, sometimes said undefined behavior, or code patterns which translate to better code for the processor you target.

Rust doesn't allow any of that, moreover limits me the way I can write my programs, no thanks.

For security-sensitive compiled code, I can use Rust, but I can't use Rust anywhere, and everywhere. It also has its niche, and it can't and won't replace C or C++ at the end of the day. It'll get its slice (which might be sizeable), a couple of compiler implementations, and will evolve to become another mainstream , boring language, which is good.

Also, if you give half an effort, C++ is pretty secure and robust to begin with, esp. on the pointers department. Just be mindful, and run a couple of valgrind tests on your code, and you're set. Been there, done that.

Re:C and C++ Prioritize Performance over Correctness by andrepd on Friday August 18, 2023 @05:21PM (#37182303)
In what sense does rust not expose the full power of the processor to the user? Can you give a concrete example?

> Just be mindful, and run a couple of valgrind tests on your code, and you're set.

Thousands of severe CVEs every year attest to the effectiveness of that mindset. "Just git gud" is not a meaningful thing to say, as even experienced devs routinely make exploitable (and other) mistakes.

Re:C and C++ Prioritize Performance over Correctness by bayindirh on Friday August 18, 2023 @06:29PM (#37182989)
Rust probably won’t allow me to implement a couple of lockless parallel algorithms I have implemented in my Ph.D., which are working on small matrices (3K by 3K). These algorithms allow any number of CPUs do the required processing without waiting for each other, and with no possibility of stepping on each other, by design.

In the tests, it became evident that the speed of the code is limited by the memory bandwidth of the system, yet for the processors I work with that limit is also very near to practical performance limit of the FPUs, as well. I could have squeezed a bit more performance by reordering the matrices to abuse prefetcher better, but it was good enough and I had no time.

Well, the method I verify the said codebase is here [0]. Also, if BSD guys can write secure code with C, everybody can. I think their buffer overflow error count is still <10 after all these years.

[0]: https://news.ycombinator.com/item?id=31218757

Re:C and C++ Prioritize Performance over Correctness by vlovich123 on Friday August 18, 2023 @07:11PM (#37183335)
> Rust probably won’t allow me to implement a couple of lockless parallel algorithms I have implemented in my Ph.D., which are working on small matrices (3K by 3K)

That’s a bold claim to make without evidence. Things like that totally seem doable in Rust without a lot of ceremony. You might have to launder some lifetimes through unsafe if you can’t convince the compiler statically, but that’s not that hard.

> Also, if BSD guys can write secure code with C, everybody can. I think their buffer overflow error count is still <10 after all these years.

As was the problem with Linux enthusiasts claiming Linux was safer bedside fewer CVEs, BSD similarly benefits from having almost no attention paid to it + very few core developers so the feature growth rate is minimal. I don’t but the claim that BSD’s qualities extrapolate anywhere rather than a more mundane explanation or that BSD is the exception that proves the role somehow given no other successful project seems to be able to do the same.

Re:C and C++ Prioritize Performance over Correctness by bayindirh on Friday August 18, 2023 @07:14PM (#37183371)
I would happily give it a try, when I have some time on the side. But that day is not today, unfortunately.

> I don’t but the claim that BSD’s qualities extrapolate anywhere rather than a more mundane explanation or that BSD is the exception that proves the role somehow given no other successful project seems to be able to do the same.

It's mostly about "it's done when it's done" mentality and always putting the discipline before writing code fast. It's a vastly different mentality when compared to modern, conventional software development. This mentality also makes what Debian is Debian.

On the other end of the spectrum, we have the stressed solo developer syndrome, which brings us OpenSSL with its all problems and grievances.

Re:C and C++ Prioritize Performance over Correctness by kouteiheika on Saturday August 19, 2023 @02:01PM (#37191390)
> Rust probably won’t allow me to implement a couple of lockless parallel algorithms I have implemented in my Ph.D.

Why? Can you be more specific? Rust has built-in support for atomics and essentially the same memory model as C++, e.g.:

https://doc.rust-lang.org/std/sync/atomic/struct.AtomicU32.h...

And you can sidestep essentially any requirements of normal Rust using an `UnsafeCell` (e.g. that's how mutexes are implemented, with an `UnsafeCell` holding the data and an atomic).

I've written my share of lockless algorithms and I haven't really noticed any deficiency compared to C++, but then, I'm not an expert in this area, so if there is a deficiency here I'd love to hear about it.

Re:C and C++ Prioritize Performance over Correctness by kouteiheika on Friday August 18, 2023 @06:28PM (#37182976)
> To get that, sometimes you need -ffmast-math, sometimes said undefined behavior, or code patterns which translate to better code for the processor you target.

> Rust doesn't allow any of that, moreover limits me the way I can write my programs, no thanks.

I'm willing to prove you wrong here. Can you give some concrete examples?

For -ffast-math you can most certainly enable it, but AFAIK for now only in a localized fashion through intrinsics, e.g.:

https://doc.rust-lang.org/std/intrinsics/fn.fadd_fast.html

https://doc.rust-lang.org/std/intrinsics/fn.fmul_fast.html

So instead of doing this the dumb way and enabling -ffast-math for the whole program (which most certainly won't need it) you can profile where you're spending the majority of your time, and only do this where it'll matter, and without the possibility of randomly breaking the rest of your numeric code.

Personally I find this to be a vastly better approach.

Re:C and C++ Prioritize Performance over Correctness by bayindirh on Friday August 18, 2023 @06:57PM (#37183192)
Thanks for the information, I'll look into that. There's another comment talking about an algorithm I have implemented, it should be around in this thread.

> So instead of doing this the dumb way and enabling -ffast-math for the whole program (which most certainly won't need it)...

When you write HPC enabled simulation code, you certainly need it, and this is where I use it.

Re:C and C++ Prioritize Performance over Correctness by maximilianburke on Friday August 18, 2023 @10:52PM (#37185001)
Is HPC code magically immune to values containing infs or NaNs?
Re:C and C++ Prioritize Performance over Correctness by kiratp on Friday August 18, 2023 @06:38PM (#37183050)
> Rust doesn't have a "I know what I'm doing, let me be" switch.

Every piece of code you write is security-sensitive. IMO It is our professional responsibility to our users that we treat it that way.

For a vast majority of situations, even if you know what you are doing, you are human and therefore will make mistakes.

If the (probably) most heavily invested C++ codebase, Chrome, has memory use-after-free/overflow bugs, there is no way that truly safe C++ can be written at any product-scale.

Re:C and C++ Prioritize Performance over Correctness by bayindirh on Friday August 18, 2023 @06:58PM (#37183212)
> Every piece of code you write is security-sensitive.

No, not every application is CRUD, not every application is interactive and no, not every application serves users and communicate with outside world in a way allowing to be abused during its lifetime.

This doesn't mean it gives me the freedom to write reckless code all over, but the security model of an authentication module and a mathematical simulation is vastly different.

> there is no way that truly safe C++ can be written at any product-scale.

I agree to disagree here, because of my own experience.

Re:C and C++ Prioritize Performance over Correctness by pjmlp on Saturday August 19, 2023 @03:58AM (#37186578)
Like every other job in the world has a concept of liability,that will eventually arrive on computing world at scale.
Re:C and C++ Prioritize Performance over Correctness by bayindirh on Saturday August 19, 2023 @04:29AM (#37186734)
The assumptions you make are almost palpable at this point.

Simulation precision and accuracy already carries tons of liability.

Talking like I or we don’t care about consequences is disturbing, esp. with the experience you have.

Re:C and C++ Prioritize Performance over Correctness by pjmlp on Saturday August 19, 2023 @06:12AM (#37187228)
Well your responses look like you don't care, as if you had any clue about my experience as well, unless the sightseeing from that high altitude cloud is giving mirages.

Any cybersecurity law that requires citzens votes for adoption, will certainly get my signature.

Re:C and C++ Prioritize Performance over Correctness by bayindirh on Saturday August 19, 2023 @06:30AM (#37187329)
If running static analyzers, testing for memory leaks, running end to end tests which takes days, adding strong bounds checking, and failing fast and with great noise as soon as something does not look right is not caring, you’re right. I don’t care.

All this is done for a code which has no network access, no kernel level work, even doesn’t accept any input after parsing the input model and making sure that it’s well formed with no loose ends.

Imagine what I’d do if it does any kind of authentication or user affecting work.

Re:C and C++ Prioritize Performance over Correctness by pjmlp on Sunday August 20, 2023 @02:49AM (#37196822)
Call me surprised. OK, fair enough.
Re:C and C++ Prioritize Performance over Correctness by gautamcgoel on Friday August 18, 2023 @08:16PM (#37183985)
During my PhD I wrote a lot of simulation code in Matlab. This code was only ever run by me on my personal machine for the purpose of generating plots for my thesis. How is that security-sensitive?

We all agree that security is important and a lot of code running in security-sensitive contexts is bad. That's very different from saying that all code is security-sensitive.

Re:C and C++ Prioritize Performance over Correctness by kaba0 on Saturday August 19, 2023 @01:56AM (#37185956)
Matlab is memory-safe, though, which is the kind of safety issue we talk about.
Re:C and C++ Prioritize Performance over Correctness by defrost on Saturday August 19, 2023 @02:05AM (#37186010)
> Matlab is memory-safe, though

Who sold you that notion?

Matlab is proprietary glue holding together modules written in Fortran, C, C++, Java that allows the adhoc inclusion of DLLS put together by graduate students on the fly.

In what universe is any of that "memory safe"?

Re:C and C++ Prioritize Performance over Correctness by kaba0 on Saturday August 19, 2023 @02:25AM (#37186114)
AFAIK it used to be written/interpreted in Java (though it uses a JIT compiler nowadays, doesn’t it?), and in that sense the language itself is memory safe. Of course if you link to a dependency written in an unsafe language you can have memory issues, but that is not what people commonly mean by “a memory safe language”.
Re:C and C++ Prioritize Performance over Correctness by defrost on Saturday August 19, 2023 @03:28AM (#37186408)
> Of course if you link to a dependency written in an unsafe language

Like the matrix handling, plotting, and other modules dragged in from the 1990s standard FORTRAN library era.

Don't get me wrong, I have placed a lot of trust in those kinds of numeric libraries since the 1980s - NIST and other national labs code and review to a high standard - but they weren't then and aren't now "memory safe" just very probably and mostly bug free.

Unless they were, say, rewritten in Java .. and that's a whole other big ball of issues .. translated code.

I'm happy to play along and agree that the JIT compiled 'Matlab language' parts of Matlab are memory safe - but to the best of my experience with past versions that's just the glue portions - the reason so many people use Matlab is for the chunks that get glued together.

Re:C and C++ Prioritize Performance over Correctness by kaba0 on Saturday August 19, 2023 @03:36AM (#37186441)
Aren’t many of these Fortran libraries heavily used all throughout almost any kind of application, somewhere deep down their dependency tree?
Re:C and C++ Prioritize Performance over Correctness by defrost on Saturday August 19, 2023 @03:54AM (#37186548)
Absolutely .. and generally written to a very high standard with a lot of checking, rechecking and running forwards and backwards across multiple architectures with extensive technical mailing lists.

They can still have bugs, memory issues, be called with parameters that cause resource blow outs and aren't "memory safe" in the sense that modern managed languages are.

Matlab is exactly as reliable as well written Fortran and C code can be.

Re:C and C++ Prioritize Performance over Correctness by SubjectToChange on Friday August 18, 2023 @07:15PM (#37183382)
To get that, sometimes you need -ffmast-math, sometimes said undefined behavior, or code patterns which translate to better code for the processor you target.

There isn't really any fundamental limitation of Rust for those situations. A better reason why C++ is better than Rust for compute is the support for OpenACC, OpenCL, OpenMP, CUDA, ROCm/HIP, etc.

Also, if you give half an effort, C++ is pretty secure and robust to begin with, esp. on the pointers department.

The problem is that even if a "Safe" C++ could be defined, it would need to interoperate with the rest of the "Unsafe" C++ world. Perhaps a programmer can write absolutely safe C++ themselves but all bets are off once you're working with other programmers.

Just be mindful, and run a couple of valgrind tests on your code, and you're set. Been there, done that.

Seriously? A "couple of valgrind tests" would be insufficient for catching all but the most trivial and/or blatant memory safety issues in C++, especially when dealing with malicious users. Although I agree that dynamic analysis like valgrind and sanitizers should be the default for C++ development, industrial grade static analysis tools are at least as important. And needless to say, if you find Rust complains about code too much then just wait for what static analysis tooling will complain about.

Re:C and C++ Prioritize Performance over Correctness by bayindirh on Friday August 18, 2023 @07:21PM (#37183433)
That kind of integration is another reason, yes, but this is a passable moat, so I'm not talking about it much (mind you, I live in HPC world).

If programming is a team sport, every team player should be up to the level your project needs. If they are not, they should raise their bars. Seriously.

Yes, seriously, be mindful. Implement allocation and destruction first, fill your code between these two steps.

A couple of Valgrind tests is actually a whole procedure. It's an unfortunate downplay by me [0], probably because I'm very used to use Valgrind as a second nature.

[0]: https://news.ycombinator.com/item?id=31218757

Re:C and C++ Prioritize Performance over Correctness by kaba0 on Saturday August 19, 2023 @01:55AM (#37185950)
So what about multithreaded code with potential race conditions that result in memory errors sometimes. Valgrind already only able to test the actually ran code paths, let alone find race condition-y bugs.

Also, this elitist notion of raising the bar is an instant “turnoff” for me, with all due respect, I can only assume from that that the given person may not be as much an expert as they think of themselves. Not saying that it applies to you as well, but you don’t leave me much other choice.

Re:C and C++ Prioritize Performance over Correctness by lenkite on Saturday August 19, 2023 @03:08AM (#37186322)
Memory safe - I will give you that. All the guarantees you listed are fine for synchronous Rust. But race conditions and deadlocks are not unknown in async Rust. Lots of race condition issues in tokio - including issues where futures leak.
Re:C and C++ Prioritize Performance over Correctness by kaba0 on Saturday August 19, 2023 @03:40AM (#37186472)
That’s true, and usually I am the one pointing this out, that Rust is only data race free :D thanks for the pointer!
Re:C and C++ Prioritize Performance over Correctness by kaba0 on Saturday August 19, 2023 @01:51AM (#37185922)
What does Rust doesn’t allow? There really are only very few hard limits I know of, e.g. tail call elimination.

Ad absurdum it has quite great inline asm support.

Re:C and C++ Prioritize Performance over Correctness by stjohnswarts on Saturday August 19, 2023 @02:00AM (#37185981)
A lot of people who criticize c++ seem to still think it's 2008 and there aren't the crazy amount of compiler checks/linters/standard practices that have happened since c++11 was released.

I like rust and I dick around with it a bit on some small REST projects I have for home automation but for day-to-day work I stick with c++ until rust support picks up in the industry a lot more than it currently is.

Re:C and C++ Prioritize Performance over Correctness by pjmlp on Saturday August 19, 2023 @04:03AM (#37186605)
Because many of us only know C++ best practices from conference slides, and our own side projects, while we cry in despair when looking at typical corporate code bases.

Why do you think Bjarne Stroustoup main subject on conferences is how to write C++ properly without C style coding, for several years now?

Re:C and C++ Prioritize Performance over Correctness by deterministic on Monday August 21, 2023 @05:39AM (#37207228)
New large scale C++ projects are started every single day. There are more than 5 million C++ programmers out there. Compared with that Rust is but a drop in the ocean.