C++ - Reddit
226 subscribers
48 photos
8 videos
1 file
25.7K links
Stay up-to-date with everything C++!
Content directly fetched from the subreddit just for you.

Join our group for discussions : @programminginc

Powered by : @r_channels
Download Telegram
Less Code Doesn't Mean Faster Code

Take, for example, a simple reduction: summing all the elements in an array...

template<typename T>
T vectorreducesimple(T src, size_t len, T initial)
{
for(size_t i = 0; i < len; i++)
{
initial += src[i];
}
return initial;
}

Or this version:

template<typename T>
T vector_reduce_cpu(const T
src, sizet len, T initial /* = 0 */)
{
while( len >= 4 ) {
T r00, r01, r02, r03 ;
r00 = src[0] ;
r01 = src[1] ;
r02 = src[2] ;
r03 = src[3] ;

r00 += r02 ;
r01 += r03 ;

r00 += r01 ;

initial += r00 ;

len -= 4 ;
src += 4 ;
} // while
while( len >= 2 ) {
T r00, r01 ;
r00 = src[0] ;
r01 = src[1] ;

r00 += r01 ;

initial += r00 ;

len -= 2 ;
src += 2 ;
} // while
while( len >= 1 ) {
T r00 ;
r00 = src[0] ;

initial += r00 ;

len -= 1 ;
src += 1 ;
} // while

return initial ;
}

On an i9 CPU, with T being 'double', in optimal cases (i.e., a length of about 2000-7000), the longer version runs 4x faster when compiled with gcc and -O3. Why?

Even going back to the 1980s, with the [68882](
https://en.wikipedia.org/wiki/Motorola68881) FPU, floating-point units could perform operations in parallel. In the shorter case, that parallelism is defeated by the accumulation occurring solely through the 'initial' variable.

By introducing more variables, the additions can occur in parallel. The compiler also optimizes memory by interleaving variable loads with additions, which can also run in parallel.

So what's the takeaway?

Just turning on -O3 isn't enough
It pays to be aware of the hardware you're going to run on

The deep dive: https://github.com/AndrewOfC/vector\_reduction

https://redd.it/1yac9ay
@r_cpp
I don’t even hate AI, I just feel a little sad about being obsolete

I felt cool and smart that I understand low level programming, pointers and memory management and all that stuff. I still program for fun as a hobby but if I start thinking about this stuff I lose my momentum. I like to write my code like an engineer and I am very specific about how I write it. I think AI has good uses for the future but it just makes me sad to think about how anyone can AI generate code and it makes my deep understanding not as cool anymore. My projects matter less, anyone could just instantly generate the code for the game I spent years on. I’m not special anymore nor are my projects. It’s just a little sad. I think about C++26 and C++29, is anyone even going to care about these things anymore with the obsolescence of people who use these features by hand… idk. I get in ruts like this someone and I just have to ignore it. I want to hear everyone else’s opinions especially ones who are very opinionated about programming features and care about their code very much like I do.

Sometimes though I think about ways I program that an AI wouldn’t do, such as my excessive and opinionated use of unsigned integers with explicit cstdint types and the lowest possible bit amount for the variable, while AI will just use standard int. Another one is how I try to avoid dynamic allocations whenever possible, even for silly things like microoptimiaztions, for file paths I’ll do a 4096 stack buffer over dynamic allocation. Makes me feel like I have somewhat of a voice.

https://redd.it/1yajs5y
@r_cpp
Why we shouldn't want a remote package registry, universal package manager or a binary toolchain orchestrator

Hi, as most of you now, we get cargo good cmake bad posts and comments here nearly every week.

I want to write a definitive post to answer these criticisms. In this post I'm going to argue for a decentralized approach instead, and will try to list some cons and pros of this approach.

Cargo is the package manager of the rust language, it stores packages in source form in a remote registry called crates.io, It also sort of acts as a build system, as you invoke it directly, rather than calling the package manager first then the build system. The problem with this approach is that you don’t own sources, you don’t know about licenses. If one of your packages break, trying to fix it is very annoying. Also builds are very slow, since you don’t have local binary cache for packages. Additionally, unlike a sane dependency model as proposed by Titus Winters. Cargo relies on semver. This means your dependency tree will not scale and will be hard to maintain.

Rustup is a toolchain orchestrator tool that downloads compiler, stdlib and various other tools from the internet.

It seems very nice to be able to setup a toolchain in seconds, as opposed to doing that with c++ which takes very long.

The drawback of this approach is the lack of knowledge about dependencies of your tools wnd especially stdlib. You need to know what version of xcode or msvc you app is compiled against.

What to do then?

We need a decentralized solution, organizations should clone their deps from a remote and write package recipes for them, we need some sort of organization to collaborate and several standards to make this easier, such as a binary package format.

Cargo and rustup are currently more closer to web technologies in toolchain departmant than c++, I don’t see why everyone is so obsessed about it. There is a new slop project written on it every minute, and we think that's a good thing. It is not, it made library quality very questionable.

Wrote on my phone so probably a lot of typos.

https://redd.it/1yaj399
@r_cpp
More community member should join this community

I think many of our discussions went unseen by community member very few member are there in the community who are active I think .If there more community members the discussion will come to some result,some point . What is your opinion on it?

https://redd.it/1yaneyi
@r_cpp