inline methods and pointers to members 
October 11th, 2005, 02:25 AM
| | | inline methods and pointers to members
Can the compiler ever inline a method when there is a pointer to the
member used?
Thanks,
--John Ratliff | 
October 11th, 2005, 02:35 AM
| | | Re: inline methods and pointers to members
* John Ratliff:[color=blue]
> Can the compiler ever inline a method when there is a pointer to the
> member used?[/color]
Do you mean, "when there is a pointer to the method used", and by "method" you
mean non-static member function?
If so, yes, it can.
Or, depending on what you mean by "compiler", and how tied to 2005 technology
you want the answer, with e.g. MSVC it's the linker that does this job.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail? | 
October 11th, 2005, 04:35 AM
| | | Re: inline methods and pointers to members
John Ratliff wrote:[color=blue]
> Can the compiler ever inline a method when there is a pointer to the
> member used?[/color]
If the question is: does taking the address of a function (or a method)
prevent the compiler from inlining that function, the answer is "no".
Remember that function calls - not functions themselves - are inlined.
Whether the compiler decides to inline a particular function call has
no relation to other code that may take the address of the function
whose call is being inlined.
If the question is: can a function call through a function (or member)
pointer be inlined? The answer is "no" for any sensibly-written
program. To inline a function call through a pointer, the compiler
would have to be certain that the pointer would always point to the
same function on every call. And the compiler would have to be able to
deduce which function the pointer points to. But if the function
pointer never varies, why would the program bother with it and not just
call the pointed-to function directly? Use of a function pointer only
makes sense when the pointer has more than one function it could be
pointing to.
Greg | 
October 11th, 2005, 04:35 AM
| | | Re: inline methods and pointers to members
John Ratliff wrote:[color=blue]
> Can the compiler ever inline a method when there is a pointer to the
> member used?[/color]
If the question is: does taking the address of a function (or a method)
prevent the compiler from inlining that function, the answer is "no".
Remember that function calls - not functions themselves - are inlined.
Whether the compiler decides to inline a particular function call has
no relation to other code that may take the address of the function
whose call is being inlined.
If the question is: can a function call through a function (or member)
pointer be inlined? The answer is "no" for any sensibly-written
program. To inline a function call through a pointer, the compiler
would have to be certain that the pointer would always point to the
same function on every call. And the compiler would have to be able to
deduce which function the pointer points to. But if the function
pointer never varies, why would the program bother with it and not just
call the pointed-to function directly? Use of a function pointer only
makes sense when the pointer has more than one function it could be
pointing to.
Greg | 
October 11th, 2005, 04:55 AM
| | | Re: inline methods and pointers to members
Alf P. Steinbach wrote:[color=blue]
> * John Ratliff:
>[color=green]
>>Can the compiler ever inline a method when there is a pointer to the
>>member used?[/color]
>
>
> Do you mean, "when there is a pointer to the method used", and by "method" you
> mean non-static member function?
>
> If so, yes, it can.
>
> Or, depending on what you mean by "compiler", and how tied to 2005 technology
> you want the answer, with e.g. MSVC it's the linker that does this job.
>[/color]
:-)
Thanks.
--John Ratliff | 
October 11th, 2005, 04:55 AM
| | | Re: inline methods and pointers to members
Greg wrote:[color=blue]
> John Ratliff wrote:
>[color=green]
>>Can the compiler ever inline a method when there is a pointer to the
>>member used?[/color]
>
>
> If the question is: does taking the address of a function (or a method)
> prevent the compiler from inlining that function, the answer is "no".
> Remember that function calls - not functions themselves - are inlined.
> Whether the compiler decides to inline a particular function call has
> no relation to other code that may take the address of the function
> whose call is being inlined.[/color]
This is what I was looking to understand. Thanks.
[color=blue]
>
> If the question is: can a function call through a function (or member)
> pointer be inlined? The answer is "no" for any sensibly-written
> program. To inline a function call through a pointer, the compiler
> would have to be certain that the pointer would always point to the
> same function on every call. And the compiler would have to be able to
> deduce which function the pointer points to. But if the function
> pointer never varies, why would the program bother with it and not just
> call the pointed-to function directly? Use of a function pointer only
> makes sense when the pointer has more than one function it could be
> pointing to.[/color]
This was my problem. How could it inline a call through a pointer to member?
Thanks,
--John Ratliff | 
October 11th, 2005, 10:05 AM
| | | Re: inline methods and pointers to members
John Ratliff wrote:[color=blue]
> Greg wrote:[color=green]
> > John Ratliff wrote:
> >[color=darkred]
> >>Can the compiler ever inline a method when there is a pointer to the
> >>member used?[/color]
> >
> >
> > If the question is: does taking the address of a function (or a method)
> > prevent the compiler from inlining that function, the answer is "no".
> > Remember that function calls - not functions themselves - are inlined.
> > Whether the compiler decides to inline a particular function call has
> > no relation to other code that may take the address of the function
> > whose call is being inlined.[/color]
>
> This is what I was looking to understand. Thanks.
>[color=green]
> >
> > If the question is: can a function call through a function (or member)
> > pointer be inlined? The answer is "no" for any sensibly-written
> > program. To inline a function call through a pointer, the compiler
> > would have to be certain that the pointer would always point to the
> > same function on every call. And the compiler would have to be able to
> > deduce which function the pointer points to. But if the function
> > pointer never varies, why would the program bother with it and not just
> > call the pointed-to function directly? Use of a function pointer only
> > makes sense when the pointer has more than one function it could be
> > pointing to.[/color]
>
> This was my problem. How could it inline a call through a pointer to member?[/color]
I would first determine whether inlining the call through the member
pointer would really have a measurable effect on the program's
performance. In all likelihood, it would not. Performance problems that
can be solved with inlining alone are few and far between in my
experience.
Note also that if the method being called is virtual, the likelihood
that the compiler would inline even a direct call to the method are
also low. Inlining a call to a virtual function is possible only in
limited cases, when the type of object whose method is being invoked is
both known and unvarying.
Using a function object instead of a function pointer often provides
better inlining potential, since the type of the function object is
usually known at compile time. Replacing the member function pointer
with an enum that is used as a selector in a switch statement to
dispatch the function call would be another way to make the call more
suitable for inlining.
Greg | 
October 11th, 2005, 03:45 PM
| | | Re: inline methods and pointers to members
>>>If the question is: can a function call through a function (or member)[color=blue][color=green][color=darkred]
>>>pointer be inlined? The answer is "no" for any sensibly-written
>>>program. To inline a function call through a pointer, the compiler
>>>would have to be certain that the pointer would always point to the
>>>same function on every call. And the compiler would have to be able to
>>>deduce which function the pointer points to. But if the function
>>>pointer never varies, why would the program bother with it and not just
>>>call the pointed-to function directly? Use of a function pointer only
>>>makes sense when the pointer has more than one function it could be
>>>pointing to.[/color]
>>
>>This was my problem. How could it inline a call through a pointer to member?[/color]
>
>
> I would first determine whether inlining the call through the member
> pointer would really have a measurable effect on the program's
> performance. In all likelihood, it would not. Performance problems that
> can be solved with inlining alone are few and far between in my
> experience.[/color]
I doubt any of my inlined methods produce any measurable performance
impact, but when you are searching for one line methods in a file where
every other method is significanly longer, it's easier to find them in
the header file. This is why I declare them inline. I used to put all my
one line methods the class declaration, but after I read the C++ FAQ, I
stopped doing that. A good IDE would probably be just as good of a
solution, but until I find one, jEdit works fine for me.
I rarely enhance my code to increase performance. It's never been
necessary for me in any language but Java. I used to optimize for space
when I wrote C programs for calculators, but that was only because
user-space memory was only 262K.
Then I started wondering how it was going to inline the call through the
pointer to member for all my one line event handlers (callbacks, slots,
insert synonym here). It's probably never going to do that, but since
I'm not really interested in the inlining, only the question of the
possibility, the result doesn't really matter.
[color=blue]
>
> Note also that if the method being called is virtual, the likelihood
> that the compiler would inline even a direct call to the method are
> also low. Inlining a call to a virtual function is possible only in
> limited cases, when the type of object whose method is being invoked is
> both known and unvarying.[/color]
None of my questioned methods are virtual. The way event handling works
in wxWidgets, they can't be.
[color=blue]
>
> Using a function object instead of a function pointer often provides
> better inlining potential, since the type of the function object is
> usually known at compile time. Replacing the member function pointer
> with an enum that is used as a selector in a switch statement to
> dispatch the function call would be another way to make the call more
> suitable for inlining.[/color]
Thanks,
--John Ratliff | 
October 28th, 2005, 01:45 AM
| | | Re: inline methods and pointers to members
> it's easier to find them in the header file.[color=blue]
> ...
> A good IDE would probably be just as good of a solution,
> but until I find one....[/color]
The Zeus for Windows IDE uses ctags for it's source of tags
information and it stores this information in special tag
database files: http://www.zeusedit.com/forum/viewtopic.php?t=185
These database files are used to drive the class browsing,
intellisensing and tag searching features.
In particular the tag searching option makes it very easy
to quickly locate functions, variables and classes.
Jussi Jumppanen
Author: Zeus for Windows IDE
Note: Zeus is shareware (45 day trial). | | Thread Tools | Search this Thread | | | |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 220,989 network members.
|