Board index » cppbuilder » Is __fastcall needed?
Rudy
![]() CBuilder Developer |
Rudy
![]() CBuilder Developer |
Is __fastcall needed?2005-12-07 11:25:51 PM cppbuilder91 Just wondering why every IDE-created function uses the __fastcall modifier, and should I use it as well on functions I create? I don't understand the purpose of this keyword. Thanks, Rudy |
Ed Mulroy
![]() CBuilder Developer |
2005-12-08 12:47:31 AM
Re:Is __fastcall needed?
The VCL uses __fastcall calling conventions so things which work with it
must use it. Normal functions and functions in non-VCL classes are free to use most calling conventions, __cdecl, __fastcall or __stdcall. If you do not specify a calling convention then __cdecl is used. A calling convention is the method whereby arguments to a function are passed and how the stack is handled. __fastcall usually loads calling arguments into registers which, for a small number of arguments, is often faster. __cdecl and __stdcall push the calling arguments on the stack but differ in the order in which they are pushed and differ on who, the caller or called function, frees the stack memory used by the calling arguments. __fastcall, __cdecl and __stdcall have descriptions in the compiler's help. . Ed QuoteRudy wrote in message |
Nate Lockwood
![]() CBuilder Developer |
2005-12-08 11:52:49 PM
Re:Is __fastcall needed?
Ed Mulroy wrote:
QuoteThe VCL uses __fastcall calling conventions so things which work with it bool foo(const int); // foo does not reference any vcl objects Since this is declared within a vcl class should it use __fastcall? Thanks for clearing this up. Nate {smallsort} |
Andrue Cope [TeamB]
![]() CBuilder Developer |
2005-12-09 12:04:24 AM
Re:Is __fastcall needed?
Nate Lockwood wrote:
QuoteSince this is declared within a vcl class should it use __fastcall? handlers simply because you have to match the original function signature. __fastcall favours putting function arguments into registers and for small, frequently called functions it can provide a useful performance boost. In practice I would imagine any such performance boost would be dwarfed by the inherent issues the VCL faces in being event driven. I've never really understood why __fastcall was chosen for the VCL. Using it too much dilutes its effect so ISTM that it's better to use it only when you have to. Wholesale use seems to me to be likely to do more harm than good. -- Andrue Cope [TeamB] [Bicester, Uk] info.borland.com/newsgroups/guide.html |
Alisdair Meredith[TeamB]
![]() CBuilder Developer |
2005-12-09 05:57:37 AM
Re:Is __fastcall needed?
Rudy wrote:
Quote
You need to use it in 3 places: i/ Event handlers for VCL code. Event handlers are callback functions. You effectively pass a function pointer to your form and say 'call this function when the event fires'. All that logic is written in the Delphi part of the product, so your function pointer must use the correct convention for Delphi to call it - __fastcall. That is why all event handlers the IDE creates for you are declared __fastcall. ii/ When you are overriding a virtual function that was declared using __fastcall. Remember when I said the VCL is implemented in Delphi? That means all virtual functions in the VCL are declared as __fastcall, so you need to use __fastcall when you override them. In particular, that means the constructor and destructor of your form class. And yes, that does mean your form constructor is a virtual override, something no normally allowed in C++ which does not support virtual constructors. iii/ When you want to put your own event handlers into a __published part of your class, normally when writing custom controls. By the time you need this, you will already understand the issue quite well! The rest of the time, you are free to choose whether or not you use __fastcall, it should not be an issue either way. When we first started using BCB almost 9 years ago, we routinely made every function __fastcall, as it seemed to be the expected thing. The problem with this was that our C++ code depended on a Borland extension and could not be used in any other compiler, and looked strange when talkng with other C++ users. We now have gone to the opposite extreme - we never use __fastcall unless it is absolutely required. This has proven more maintainable to us over the long run. If nothing else, our code is a lot more readable without the extra keyword everythere! The important thing is to pick a consistent set of rules and stick to them. -- AlisdairM(TeamB) |
Nate Lockwood
![]() CBuilder Developer |
2005-12-09 09:38:57 AM
Re:Is __fastcall needed?
Andrue Cope [TeamB] wrote:
QuoteNate Lockwood wrote: |