From dd1a842285aa621567925eef8263ff336ba077be Mon Sep 17 00:00:00 2001 From: Arthur O'Dwyer Date: Sat, 23 Jun 2018 18:19:22 -0700 Subject: [PATCH] Refactor to improve the compile time of "langen.cpp". $ time c++ -std=c++11 -march=native -DMAC -I/usr/local/include -DCAP_PNG=0 -O0 -w langen.cpp -o langen real 0m3.174s user 0m2.985s sys 0m0.179s --- langen.cpp | 155 ++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 111 insertions(+), 44 deletions(-) diff --git a/langen.cpp b/langen.cpp index 4270fb35..9f82f779 100644 --- a/langen.cpp +++ b/langen.cpp @@ -24,20 +24,29 @@ const char *escape(std::string s, const std::string& dft); template struct dictionary { std::map m; - void add(const std::string& s, const T& val) { - if(m.count(s)) add(s + " [repeat]", val); - else m[s] = val; + void add(const std::string& s, T val) { + if(m.count(s)) add(s + " [repeat]", std::move(val)); + else m[s] = std::move(val); } T& operator [] (const std::string& s) { return m[s]; } int count(const std::string& s) { return m.count(s); } - void clear() { m.clear(); } }; dictionary d[NUMLAN]; +struct noun2 { + int genus; + const char *nom; + const char *nomp; + const char *acc; + const char *abl; + }; + struct noun { int genus; std::string nom, nomp, acc, abl; + noun() = default; + noun(const noun2& n) : genus(n.genus), nom(n.nom), nomp(n.nomp), acc(n.acc), abl(n.abl) {} }; dictionary nouns[NUMLAN]; @@ -117,60 +126,118 @@ const char *escape(std::string s, const std::string& dft) { std::set nothe; std::set plural; -void langPL() { - #define S(a,b) d[1].add(a,b); - #define N(a,b,c,d,e,f) \ - {noun n; n.genus = b; n.nom = c; n.nomp = d; n.acc = e; n.abl = f; nouns[1].add(a,n);} - #include "language-pl.cpp" - #undef N - #undef S - } + void langPL() { + static std::pair ds[] = { + #define S(a,b) { a, b }, + #define N(a,b,c,d,e,f) + #include "language-pl.cpp" + #undef N + #undef S + }; + static std::pair ns[] = { + #define S(a,b) + #define N(a,b,c,d,e,f) { a, noun2{ b, c, d, e, f } }, + #include "language-pl.cpp" + #undef N + #undef S + }; + for(auto&& elt : ds) d[1].add(elt.first, elt.second); + for(auto&& elt : ns) nouns[1].add(elt.first, elt.second); +} void langTR() { -#define S(a,b) d[2].add(a,b); -#define N5(a,b,c,d,e) \ - {noun n; n.genus = b; n.nom = c; n.nomp = d; n.acc = e; n.abl = e; nouns[2].add(a,n);} -#define N(a,b,c,d,e,f) \ - {noun n; n.genus = b; n.nom = c; n.nomp = d; n.acc = e; n.abl = f; nouns[2].add(a,n);} -#include "language-tr.cpp" -#undef N -#undef S + static std::pair ds[] = { + #define S(a,b) { a, b }, + #define N(a,b,c,d,e,f) + #include "language-tr.cpp" + #undef N + #undef S + }; + static std::pair ns[] = { + #define S(a,b) + #define N(a,b,c,d,e,f) { a, noun2{ b, c, d, e, f } }, + #include "language-tr.cpp" + #undef N + #undef S + }; + for(auto&& elt : ds) d[2].add(elt.first, elt.second); + for(auto&& elt : ns) nouns[2].add(elt.first, elt.second); } void langCZ() { -#define S(a,b) d[3].add(a,b); -#define N(a,b,c,d,e,f) \ - {noun n; n.genus = b; n.nom = c; n.nomp = d; n.acc = e; n.abl = f; nouns[3].add(a,n);} -#include "language-cz.cpp" -#undef N -#undef S + static std::pair ds[] = { + #define S(a,b) { a, b }, + #define N(a,b,c,d,e,f) + #include "language-cz.cpp" + #undef N + #undef S + }; + static std::pair ns[] = { + #define S(a,b) + #define N(a,b,c,d,e,f) { a, noun2{ b, c, d, e, f } }, + #include "language-cz.cpp" + #undef N + #undef S + }; + for(auto&& elt : ds) d[3].add(elt.first, elt.second); + for(auto&& elt : ns) nouns[3].add(elt.first, elt.second); } void langRU() { -#define S(a,b) d[4].add(a,b); -#define N(a,b,c,d,e,f) \ - {noun n; n.genus = b; n.nom = c; n.nomp = d; n.acc = e; n.abl = f; nouns[4].add(a,n);} -#include "language-ru.cpp" -#undef N -#undef S + static std::pair ds[] = { + #define S(a,b) { a, b }, + #define N(a,b,c,d,e,f) + #include "language-ru.cpp" + #undef N + #undef S + }; + static std::pair ns[] = { + #define S(a,b) + #define N(a,b,c,d,e,f) { a, noun2{ b, c, d, e, f } }, + #include "language-ru.cpp" + #undef N + #undef S + }; + for(auto&& elt : ds) d[4].add(elt.first, elt.second); + for(auto&& elt : ns) nouns[4].add(elt.first, elt.second); } void langDE() { -#define S(a,b) d[5].add(a,b); -#define N(a,b,c,d,e) \ - {noun n; n.genus = b; n.nom = c; n.nomp = d; n.acc = e; n.abl = e; nouns[5].add(a,n);} -#include "language-de.cpp" -#undef N -#undef S + static std::pair ds[] = { + #define S(a,b) { a, b }, + #define N(a,b,c,d,e) + #include "language-de.cpp" + #undef N + #undef S + }; + static std::pair ns[] = { + #define S(a,b) + #define N(a,b,c,d,e) { a, noun2{ b, c, d, e, e } }, + #include "language-de.cpp" + #undef N + #undef S + }; + for(auto&& elt : ds) d[5].add(elt.first, elt.second); + for(auto&& elt : ns) nouns[5].add(elt.first, elt.second); } void langPT() { -#define S(a,b) d[6].add(a,b); -#define N(a,b,c,d,e) \ - {noun n; n.genus = b; n.nom = c; n.nomp = d; n.abl = e; nouns[6].add(a,n);} -#include "language-ptbr.cpp" -#undef N -#undef S + static std::pair ds[] = { + #define S(a,b) { a, b }, + #define N(a,b,c,d,e) + #include "language-ptbr.cpp" + #undef N + #undef S + }; + static std::pair ns[] = { + #define S(a,b) + #define N(a,b,c,d,e) { a, noun2{ b, c, d, "", e } }, + #include "language-ptbr.cpp" + #undef N + #undef S + }; + for(auto&& elt : ds) d[6].add(elt.first, elt.second); + for(auto&& elt : ns) nouns[6].add(elt.first, elt.second); } int completeness[NUMLAN];