inline methods and pointers to members

J

John Ratliff

Can the compiler ever inline a method when there is a pointer to the
member used?

Thanks,

--John Ratliff
 
A

Alf P. Steinbach

* John Ratliff:
Can the compiler ever inline a method when there is a pointer to the
member used?

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.
 
G

Greg

John said:
Can the compiler ever inline a method when there is a pointer to the
member used?

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
 
G

Greg

John said:
Can the compiler ever inline a method when there is a pointer to the
member used?

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
 
J

John Ratliff

Alf said:
* John Ratliff:



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.

:)

Thanks.

--John Ratliff
 
J

John Ratliff

Greg said:
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.

This is what I was looking to understand. Thanks.
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.

This was my problem. How could it inline a call through a pointer to member?

Thanks,

--John Ratliff
 
G

Greg

John said:
This is what I was looking to understand. Thanks.


This was my problem. How could it inline a call through a pointer to member?

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
 
J

John Ratliff

If the question is: can a function call through a function (or member)
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.

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.
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.

None of my questioned methods are virtual. The way event handling works
in wxWidgets, they can't be.
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.

Thanks,

--John Ratliff
 
J

jussij

it's easier to find them in the header file.
...
A good IDE would probably be just as good of a solution,
but until I find one....

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).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top