performance bottleneck in yy_get_next_buffer function

Gautam Kapoor <[email protected]> Wed, 9 Jan 2013 22:50:15 +0530
Newsgroups gmane.comp.lex.flex.general
Message-ID <664A5BEDF5FDDC4BA499CFCA7FCFAC990FDA57BE01@MAILIN2.global.cadence.com>
Hi Compiler Experts,
I am using flex to generate a scanner. It creates a yy_get_next_buffer() function in lex.yy.cc among other things.

I want to discuss a particular performance issue and how I tried to fix it. I am wondering why it is not part of the default scanner generated by flex because I think the developers must have seen this too.


When I run a particular test-case, I see a lot of time being taken by this function (yy_get_next_buffer). On running tools like "quantify" to measure performance, I found that these particular lines in yy_get_next_buffer() are taking a lot of time.

        /* First move last chars to start of buffer. */
        number_to_move = (int) ((yy_c_buf_p) - (yytext_ptr)) - 1;

        for ( i = 0; i < number_to_move; ++i )
              *(dest++) = *(source++);


When I change this to the following, I get a huge improvement in performance. 

        /* First move last chars to start of buffer. */
        number_to_move = (int) ((yy_c_buf_p) - (yytext_ptr)) - 1;

        memcpy(dest,source,number_to_move);


-regards
Gautam