in boost::spirit::lex, it takes longest time to do first parsing, following parsing will be much shorter

前端 未结 1 1600
囚心锁ツ
囚心锁ツ 2021-01-21 17:32

I feed a series of text into my sip parser.the first one takes the longest time, no matter which is the first one.I wonder if there is any initialization work when spirit::lex d

1条回答
  •  再見小時候
    2021-01-21 18:00

    Clearly, it does :)

    Lex will likely generate a DFA (one for each Lexer state, maybe). This is most likely the thing that takes the most time. Use a profiler to be certain :/

    Now, you can

    • make sure the tables are initialized before first use, or
    • use the The Static Lexer Model to prevent the startup cost

    This means you'll write an 'extra' main to generate the DFA as C++ code:

    #include 
    #include 
    
    #include 
    
    #include "sip_token.hpp"
    
    using namespace boost::spirit;
    
    int main(int argc, char* argv[])
    {
        // create the lexer object instance needed to invoke the generator
        sip_token > my_lexer; // the token definition
    
        std::ofstream out(argc < 2 ? "sip_token_static.hpp" : argv[1]);
    
        // invoke the generator, passing the token definition, the output stream 
        // and the name suffix of the tables and functions to be generated
        //
        // The suffix "sip" used below results in a type lexertl::static_::lexer_sip
        // to be generated, which needs to be passed as a template parameter to the 
        // lexertl::static_lexer template (see word_count_static.cpp).
        return lex::lexertl::generate_static_dfa(my_lexer, out, "sip") ? 0 : -1;
    }
    

    An example of the code generated is here (in the word-count example from the tutorial): http://www.boost.org/doc/libs/1_54_0/libs/spirit/example/lex/static_lexer/word_count_static.hpp

    0 讨论(0)
提交回复
热议问题