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