Live data from Hacker News

Does C++ still deserve a bad rap?

nibblestew.blogspot.com

301–310 of 310 posts

Re: Does C++ still deserve a bad rap?

#301
post #159

Earlier quoted context omitted.

> That assertion makes zero sense if we take into account that parallelization is one of the most basic performance techniques there is I would not support that stance. There are areas where parallelization is helpful, like numerical weather models, graphics, and scientific number crunching, but for by far the most cases, parallelization is anything but trivial and requires a pretty deep knowledge about things like t…

Its funny that you mentioned games as an example of parallel computation because I'd argue they're some of the hardest programs to parallelize effectively since they don't generally involve that much bulk-processing of read-only data. Parellism in C++ is most often used for scientific applications and other forms of mass number crunching. It's really easy to just throw a "#pragma omp parallel for" on a loop and call…

> Parallelism in C++ is most often used for scientific applications and other forms of mass number crunching.

There are two aspects. I agree to the aspect that today, C++ is used often for such tasks.

Now, what are the reasons why C++ is used dominantly in this domain? I think more or less the only reason is performance. The good performance is what makes the authors of such libraries put up with the disadvantages of C++.

However, I think it is also true that Rust allows for a more concise and safe formulation of the computation (also Scala, for example, which serves somewhat different purposes). For scientific applications, correctness matters, and knowing that the code which compiles does not has hidden memory errors and data races is extremely valuable, because it can save a ton of time.

Also, if there are still differences between Rust and C++ performance, they are minor. In many cases, Rust is faster.

Now, if authors of scientific computing libraries were to come to the conclusion that there exist an alternative which produces at least as fast code, provides better and safer support for parallelization, and is with some learning easier to work with, why should these library authors continue to use C++?

Of course, nobody is going to ditch a large C++ project like Eigen over night and rewrite it in Rust. There is too much inertia for that. Also, GCC support is still lacking. However, one can expect that the number of new projects which use Rust is going to increase and the project which are successful there will blaze a new trail. For something like Python extension modules, users of these libraries do not need to know anything about Rust.

Also, some nitpick. C++ is used in important scientific libraries. However, many essential libraries such as Numpy are written either completely in C, or use C interfaces, because C++ does not has a stable ABI and Python uses the C ABI. This would make a switch to Rust pretty easy. In fact, I think the impulses in this domain will come first from researchers and analysts which start to write small Rust extension for Python which use the C ABI and integrate with Numpy, for example.

Re: Does C++ still deserve a bad rap?

#302
post #289

Earlier quoted context omitted.

> C++ can be vastly simpler and more readable, while retaining 100% control over memory and layout. But unlike assembly, you don't have control over CPU flag registers and special instructions. Can you explain why and when, exactly, 100% control over memory is needed? Outside of OS kernels, high-frequency trading engines, and real-time control systems? If using assembly is too costly, why is using C++ economical? And…

You don't have direct control over caches, however if you have control over memory layout you can indirectly influence how the code performs by leveraging the proper cache sizes/lanes. If that's your goal, you cannot focus on program flow alone, and that's the main point. Low-level and high-performance code is not "pretty" by modern standards, as it often requires DMA and tight control over data placement in order to…

> Low-level and high-performance code is not "pretty" by modern standards, as it often requires DMA and tight control over data placement in order to fully leverage instruction-level and hardware-level parallelism.

This is true if you look at implementations like these:

https://benchmarksgame-team.pages.debian.net/benchmarksgame/...

for example:

https://benchmarksgame-team.pages.debian.net/benchmarksgame/...

vs.

https://benchmarksgame-team.pages.debian.net/benchmarksgame/...

I'd say the C++ code in this particular case is not only much longer but also uglier.

However, how much of a fraction of C++ code in use does really require direct control over DMA calls? If you could get an at least equally fast result as when writing "normal C++" in Rust, with more safety and less effort for debugging, would this not be tempting for many people using C++? Because this is what I observed in my case. And this given that I've written no more than a few hundred lines of performance-critical code in Rust, and have worked for > 10 years on performance-critical code in C++ and C.

Re: Does C++ still deserve a bad rap?

#303

Earlier quoted context omitted.

I would love to know an example of that.

https://drewdevault.com/2020/09/25/A-story-of-two-libcs.html

That quotes a glibc comment "we also support negative `signed char' values for broken old programs" which means the OP code is in fact going to behave OK with glibc, providing that "support for broken old programs" is never removed.

Re: Does C++ still deserve a bad rap?

#304
post #289

Earlier quoted context omitted.

> C++ can be vastly simpler and more readable, while retaining 100% control over memory and layout. But unlike assembly, you don't have control over CPU flag registers and special instructions. Can you explain why and when, exactly, 100% control over memory is needed? Outside of OS kernels, high-frequency trading engines, and real-time control systems? If using assembly is too costly, why is using C++ economical? And…

You don't have direct control over caches, however if you have control over memory layout you can indirectly influence how the code performs by leveraging the proper cache sizes/lanes. If that's your goal, you cannot focus on program flow alone, and that's the main point. Low-level and high-performance code is not "pretty" by modern standards, as it often requires DMA and tight control over data placement in order to…

> Your example about vector is not really surprising. Do you want the iterator to be efficient, or consistent?

