Compiled reality

General discussion for topics related to the FreeBASIC project or its community.
Provoni
Posts: 514
Joined: Jan 05, 2014 12:33
Location: Belgium

Re: Compiled reality

Post by Provoni »

deltarho[1859] wrote:In the opening post replace

Code: Select all

dim as ulongint i,j
with

Code: Select all

dim as ulongint i
dim shared as ulongint j
and the loop is no longer optimized out.

Compiled with -gen gcc -Wc -O3
Interesting! Is that because it allows scenarios in which the compiler is not able to see/determine if the shared variable is used or not? And thus it keeps the loop to be safe?
deltarho[1859]
Posts: 4308
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Compiled reality

Post by deltarho[1859] »

Provoni wrote:Is that because it allows scenarios in which the compiler is not able to see/determine if the shared variable is used or not?
I am sure that the compiler could be made to do that but I should not think that would come cheap.

I originally figured that there was a fair chance that the compiler would not interfere with a global scope. That was just a 'gut feeling' - no real grounds for supposing that. However, I think that there is more to it than that. It may be because we have put 'j' into the application's .data section but, again, I have no grounds for supposing that either.

Perhaps someone better acquainted with compilers can provide an answer.
anonymous1337
Posts: 5494
Joined: Sep 12, 2005 20:06
Location: California

Re: Compiled reality

Post by anonymous1337 »

Compilers are best at optimizing individual functions, and sometimes good at optimizing their immediate context. Compiler authors aim to make optimizations that are as close as possible to theoretically, deterministically proven not to cause harm. They don't always succeed. Sometimes, compilers will avoid optimizations that it can't predictably say will not have any effect on the programs.

Depending on how you write your code, there are PLENTY of optimizations that will be missed... Also, a compiler can only optimize so much. Well-written code goes much further.

Yes, individual algorithms can be greatly optimized with rearrangements and distribution of work between CPU and memory, but which algorithms and data structures used will ultimately have the biggest impact.

When you're working in something like C++, there isn't too much background processing going on to begin with. I think overall, optimization improves specific algorithms greatly, but not too much for overall application performance in many applications. Ex: If you use even a single slow library, that will be the biggest bottleneck.

Not to digress too much, but I'm seeing even C++ get support for patterns that make it easier to code. The real neat stuff won't be in C++ for another 5 - 10 years possibly, but the C++ roadmap is trending in the right direction. If C++ had moved faster in this way, I think a lot of modern, dynamic programming languages would be executing much faster and have different syntax and semantics.
deltarho[1859]
Posts: 4308
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Compiled reality

Post by deltarho[1859] »

anonymous1337 wrote:Also, a compiler can only optimize so much. Well-written code goes much further.

Yep, we cannot "make a silk purse of a sow's ear".
grindstone
Posts: 862
Joined: May 05, 2015 5:35
Location: Germany

Re: Compiled reality

Post by grindstone »

deltarho[1859] wrote:Yep, we cannot "make a silk purse of a sow's ear".
Unless you accomplish to breed a silk sow. <grin>
deltarho[1859]
Posts: 4308
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Compiled reality

Post by deltarho[1859] »

grindstone wrote:Unless you accomplish to breed a silk sow. <grin>
Quite so and likely to be seen beneath a gaggle of low flying pigs.
Post Reply