News for hackers. Stuff that matters.
Re:Bubble sort slower with -O3 than -O2 with GCC by codr7 on Sunday October 17, 2021 @11:47AM (#28897280)
I've written a lot of interpreters in C/++ lately, and spent a lot of time profiling and optimizing them.

I can't remember -O3 ever being faster than -O2, it's usually slightly slower for me.

Re:Bubble sort slower with -O3 than -O2 with GCC by DannyBee on Sunday October 17, 2021 @11:55AM (#28897350)
For interpreters, that's highly likely to be true, especially if they are mainly spending time in switch tables or executing basic opcodes.
Re:Bubble sort slower with -O3 than -O2 with GCC by infberg on Sunday October 17, 2021 @12:31PM (#28897636)
Care to explain? -O3 generates larger code than -O2?
Re:Bubble sort slower with -O3 than -O2 with GCC by pclmulqdq on Sunday October 17, 2021 @12:57PM (#28897836)
Yes, -O3 tends to include a lot of features that increase code size, like aggressive loop unrolling. If you are jumping around a large amount of code, -O3 generally performs more poorly than -O2, but if you are running a tight loop (like HPC code), -O3 is better.

In the past, at a time when I worked on a very performance sensitive codebase that was also limited in scope, we compiled with -Osize and did all the loop optimizations we wanted manually (and with pragmas). That produced faster code than -O2 or -O3.

Re:Bubble sort slower with -O3 than -O2 with GCC by gnufx on Sunday October 17, 2021 @02:14PM (#28898458)
Regarding unrolling, -O3 contains -funroll-and-jam but not -funroll-loops. You may want one or the other, maybe both, depending on circumstances. I don't see much benefit from the available pragmas on HPC-type code unless for OpenMP, and "omp simd" isn't necessary to get vectorization in the places I've seen people say it is. Mileage always varies somewhat, of course. (Before second-guessing anything, use -fopt-info.)
Re:Bubble sort slower with -O3 than -O2 with GCC by boibombeiro on Sunday October 17, 2021 @11:21PM (#28901938)
Modern x86 CPUs have micro instr caches to store small loops (about 50 instr) and medium loops (~2k instr). Also, the bottleneck is usually the instruction decoding (Alder Lake made huge changes on that, so this might change).

In other words, loop unrolling is, more often than not, harmful.

Re:Bubble sort slower with -O3 than -O2 with GCC by jleahy on Sunday October 17, 2021 @02:52PM (#28898770)
It’s a shame that Osize can sometimes produce truly awful code. There are a few optimisations in there that trade a byte for a massive slowdown.
Re:Bubble sort slower with -O3 than -O2 with GCC by userbinator on Sunday October 17, 2021 @04:46PM (#28899649)
You asked for minimum size, and that's what you got. I'd say that's working as it should.

A more granular control over optimisation would be good, however.

Re:Bubble sort slower with -O3 than -O2 with GCC by jleahy on Monday October 18, 2021 @08:07AM (#28904566)
Probably just some tweaks to O2 would be enough, after all people are selecting Os over O2 because they see better performance, and that should not be happening.
Re:Bubble sort slower with -O3 than -O2 with GCC by kevin_thibedeau on Sunday October 17, 2021 @07:31PM (#28900683)
You can enable/disable individual optimizations. How much more granular do you need?
Re:Bubble sort slower with -O3 than -O2 with GCC by jhgb on Sunday October 17, 2021 @04:15PM (#28899409)
Surely a profile-guided build should be able to only apply -Os to those functions where it doesn't cause a lot of problems.
Re:Bubble sort slower with -O3 than -O2 with GCC by pclmulqdq on Sunday October 17, 2021 @07:07PM (#28900578)
In the application I referred to, PGO was also used. However, that only applies -Os to cold code, and if what you're doing is very branchy, it can help even in the hot path.
Re:Bubble sort slower with -O3 than -O2 with GCC by gnufx on Sunday October 17, 2021 @02:07PM (#28898400)
Python, specifically, is supposed to get most benefit from -fno-semantic-interposition
Re:Bubble sort slower with -O3 than -O2 with GCC by nly on Sunday October 17, 2021 @12:32PM (#28897641)
What about when you factor in PGO? (-fprofile-generate & -fprofile-use)
Re:Bubble sort slower with -O3 than -O2 with GCC by mhh__ on Sunday October 17, 2021 @01:03PM (#28897881)
This is the rub. the compiler is flying completely blind without PGO.

I turned on PGO for the D compiler recently and it compiled it's tests 15% faster. Boom.

Re:Bubble sort slower with -O3 than -O2 with GCC by codr7 on Sunday October 17, 2021 @01:20PM (#28898019)
Haven't really been down that rabbit hole yet, thanks for reminding me.