[OT/2] Curiosity about inlining member functions

  • Thread starter Francesco S. Carta
  • Start date
F

Francesco S. Carta

Hi there,
just wondering... what are the chances of a compiler to inline a member
function in a case like this:

struct A {
void method() {
helper();
}
void helper() {
//...
}
};

The definition of helper() is not available where it's being used, would
that mean that the compiler will not be able to inline it?

Is the compiler allowed to "rewrite" the above as something like this:

struct A {
void helper() {
//...
}
void method() {
helper();
}
};

In order to take advantage of inlineing?

And finally, does it change anything regarding this "inlineing issue" if
A is a template?

As I sense it, since both members /can/ be inlined at the calling place
(in main(), for instance), both of them /will/ be eventually inlined,
but what if method() by itself cannot be inlined?

The picture I'm looking at, right now, is something like this:

class A {
public:
void method() {
// assume NOT inlineable
// calls helper() lots of times
}
private:
void helper() {
// assume trivial, easily inlineable
}
};

In a case like the above I'm expected to keep the order of access
specifiers for reading ease, but eventually I could "ensure" the
inlineing by implementing it like this:

class A {
public:
void method() {
private_method();
}
private:
void helper() {
// assume trivial, easily inlineable
}
void private_method() {
// calls helper() lots of times
}
};

Of course, all of this can be figured out on a per-implementation,
per-optimization basis by profiling the various versions, but I wanted
to post it in order to get some input from anybody who could have dug
this issue before.

Thank you for reading this half-off-topic curiosity questions, I had a
look to the standard ([class.mfct] in particular) but I didn't find
anything about the "special" cases depicted here, so to say.
 
T

tni

just wondering... what are the chances of a compiler to inline a member
function in a case like this:

struct A {
void method() {
helper();
}
void helper() {
//...
}
};

The definition of helper() is not available where it's being used,

Sure it is. It's in the same translation unit.
would that mean that the compiler will not be able to inline it?

As long as it is in the same translation unit, there is no issue at all.
 
F

Francesco S. Carta

Sure it is. It's in the same translation unit.


As long as it is in the same translation unit, there is no issue at all.

Don't know why, but I thought that the order of definitions actually
mattered...

For another example, I thought that something like this would actually
prevent the compiler from inlineing the function:

inline void test();

int main() {
test();
}

inline void test() {
//...
}

Thank you for your reply tni :)
 
T

tni

Don't know why, but I thought that the order of definitions actually
mattered...

It doesn't (at least for the current mainstream compilers). Building a
single-pass C++ compiler isn't really possible in the first place.
 
B

Bo Persson

Francesco said:
Don't know why, but I thought that the order of definitions actually
mattered...

For another example, I thought that something like this would
actually prevent the compiler from inlineing the function:

inline void test();

int main() {
test();
}

inline void test() {
//...
}

It makes it slightly harder, but doesn't prevent anything. Consider
that a compiler doing whole program optimization can inline the
function even if it is defined in another translation unit, and
compiler after the main() function.

Also, have you noticed that inside a class you can call a function
declared further down in the class, while outside a class functions
must be declared before being called? :)



Bo Persson
 
F

Francesco S. Carta

It makes it slightly harder, but doesn't prevent anything. Consider
that a compiler doing whole program optimization can inline the
function even if it is defined in another translation unit, and
compiler after the main() function.

Eh, missed that too...
Also, have you noticed that inside a class you can call a function
declared further down in the class, while outside a class functions
must be declared before being called? :)

Yes, of course I knew that everything declared in the class must be
visible even before its actual definition (to put it simply) - I just
missed to make that further step: the compiler must do a two-pass -->
the compiler will be able to inline it ;-)

Thank you for your notes Bo.
 

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

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top