Last active
August 29, 2015 14:21
-
-
Save elbeno/9802ed10f05e166499d6 to your computer and use it in GitHub Desktop.
Quick compilation of large tuples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <tuple> | |
using namespace std; | |
// When tuples get large, compile times suffer because the compiler is hashing | |
// the name of a really long type. Lambdas offer a way to truncate that type | |
// name, leading to faster compile times and the ability to instantiate larger | |
// tuples (the template depth quickly exceeds the max). | |
// Compare compile times: | |
// $ time g++ -std=c++1y -DUSE_LAMBDAS_AS_TUPLES -g -O2 -c main.cpp -o main-lambda.o | |
// | |
// real 0m0.323s | |
// user 0m0.296s | |
// sys 0m0.020s | |
// $ time g++ -std=c++1y -ftemplate-depth=1024 -g -O2 -c main.cpp -o main-template.o | |
// | |
// real 0m0.833s | |
// user 0m0.780s | |
// sys 0m0.048s | |
#ifdef USE_LAMBDAS_AS_TUPLES | |
int main(void) | |
{ | |
auto list = [] (auto ...xs) { | |
return [=] (auto access) { return access(xs...); }; | |
}; | |
auto length = [] (auto xs) { | |
return xs([] (auto ...z) { return sizeof...(z); }); | |
}; | |
// A tuple of length 100! | |
auto len = length(list( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, | |
11, 12, 13, 14, 15, 16, 17, 18, 19, 20, | |
21, 22, 23, 24, 25, 26, 27, 28, 29, 30, | |
31, 32, 33, 34, 35, 36, 37, 38, 39, 40, | |
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, | |
51, 52, 53, 54, 55, 56, 57, 58, 59, 60, | |
61, 62, 63, 64, 65, 66, 67, 68, 69, 70, | |
71, 72, 73, 74, 75, 76, 77, 78, 79, 80, | |
81, 82, 83, 84, 85, 86, 87, 88, 89, 90, | |
91, 92, 93, 94, 95, 96, 97, 98, 99, 100)); | |
cout << len << endl; | |
} | |
#else | |
int main(void) | |
{ | |
// The compiler can't deal with a tuple much bigger than this (50) | |
auto len = tuple_size<decltype( | |
make_tuple( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, | |
11, 12, 13, 14, 15, 16, 17, 18, 19, 20, | |
21, 22, 23, 24, 25, 26, 27, 28, 29, 30, | |
31, 33, 33, 34, 35, 36, 37, 38, 39, 40, | |
41, 44, 44, 44, 44, 46, 47, 48, 49, 50))>::value; | |
cout << len << endl; | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment