|
From: Olivier on 26 Sep 2006 10:47 Hi All, I've been playing around with expression templates, implementing my own solution as an exercice - on mathematical expressions - and everything works fine, but I can't seem to find the solution to one slight problem I have : expression templates are interesting if they can simplify writing expressions while still providing fast code. for example the following code should shrink down to the same assembly code : float get_value_template( float value ) { wrapper2<wrapper1<float> > w( wrapper1<float>(value) ); return w(); } float get_value_normal( float value ) { return value; } With wrapper1 and wrapper2 being simple wrapping classes defined as follow : template< typename wrapped_type > class wrapper1 { public: typedef wrapped_type const & value_type; wrapper1( wrapped_type const &instance ) : _M_instance(instance) {} value_type operator()( void ) const { return _M_instance; } private: wrapped_type const &_M_instance; }; template< typename wrapped_type > class wrapper2 { public: typedef typename wrapped_type::value_type value_type; wrapper2( wrapped_type const &instance ) : _M_instance(instance) {} value_type operator()( void ) const { return _M_instance(); } private: wrapped_type const &_M_instance; }; or at least I would have expected it... But it doesn't. The resulting assembly code differs just a bit : ; this is function get_value_normal pushl %ebp movl %esp, %ebp flds 8(%ebp) popl %ebp ret ; this is function get_value_template pushl %ebp movl %esp, %ebp subl $4, %esp flds 8(%ebp) leave ret After tuning the code a bit, and experiencing different things, it seems like the compiler - g++ - can't optimize the double operator() call away, even though both nested objects are const references, and that the get_value_template function is very simple. Is there something i'm missing ? Thanks for your help, Olivier. [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: David Abrahams on 26 Sep 2006 14:15 "Olivier" <olivier.grant(a)gmail.com> writes: > After tuning the code a bit, and experiencing different things, it > seems like the compiler - g++ - can't optimize the double operator() > call away, even though both nested objects are const references, and > that the get_value_template function is very simple. > > Is there something i'm missing ? This question is really very specific to g++, and even the particular version of g++ you're using. I suggest you ask the g++ developers (or even post this as a bug). I know that reducing abstraction penalty has been an ongoing project for them. HTH, -- Dave Abrahams Boost Consulting www.boost-consulting.com [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
|
Pages: 1 Prev: Strategies for indexing very large arrays Next: Why is VC++ STL so slow? |