The thing is, what cppreference says here is that this innocent-looking code might either not compile or cause undefined behavior in whatever part of the program. In my case on GCC 8, it caused memory leak warnings with address sanitizer. For larger programs and especially for programs which have hard requirements on real-time latencies, safety or robustness, this is not acceptable.

And yes, an experienced C++ developer will initially need a little more time to write the same code in Rust, compared to C++. (I do not think this is true for developers new to C++ because C++ is a very large language). However, in any larger program, the time spent for testing and debugging is very likely larger than the time spent for typing in the first code. And in this metric, Rust's strict approach to correctness is far better.

And if you get to debug undefined behavior in large multi-threaded programs, the time you can spend debugging code is basically unbound if you were were not both experienced and careful with writing first.

Re: Does C++ still deserve a bad rap?

#305
post #143

Earlier quoted context omitted.

> There isn't any point of using C++ to count words in a text file. Any high-level language like Python will beat you to it. Well, I think that Python will probably do that in the shortest code but it is also more or less the slowest language which is today widely in use, at frequently about 1/50 the speed of C. You of course could write the example in the link in Rust but for a quick script with decent performance,…

I feel like the fastest growing and most dominant area for C++ is scientific computing and AI/ML. Although many people write these programs in more user-friendly languages like Python and R, most of these programs call through to C++ frameworks (which many people use directly too), so in a way C++ has latched itself to the growth of such languages. Furthermore many major accelerated computing platforms (frameworks?)…

To avoid double-posting the same content, please see my other reply here on about the same topic: https://news.ycombinator.com/item?id=24821043

Re: Does C++ still deserve a bad rap?

#306

Earlier quoted context omitted.

for people that dont like headers, how do you browse the api of a class? Scrolling through the implementation?

a proper, standardized documentation system like godoc and rustdoc

I'm down for that, but many languages without headers don't have that by default.

Re: Does C++ still deserve a bad rap?

#307
post #283

Earlier quoted context omitted.

For data-intensive applications, which are only increasing, C++ is the only game in town. State-of-the-art architectures typically rely on schedule-based safety models that are not productively expressible within Rust's ownership-based safety model, and the performance characteristics of working within these respective safety models is not comparable. There aren't many alternatives when a GC language is a non-starter…

What exactly do you mean by "schedule-based safety model"?

Many safety issues in complex compute environments are predicated on the notion that many different threads/contexts can manipulate the data structures in somewhat arbitrary and unpredictable orders. This is approximately correct for classic multithreaded software.

In some software architectures, there are schedulers with a global view of the entire (potential) conflict graph and they have complete control of what gets executed when. These architectures don’t even require locks because the scheduler has enough visibility and control to guarantee that execution won’t be scheduled such that there would ever be a contended lock or some other concurrency conflict. No amount of mutable references to the same memory will break these models, and the correctness of some implementations have been formally verified. The scheduler can always dynamically reorder execution to guarantee the invariants of the system. These models have the added benefit of having insanely good locality properties such that throughput is excellent.

These software architectures originated in HPC over a decade ago and eventually bled over into high-end database kernels. I learned it from when I worked in HPC many years ago and have used it every since, due to its unambiguous advantages.

Re: Does C++ still deserve a bad rap?

#308
post #277

Earlier quoted context omitted.

Yeah I wouldn't use C++ for building web applications.

Which is the majority of applications nowadays.

You would use C++ for a service or very focused component that does one thing well (like a database, or crunching numbers). For those, even Rust is not always a good answer.

You shouldn't use C++ as the backend for your marketing site, or some simple crud backend where a JIT runtime with GC will do.

Hell, if you're even building a small compiler today a JVM language or Go is a better choice.

But if you need to allocate memory yourself, C/C++ is very hard to beat. Games and systems dealing with huge chunks of memory will always go this route... and just (most likely) copy the dependencies into version control and deal with things that way.

It doesn't matter if the majority of applications are web applications. That doesn't make C++ less relevant because that was never the target audience.

Re: Does C++ still deserve a bad rap?

#309
post #283

Earlier quoted context omitted.

What exactly do you mean by "schedule-based safety model"?

Many safety issues in complex compute environments are predicated on the notion that many different threads/contexts can manipulate the data structures in somewhat arbitrary and unpredictable orders. This is approximately correct for classic multithreaded software. In some software architectures, there are schedulers with a global view of the entire (potential) conflict graph and they have complete control of what ge…

Do you have any references to HPC systems that exemplify this approach?

Re: Does C++ still deserve a bad rap?

#310
post #283

Earlier quoted context omitted.

What exactly do you mean by "schedule-based safety model"?

Many safety issues in complex compute environments are predicated on the notion that many different threads/contexts can manipulate the data structures in somewhat arbitrary and unpredictable orders. This is approximately correct for classic multithreaded software. In some software architectures, there are schedulers with a global view of the entire (potential) conflict graph and they have complete control of what ge…

Thanks! Sounds interesting, but it's not obvious to me that such a framework can't be captured in the Rust type/ownership system in a reasonably ergonomic way. Has anyone even tried doing that?

For example you might be able to wrap shared-mutable state in a kind of degenerate mutex and pass a "scheduler guarantee" token around that unlocks those "mutexes" without doing any runtime work.

Post reply on HN