Boost logo

Boost :

Subject: [boost] [fusion] improving compile times
From: Eric Niebler (eric_at_[hidden])
Date: 2009-06-01 21:26:19


I'm attaching a simple patch to vector_n_chooser.hpp that replaces some
template metaprogramming with preprocessor metaprogramming in the
interest of improving compile times. I found this hotspot through
profiling, and there are likely to be many more such patches in the near
future. I could open a trac ticket and attach all the patches there, or
I could just commit them to trunk as I go. Thoughts?

P.S. It would be great to get some other heavy users of template
metaprogramming interested in making other such changes to bring down
the compile times of the core TMP libraries like MPL, Fusion, Proto,
etc., etc.

-- 
Eric Niebler
BoostPro Computing
http://www.boostpro.com

Index: vector_n_chooser.hpp
===================================================================
--- vector_n_chooser.hpp (revision 53535)
+++ vector_n_chooser.hpp (working copy)
@@ -25,11 +25,13 @@
 #include <boost/fusion/container/vector/vector50.hpp>
 #endif
 
-#include <boost/mpl/distance.hpp>
-#include <boost/mpl/find.hpp>
-#include <boost/mpl/begin_end.hpp>
 #include <boost/preprocessor/cat.hpp>
+#include <boost/preprocessor/arithmetic/dec.hpp>
+#include <boost/preprocessor/arithmetic/sub.hpp>
+#include <boost/preprocessor/facilities/intercept.hpp>
 #include <boost/preprocessor/repetition/enum_params.hpp>
+#include <boost/preprocessor/repetition/enum_trailing_params.hpp>
+#include <boost/preprocessor/repetition/enum_params_with_a_default.hpp>
 
 namespace boost { namespace fusion
 {
@@ -38,40 +40,23 @@
 
 namespace boost { namespace fusion { namespace detail
 {
- template <int N>
- struct get_vector_n;
+ template <BOOST_PP_ENUM_PARAMS(FUSION_MAX_VECTOR_SIZE, typename T)>
+ struct vector_n_chooser
+ {
+ typedef BOOST_PP_CAT(vector, FUSION_MAX_VECTOR_SIZE)<BOOST_PP_ENUM_PARAMS(FUSION_MAX_VECTOR_SIZE, T)> type;
+ };
 
     template <>
- struct get_vector_n<0>
+ struct vector_n_chooser<BOOST_PP_ENUM_PARAMS(FUSION_MAX_VECTOR_SIZE, void_ BOOST_PP_INTERCEPT)>
     {
- template <BOOST_PP_ENUM_PARAMS(FUSION_MAX_VECTOR_SIZE, typename T)>
- struct call
- {
- typedef vector0 type;
- };
+ typedef vector0 type;
     };
 
 #define BOOST_PP_FILENAME_1 \
     <boost/fusion/container/vector/detail/vector_n_chooser.hpp>
-#define BOOST_PP_ITERATION_LIMITS (1, FUSION_MAX_VECTOR_SIZE)
+#define BOOST_PP_ITERATION_LIMITS (1, BOOST_PP_DEC(FUSION_MAX_VECTOR_SIZE))
 #include BOOST_PP_ITERATE()
 
- template <BOOST_PP_ENUM_PARAMS(FUSION_MAX_VECTOR_SIZE, typename T)>
- struct vector_n_chooser
- {
- typedef
- mpl::BOOST_PP_CAT(vector, FUSION_MAX_VECTOR_SIZE)
- <BOOST_PP_ENUM_PARAMS(FUSION_MAX_VECTOR_SIZE, T)>
- input;
-
- typedef typename mpl::begin<input>::type begin;
- typedef typename mpl::find<input, void_>::type end;
- typedef typename mpl::distance<begin, end>::type size;
-
- typedef typename get_vector_n<size::value>::template
- call<BOOST_PP_ENUM_PARAMS(FUSION_MAX_VECTOR_SIZE, T)>::type
- type;
- };
 }}}
 
 #endif
@@ -85,14 +70,12 @@
 
 #define N BOOST_PP_ITERATION()
 
- template <>
- struct get_vector_n<N>
+ template <BOOST_PP_ENUM_PARAMS(N, typename T)>
+ struct vector_n_chooser<
+ BOOST_PP_ENUM_PARAMS(N, T)
+ BOOST_PP_ENUM_TRAILING_PARAMS(BOOST_PP_SUB(FUSION_MAX_VECTOR_SIZE, N), void_ BOOST_PP_INTERCEPT)>
     {
- template <BOOST_PP_ENUM_PARAMS(FUSION_MAX_VECTOR_SIZE, typename T)>
- struct call
- {
- typedef BOOST_PP_CAT(vector, N)<BOOST_PP_ENUM_PARAMS(N, T)> type;
- };
+ typedef BOOST_PP_CAT(vector, N)<BOOST_PP_ENUM_PARAMS(N, T)> type;
     };
 
 #undef N


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk