This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Inlining functions vs. inlining member functions as templates


Hi.

I'm working on a lib that uses policy templates quite extensively, in the vein 
of the STL's template <class Compare> std::list::sort(Compare comp).

Compare can be a function or a struct with operator(). While the functionality 
is equivalent the compiler inlines the struct but not the function. Changing 
to the struct implementation decreased runtime by more than 40% in an image 
filtering function.

Below you can find a tiny example. The commented code gives the alternate 
implementation which is perfectly inlined. See attachments for assembler 
code.

Can somebody explain why lessFunc is not inlined?

/usr/i686-pc-linux-gnu/gcc-bin/4.0.1-beta20050507/gcc --version
gcc (GCC) 4.0.1-beta20050507 (Gentoo 4.0.1_beta20050507)
flags: -mtune=pentium4 -O3 -fomit-frame-pointer -S

gcc3.3.3 (SuSE9.1), gcc3.3.5(gentoo), gcc.3.4.3(Gentoo 3.4.3.20050110-r2) 
perform much worse in both cases. Good point for gcc4.
icc8.0 produces roughly the same result as gcc4.0.1. Also very good.
I think the gcc4.x of gentoo is very close to or the same as the official 
snapshot.

Thanks for hints,
Peter


--------------------------------------------------------------------------------------------

// struct less {
//   inline bool operator()(const int a, const int b) {
//     return a<b;
//   }
// };

inline bool lessFunc(const int a, const int b) {
  return a<b;
}

template <class Comp>
bool foo(const int a, const int b, Comp comp) {
  return comp(a,b);
}

int main(int argc, char** argv) {
  return foo(argc,1,lessFunc);
//   return foo(argc,3,less());
}


// struct less {
//   inline bool operator()(const int a, const int b) {
//     return a<b;
//   }
// };

inline bool lessFunc(const int a, const int b) {
  return a<b;
}

template <class Comp>
bool foo(const int a, const int b, Comp comp) {
  return comp(a,b);
}

int main(int argc, char** argv) {
  return foo(argc,1,lessFunc);
//   return foo(argc,3,less());
}

Attachment: testTemplateInlineFunc.s
Description: Text document

Attachment: testTemplateInlineStruct.s
Description: Text document


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]