1

I am writing a C++ library in which I use functions that take as parameters expression templates in Eigen3. Basically the definitions of my functions are similar to

template <typename T>
/* return type */ f(Eigen::MatrixBase<T> param)
{
    // function body
}

The input parameter looks a bit strange at a first sight, but what it really does is that it allows to use expression templates. For example, I can use the function like

f(A * B + C * D * F)

The type of the above argument is some very-very-long-to-spell-here template type (an expression template), and not a plain matrix. Such expression templates allow for speed optimizations etc, so I really need to use them.

Now comes my question:

I have quite a few of these functions, and whenever I instantiate them with different expressions, the compiler generates an instantiation at compile time, hence the compiling time becomes a bit larger. In my case, it's still quite ok, around 30 seconds on an i5 8GB RAM, however I'd very much like to reduce the compilation time. Is there any way or any tricks I can use to somehow reduce the compiling time but still to be able to use expression templates?

vsoftco
  • 55,410
  • 12
  • 139
  • 252
  • Are you using pre-compiled headers with the Eigen headers in it? – Neil Kirk Mar 09 '15 at 02:51
  • @NeilKirk No, I just use standard C++ library headers plus `#include ` – vsoftco Mar 09 '15 at 02:51
  • That would be the first thing to try. Put the most commonly used std headers in there and any "global" library headers used by your project. – Neil Kirk Mar 09 '15 at 02:52
  • @NeilKirk thanks, will try after I figure out how to do it – vsoftco Mar 09 '15 at 02:55
  • Is 30 seconds per whole project or per file? Roughly how many files using this Eigen stuff in your project? – Neil Kirk Mar 09 '15 at 02:55
  • @NeilKirk no, per whole project, but the whole project has around 5000 lines of code (pure lines of code, no comments/white lines). Mostly all of my defined headers include the Eigen stuff. – vsoftco Mar 09 '15 at 02:56
  • I don't know about this Eigen stuff in particular, but lots of templates are slow to compile. You say `expression templates allow for speed optimizations etc, so I really need to use them`. Is this really true? Have you tried without and profiled it? – Neil Kirk Mar 09 '15 at 02:59
  • @NeilKirk yes, I tried, and it makes a difference. Also, if I don't use them, some code like `f(A*B)` doesn't compile without explicitly casting to a matrix, so I prefer to use them – vsoftco Mar 09 '15 at 03:02
  • If you don't mind explicitly listing the required type parameters in a source file, you can use the following trick to split template code between headers and source, like normal code. Less stuff in headers is faster to compile. http://www.cplusplus.com/articles/1C75fSEw/ – Neil Kirk Mar 09 '15 at 03:06
  • I knew about this, the problem is that the number of expression templates is exponential, the user decided which function to call, so e.g. `f(A+B)` is different then `f(A*B)` etc... And the issue is to make the library compile faster for the user. Will try the precompiled headers though, thanks for your tips! – vsoftco Mar 09 '15 at 03:09
  • What about hiding the expression templates in a pimpl (http://herbsutter.com/gotw/_100/) wrapper. That would then generally prevent recompilation - unless of course you are changing the expressions themselves. – Peter R Mar 09 '15 at 05:59

0 Answers0