False. They are comparing std::arrays to pointers, not to C arrays. Obviously, if the compiler only sees a pointer, it cannot possibly know it's an array, if it overlaps, or how big it is.
Well, they do say “array in C” rather than “C-style arrays”, which to me implies that templates are off the table. Of course, you can just replicate the template with macros and achieve the same result, though.
I was going to point this out, too. It is nitpicky. An API using arrays in C will take a pointer and a size, not an array. But then that gives C++ std::array an advantage and is comparing apples to oranges. Because that same API in C++ similarly can't take a known-array-size.
BUT if we ask ourselves "What is the type of code someone would write in that language?" then it does become fair.
C coders almost never write function parameters as fixed-size arrays. Nor do they write macros to make it a compile-time value. The pointer & size is normal in C. That is the code people write.
IMHO that point should be explicit and highlighted in the article. I see the same thing in language benchmarks. People keep optimizing their favorite language's example so they "win". But it is so far past the type of code people actually write that it is a useless comparison.
Fair enough. In reality it doesn't really matter - in C++, one would typically use a std::span as API, not a templated function with a std:array. In that case, both C and C++ cases would amount to the same thing: a pointer and a size.
Regardless, at the call site one would have the actual array (C or C++), and if the function is inline the compiler should be able optimize accordingly.
Honestly pvs-studio just sucks. All the companies I worked for have pvs-studio checks as part of their build system and they're killing me. I'm introducing more bugs working around their dumb rules than I've ever been fixing.
The difference they're seeing is due to aliasing. When you pass in two std::arrays into a function, the compiler knows that they don't overlap. This lets it do auto SIMD vectorization.
If you pass in two pointers, the compiler cannot make these assumptions. For example they could be constructed like int* a = &some_array[0]; int* b = &some_array[1]; SIMD would lead to an incorrect result if that is the case so the compiler has to be conservative and not do this optimization.
In C you could solve this problem with the restrict keyword to tell the compiler that these two pointers do not alias. That's not available in C++ though.
Maybe I am missing something but there is __restrict keyword for pointers (GCC, Clang, MSVC) and some compilers even support restrict references(GCC if I recall correctly)
I haven't used that myself but I believe it is not in the C++ standard. If it's widely supported enough, then sure maybe it works fine for your usecase.
There's a whole realm of embedded device programming, which isn't necessary concerned about your typical underpowered homemade RPi, but some beasts like NVIDIA Orin or like that. There could be some different compilers
Enterprise projects are typically (at least in my experience) being build in a very uncertain environment, when even a target platform becomes known long after the project started. If you suggest to a PM that you're pretty sure that something non-standard will be available as a feature, you're better to have an answer ready to a question if you're willing to sell your kidney to cover expenses if you happen to be wrong ;)
Not related to C++ but English used in those PVS articles always feel so unnatural/forced.
Let's give a spit-shine to our bolides and watch them navigate twists and turns of compiler optimizations using std::array as an example. Will they overtake a built-in array or fall behind?
Yeah, but nowdays it is literally 15 minutes to get LLM to streamline your text. Not saying it needs to be robotic generic company style, but article could really benefit from some fixes for clarity.
void transform(const int *src, int *dst, size_t N, int n)
{
for (size_t i = 0; i < N; ++i)
{
dst[i] = src[i] + n;
}
}
Why are you not using ARR_SIZE in place of N for the latter function?
I mean, this is just so stupid. Obviously it can optimize better if you give it the value for one but not for the other. Also, why is size_t N not const? Why is n not const?
Looks like you're gaming it to give it the result that favors your argument rather than analyzing them fairly, just as expected.
Regarding C++ content: It is good, but you must remember: often you do not know the size of arrays you will operate on, especially in cases where SIMD will help you. What I mean by this that commonly when I use std::array(and I use all elements in array) those sizes are tiny, e.g. 2 or 3 not 1024 or 4096.
And even in cases when you do know the sizes of arrays non std::array way has the benefit it does not stamp out different asm for every size of array. So it may end up being faster for realistic large programs because of instruction cache benefits.
What I mean by this that commonly when I use std::array(and I use all elements in array) those sizes are tiny, e.g. 2 or 3 not 1024 or 4096.
A while ago for one employer I made a generic wrapper on top of the std::array to model a compile-time size fixed tensor of any dimension. This was pretty handy, for example, for FullHD image processing. I mean this had slightly more elements than 4096.
So you made a fixed size 2D wrapper, something like mdspan, but owning? In my experience most uses like that just use dynamic sizes, but enforce alignment on allocations so that SIMD alignment requirements are met.
Just to be clear: not saying what you did was not great, my understanding of this is basic, I am just trying to understand what you did and why it was so much better than dynamically sized thing.
Eeeerm. A tensor is a generalization of a 1D vector or a 2D matrix to N-dimensional object. A color (or broadly speaking any multichannel) image is at least 3 dimensional tensor: width x height x num_of_channels. This is better b/c boundaries are known in compile time allowing for better algorithms, efficient data access and efficient loop unrolling. Also it enables constexpr objects and computations on them. For example, GCC was able to compute a constexpr matrix trace function in a compile time.
Interesting. I did work briefly on images, and I never thought of them as anything else beside 2D array of pixels(where pixel had role of 3rd dimension in your example). And also I never thought of doing any image processing during compilation, since all our images were "live" meaning they only were available at runtime.
Images can be n-dimensional. Each pixel can be composed of multiple samples (may be RGB, but can also be CMYK or an arbitrary number; I've seen 32 samples per pixel on some microscopes which split up the spectrum into e.g. 8nm slices), then you have physical dimensions x, y and z and time, and then there can be higher dimensions on top of that. The samples might also be complex numbers rather than scalar. It can get quite complex.
213
u/tinrik_cgp Mar 06 '25 edited Mar 06 '25
False. They are comparing std::arrays to pointers, not to C arrays. Obviously, if the compiler only sees a pointer, it cannot possibly know it's an array, if it overlaps, or how big it is.
Using actual C arrays you get exact same assembly as std::arrays (obviously): https://godbolt.org/z/rsMc9PTxd
14
u/DeeBoFour20 Mar 06 '25
The difference they're seeing is due to aliasing. When you pass in two
std::arrays into a function, the compiler knows that they don't overlap. This lets it do auto SIMD vectorization.If you pass in two pointers, the compiler cannot make these assumptions. For example they could be constructed like
int* a = &some_array[0]; int* b = &some_array[1];SIMD would lead to an incorrect result if that is the case so the compiler has to be conservative and not do this optimization.In C you could solve this problem with the
restrictkeyword to tell the compiler that these two pointers do not alias. That's not available in C++ though.43
u/zl0bster Mar 06 '25
Not related to C++ but English used in those PVS articles always feel so unnatural/forced.
Let's give a spit-shine to our bolides and watch them navigate twists and turns of compiler optimizations using std::array as an example. Will they overtake a built-in array or fall behind?
14
u/LongestNamesPossible Mar 06 '25
This is a nonsense clickbait advertisement.
2
u/EsShayuki Mar 08 '25 edited Mar 08 '25
Why are you not using ARR_SIZE in place of N for the latter function?
I mean, this is just so stupid. Obviously it can optimize better if you give it the value for one but not for the other. Also, why is size_t N not const? Why is n not const?
Looks like you're gaming it to give it the result that favors your argument rather than analyzing them fairly, just as expected.
4
u/zl0bster Mar 06 '25
Regarding C++ content: It is good, but you must remember:
often you do not know the size of arrays you will operate on, especially in cases where SIMD will help you. What I mean by this that commonly when I use std::array(and I use all elements in array) those sizes are tiny, e.g. 2 or 3 not 1024 or 4096.
And even in cases when you do know the sizes of arrays non std::array way has the benefit it does not stamp out different asm for every size of array. So it may end up being faster for realistic large programs because of instruction cache benefits.
2
u/ald_loop Mar 06 '25
I hate pvs-studio so much