Archived

This topic is now archived and is closed to further replies.

Craazer

When does the combiler optimize?

Recommended Posts

If u use register or inline for example these are just suggestions to combiler, am I right? So can u know when combiler does the trick? Just curius..

Share this post


Link to post
Share on other sites
The "combiler" will do exactly what you tell it to do...

register is telling the "combiler" to mark a variable as cacheable to the CPU... Now, when the program is run the CPU might not cache it... If it does not have room in the cache to store the variable, it will just place it into system memory... However, the "combiler" will mark it cacheable no matter what, its just up the CPU to listen.

inline is telling the "combiler" to place the contents of a function into the place where the function is called... That kinda sounds like the "combiler" is changing your code. The "combiler" does not change your code, only the .obj files it generates while its being "combiled." To the programmer, it looks like any other function with just "inline" in front.. An inline function removes the overhead of allocating/deallocating of stack space and because of that inline functions runs faster. Small functions should always be inlined. The "combiler" will optimize small functions in release mode and automatically generate them as inline. However, it might not (depending on the quality of the optimizer), thats why you should specifically mark small (with no/low variables) functions as inline...


[edited by - JoeyBlow2 on January 28, 2003 6:11:08 PM]

Share this post


Link to post
Share on other sites
Some compilers take them as commands.
Some compilers take them as suggestions.
Some compilers ignore you, and work out what is best on their own.

For example, Microsoft''s CL (the VC++ compiler) ignores the "register" tag completely, and will optimize this sort of thing as it sees fit. However, it will not do any inlining without your say-so, although you do have to use "__inline" instead... "inline" is ignored, and considered "antiquated" (*spits* But it should be accepted for effing compatibility).

I don''t know for any other compilers, though. Read ''em up.

Share this post


Link to post
Share on other sites
huh.... I knew that "inline" could be ignored, but I did not know that "__inline" could overide the compiler monopoly. Altought, can it be dangerous to use it if the function is too big or complicated ?



/* Bullmax */
-------------
Reality has many forms : good and evil, black and white, yin and yang.

Share this post


Link to post
Share on other sites
quote:
Original post by JoeyBlow2
register is telling the "combiler" to mark a variable as cacheable to the CPU... Now, when the program is run the CPU might not cache it... If it does not have room in the cache to store the variable, it will just place it into system memory... However, the "combiler" will mark it cacheable no matter what, its just up the CPU to listen.
Unless I''m totally off with new systems, that''s not what''s going on. register marks a variable to be stored in a CPU register (eax, edx, etc.) rather than in memory; it has nothing to do with cache. I needs to be desided on when the compilation takes place, as it will generate different machine code.

Methods for marking instructions (or any memory) as cachable or uncachable, while possible with some processors, is AFAIK not implemented in standard C++. Some compilers might do such things on volatile variables on some platforms, but I doubt it.


Share this post


Link to post
Share on other sites
Guest Anonymous Poster
If my compiler actually <b>comb</b>iled, there would be no need for a deb<b>o</b>gger. No offense.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
The keywords "register" and "inline" are only suggestions to most compilers that you wish for certain optimizations to happen. The choice over these is usually down to the compiler.
The inline keyword means that the compiler would eliminate the overhead of calling the function by placing the functions code directly into the calling function, increasing speed but increasing the executable size at the same time. The compiler is free to ignore the inline keyword, as certain functions cannot be inlined, namely recursive functions. __forceinline is defined by Visual C++ to always force the function to be inlined, however I''m not sure what would happen if you did this to a recrusive function (try it). Any function defined within the definition of a class/struct "getbleh() { bleh = 1; }" is normally inlined by the compiler, though i''m not sure on the standards on this subject.
The register keywords means the compiler registers (eax, ebx, ecx, edx). The compiler has no control over the cache, however it does behind the scene use these for any variables used a lot (like loop variables). Once again it is only asking the compiler to use one of the CPU registers.

NB: inline functions can lead to rather large executables if used to often, so don''t inline everything. Large executables take more memory to store in the CPU, which means less space for holding any data that your executable wishs to use.

- Kehoeshi

Share this post


Link to post
Share on other sites
Actualy I knew what they are, but the queston is when do I know WHEN does the compiler optimize?

I mean can u see it in some log or shometing? Im using visual studio.

Share this post


Link to post
Share on other sites
quote:
Original post by Craazer
Actualy I knew what they are, but the queston is when do I know WHEN does the compiler optimize?
You usually can not; they are only suggestions to the compiler.
quote:
I mean can u see it in some log or shometing? Im using visual studio.
Check the assembly . You can make VC++ output the assembly to a file when assembling: Go to the Project | Settings menu and click C/C++ tab. Choose Listing files under category dropdown and one of the assembly options in the Listing file type dropdown.


Share this post


Link to post
Share on other sites
quote:
Original post by CWizard
[quote]Original post by Craazer
Actualy I knew what they are, but the queston is when do I know WHEN does the compiler optimize?
You usually can not; they are only suggestions to the compiler.
quote:
I mean can u see it in some log or shometing? Im using visual studio.
Check the assembly . You can make VC++ output the assembly to a file when assembling: Go to the Project | Settings menu and click C/C++ tab. Choose Listing files under category dropdown and one of the assembly options in the Listing file type dropdown.




Ookey, thanks.
Even I was hoping something cool.
Well I learnd new things about register and inline though, so thanks to all!

Share this post


Link to post
Share on other sites