From 79fbc8bf495770e4a8b7c66c46acf07f4e47e568 Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Mon, 30 Mar 2020 18:33:01 +0200 Subject: Speed up compile --- grammer.h | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) (limited to 'grammer.h') diff --git a/grammer.h b/grammer.h index 88cb7b7..df7ff8d 100644 --- a/grammer.h +++ b/grammer.h @@ -5,6 +5,7 @@ #include #include +#include #include #include @@ -41,8 +42,9 @@ private: BNF &bnf; // not const for access via operator[] const std::string m_top; - std::unordered_map> ReverseBNF; // possible parent types of a given type; unused now: remove? - std::unordered_map> reversedFirst; // possible parent types of first childs of a given type + //std::unordered_map> ReverseBNF; // possible parent types of a given type; unused now: remove? + //std::unordered_map> reversedFirst; // possible parent types of first childs of a given type + std::unordered_map, PairHash>> reversedPosFirst; // possible parent types of first childs of a given type std::deque symbol_variants; decltype(symbol_variants)::iterator symbol_variants_it; @@ -63,12 +65,26 @@ private: void IncNodePosition(NodePosition& pos); // top-down algorithm - std::unordered_map m_min; // cache - size_t minimumSymbolsNeeded(std::string symbol); - size_t minimumSymbolsNeeded(std::vector symbol_list); + std::unordered_map m_min; // cache for MinimumSymbolsNeeded + size_t minimumSymbolsNeeded(const std::string& symbol); + size_t minimumSymbolsNeeded(const std::vector& symbol_list); bool match(std::string symbol, size_t begin, size_t end); bool match(std::vector symbol_list, size_t begin, size_t end); + // start / end cache + struct TupleHash { + size_t operator()(const std::tuple& t) const noexcept + { + size_t h0 {std::hash{}(std::get<0>(t))}; + size_t h1 {std::hash{}(std::get<1>(t))}; + size_t h2 {std::hash{}(std::get<2>(t))}; + return h0 ^ (h1 << 1) ^ (h2 << 2); + } + }; + std::unordered_set, TupleHash> m_start_cache; + bool canStartWith(const std::string& non_terminal, size_t variant, const std::string& terminal) const; + void fillStartCache(); + void constructTree(); std::vector m_symbol_list; index_t m_symbol_list_pos{}; -- cgit v1.2.3