isocpp / CppCoreGuidelines

The C++ Core Guidelines are a set of tried-and-true guidelines, rules, and best practices about coding in C++
http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines
Other
42.78k stars 5.44k forks source link

CP: new rule proposal #1233

Open imre-palik opened 6 years ago

imre-palik commented 6 years ago

CP.0: Concurrency and Parallelism are performance optimisation techniques. So all the rules about performance optimisation apply here.

MikeGitb commented 6 years ago

Concurrency and Parallelism are performance optimisation techniques.

Parallelism for sure Concurrency not necessarily.

hsutter commented 6 years ago

Editors: What is the proposed rule? We don't think these are the same as other performance optimization. Concurrency is about overlapping/hiding waiting (latency), parallelism is about using more hardware to get the answer faster (but at the possible expense of some other computation). So they're not really about optimizing a particular function in isolation, or what did you have in mind?

imre-palik commented 6 years ago

I think, outside of the world of general purpose library developpers, optimization is never done by tweaking functions in isolation. It is about fulfilling the whole program's (or possibly the whole device's) non-functional requirements relating to speed, size, or power consumption.

scraimer commented 6 years ago

@imre-palik My own experience has shown me that optimizing a single function, or even a single line in a function, has often removed 80% of the latency of a bottleneck. In HFT, while speed is not a functional requirement, it is an essential requirement. Two functions that fulfill the same functional requirements are absolutely not the same if one is 10 times faster than the other.

I'm not sure what you meant by opening this issue. Could you write out the rule you are proposing with more detail? Usually guidelines have a several sections: reason, notes, enforcement, a "bad" example, and a "good" example, etc. Please write as many of them as you think appropriate.

Or are you just suggesting adding a single line to the introduction the section "CP: Concurrency and parallelism" ? The line from your first comment about "all the rules about performance optimisation apply here." ?

MikeGitb commented 6 years ago

A lot of the rules in performace section boil down to: Only go for complicated and or low/level optimizations when you have to and measure to make sure, your "optimization" actually made the program faster.

The same applies to parallel programming: Only employ it where necessary, stay high level and measure before and afterwards.

scraimer commented 6 years ago

@MikeGitb When I read the rules in the CP section, most of what I'm seeing can be badly paraphrased as "This C++ myth is incorrect and will lead you to write bad code, and here's why". But each rule is tailored for each individual myth. The point being that you could give a link to a single rule to someone during a discussion, and that's all they'd have to read.

In a way, the rules expand on the basic tenant you wrote: "Only employ it where necessary [...]" by explaining for each rule what "necessary" really is. I don't expect the target audience of the guidelines to always be able to fully unpack your phrase into each of its components. I know I can't. I'm sure I'll miss something. Just off the top of my head, I know that there's a hefty latency cost for switching from non-AVX to AVX instructions for vectorization in Intel CPUs. But that's not something I'd like to see in the C++ guidelines, since it's specific to a certain architecture. Instead, that information falls in the "measure before and after": something that will be obvious from the results if it's relevant.

imre-palik commented 6 years ago

It might be obvious to you, but according to my experience when this comes to parallelism, it is an offten forgotten rule. Parallelism is somewhat like template metaprogramming. Some folks like the intellectual challenge, and try to use it even when it doesn't make much sense. (Been there, done that, ...)

Similarly to template metaprogramming, these mistakes are sometimes presented at renowned conferences, dressed up to look like success.

scraimer commented 6 years ago

@imre-palik: Ah, I think I see what you mean.

Would a warning/disclaimer at the top of the CP section be good? Something like what you had originally wrote:

Warning: Please stop and consider if parallelism is necessary for the code you are writing. Parallelism is a performance optimisation technique. Like all optimizations, it should not be used prematurely or needlessly. Consider a non-parallel solution instead.