/[pcre]/code/trunk/pcre_dfa_exec.c
ViewVC logotype

Diff of /code/trunk/pcre_dfa_exec.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 428 by ph10, Mon Aug 31 17:10:26 2009 UTC revision 459 by ph10, Sun Oct 4 09:21:39 2009 UTC
# Line 45  FSM). This is NOT Perl- compatible, but Line 45  FSM). This is NOT Perl- compatible, but
45  applications. */  applications. */
46    
47    
48    /* NOTE ABOUT PERFORMANCE: A user of this function sent some code that improved
49    the performance of his patterns greatly. I could not use it as it stood, as it
50    was not thread safe, and made assumptions about pattern sizes. Also, it caused
51    test 7 to loop, and test 9 to crash with a segfault.
52    
53    The issue is the check for duplicate states, which is done by a simple linear
54    search up the state list. (Grep for "duplicate" below to find the code.) For
55    many patterns, there will never be many states active at one time, so a simple
56    linear search is fine. In patterns that have many active states, it might be a
57    bottleneck. The suggested code used an indexing scheme to remember which states
58    had previously been used for each character, and avoided the linear search when
59    it knew there was no chance of a duplicate. This was implemented when adding
60    states to the state lists.
61    
62    I wrote some thread-safe, not-limited code to try something similar at the time
63    of checking for duplicates (instead of when adding states), using index vectors
64    on the stack. It did give a 13% improvement with one specially constructed
65    pattern for certain subject strings, but on other strings and on many of the
66    simpler patterns in the test suite it did worse. The major problem, I think,
67    was the extra time to initialize the index. This had to be done for each call
68    of internal_dfa_exec(). (The supplied patch used a static vector, initialized
69    only once - I suspect this was the cause of the problems with the tests.)
70    
71    Overall, I concluded that the gains in some cases did not outweigh the losses
72    in others, so I abandoned this code. */
73    
74    
75    
76  #ifdef HAVE_CONFIG_H  #ifdef HAVE_CONFIG_H
77  #include "config.h"  #include "config.h"
78  #endif  #endif
# Line 389  if (*first_op == OP_REVERSE) Line 417  if (*first_op == OP_REVERSE)
417        current_subject - start_subject : max_back;        current_subject - start_subject : max_back;
418      current_subject -= gone_back;      current_subject -= gone_back;
419      }      }
420    
421      /* Save the earliest consulted character */
422    
423      if (current_subject < md->start_used_ptr)
424        md->start_used_ptr = current_subject;
425    
426    /* Now we can process the individual branches. */    /* Now we can process the individual branches. */
427    
# Line 545  for (;;) Line 578  for (;;)
578          }          }
579        }        }
580    
581      /* Check for a duplicate state with the same count, and skip if found. */      /* Check for a duplicate state with the same count, and skip if found.
582        See the note at the head of this module about the possibility of improving
583        performance here. */
584    
585      for (j = 0; j < i; j++)      for (j = 0; j < i; j++)
586        {        {
# Line 612  for (;;) Line 647  for (;;)
647  /* ========================================================================== */  /* ========================================================================== */
648        /* Reached a closing bracket. If not at the end of the pattern, carry        /* Reached a closing bracket. If not at the end of the pattern, carry
649        on with the next opcode. Otherwise, unless we have an empty string and        on with the next opcode. Otherwise, unless we have an empty string and
650        PCRE_NOTEMPTY is set, save the match data, shifting up all previous        PCRE_NOTEMPTY is set, or PCRE_NOTEMPTY_ATSTART is set and we are at the
651          start of the subject, save the match data, shifting up all previous
652        matches so we always have the longest first. */        matches so we always have the longest first. */
653    
654        case OP_KET:        case OP_KET:
# Line 629  for (;;) Line 665  for (;;)
665        else        else
666          {          {
667          reached_end++;    /* Count branches that reach the end */          reached_end++;    /* Count branches that reach the end */
668          if (ptr > current_subject || (md->moptions & PCRE_NOTEMPTY) == 0)          if (ptr > current_subject ||
669                ((md->moptions & PCRE_NOTEMPTY) == 0 &&
670                  ((md->moptions & PCRE_NOTEMPTY_ATSTART) == 0 ||
671                    current_subject > start_subject + md->start_offset)))
672            {            {
673            if (match_count < 0) match_count = (offsetcount >= 2)? 1 : 0;            if (match_count < 0) match_count = (offsetcount >= 2)? 1 : 0;
674              else if (match_count > 0 && ++match_count * 2 >= offsetcount)              else if (match_count > 0 && ++match_count * 2 >= offsetcount)
# Line 800  for (;;) Line 839  for (;;)
839          if (ptr > start_subject)          if (ptr > start_subject)
840            {            {
841            const uschar *temp = ptr - 1;            const uschar *temp = ptr - 1;
842              if (temp < md->start_used_ptr) md->start_used_ptr = temp;
843  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
844            if (utf8) BACKCHAR(temp);            if (utf8) BACKCHAR(temp);
845  #endif  #endif
# Line 2247  for (;;) Line 2287  for (;;)
2287    
2288          /* Back reference conditions are not supported */          /* Back reference conditions are not supported */
2289    
2290          if (condcode == OP_CREF) return PCRE_ERROR_DFA_UCOND;          if (condcode == OP_CREF || condcode == OP_NCREF)
2291              return PCRE_ERROR_DFA_UCOND;
2292    
2293          /* The DEFINE condition is always false */          /* The DEFINE condition is always false */
2294    
# Line 2258  for (;;) Line 2299  for (;;)
2299          which means "test if in any recursion". We can't test for specifically          which means "test if in any recursion". We can't test for specifically
2300          recursed groups. */          recursed groups. */
2301    
2302          else if (condcode == OP_RREF)          else if (condcode == OP_RREF || condcode == OP_NRREF)
2303            {            {
2304            int value = GET2(code, LINK_SIZE+2);            int value = GET2(code, LINK_SIZE+2);
2305            if (value != RREF_ANY) return PCRE_ERROR_DFA_UCOND;            if (value != RREF_ANY) return PCRE_ERROR_DFA_UCOND;
# Line 2503  for (;;) Line 2544  for (;;)
2544        {        {
2545        if (offsetcount >= 2)        if (offsetcount >= 2)
2546          {          {
2547          offsets[0] = current_subject - start_subject;          offsets[0] = md->start_used_ptr - start_subject;
2548          offsets[1] = end_subject - start_subject;          offsets[1] = end_subject - start_subject;
2549          }          }
2550        match_count = PCRE_ERROR_PARTIAL;        match_count = PCRE_ERROR_PARTIAL;
# Line 2611  if (extra_data != NULL) Line 2652  if (extra_data != NULL)
2652    if ((flags & PCRE_EXTRA_TABLES) != 0)    if ((flags & PCRE_EXTRA_TABLES) != 0)
2653      md->tables = extra_data->tables;      md->tables = extra_data->tables;
2654    }    }
2655    
2656  /* Check that the first field in the block is the magic number. If it is not,  /* Check that the first field in the block is the magic number. If it is not,
2657  test for a regex that was compiled on a host of opposite endianness. If this is  test for a regex that was compiled on a host of opposite endianness. If this is
2658  the case, flipped values are put in internal_re and internal_study if there was  the case, flipped values are put in internal_re and internal_study if there was
# Line 2645  md->start_code = (const uschar *)argumen Line 2686  md->start_code = (const uschar *)argumen
2686      re->name_table_offset + re->name_count * re->name_entry_size;      re->name_table_offset + re->name_count * re->name_entry_size;
2687  md->start_subject = (const unsigned char *)subject;  md->start_subject = (const unsigned char *)subject;
2688  md->end_subject = end_subject;  md->end_subject = end_subject;
2689    md->start_offset = start_offset;
2690  md->moptions = options;  md->moptions = options;
2691  md->poptions = re->options;  md->poptions = re->options;
2692    
# Line 2749  if (!anchored) Line 2791  if (!anchored)
2791      }      }
2792    else    else
2793      {      {
2794      if (startline && study != NULL &&      if (!startline && study != NULL &&
2795           (study->options & PCRE_STUDY_MAPPED) != 0)           (study->flags & PCRE_STUDY_MAPPED) != 0)
2796        start_bits = study->start_bits;        start_bits = study->start_bits;
2797      }      }
2798    }    }
# Line 2801  for (;;) Line 2843  for (;;)
2843        }        }
2844    
2845      /* There are some optimizations that avoid running the match if a known      /* There are some optimizations that avoid running the match if a known
2846      starting point is not found, or if a known later character is not present.      starting point is not found. However, there is an option that disables
2847      However, there is an option that disables these, for testing and for      these, for testing and for ensuring that all callouts do actually occur. */
     ensuring that all callouts do actually occur. */  
2848    
2849      if ((options & PCRE_NO_START_OPTIMIZE) == 0)      if ((options & PCRE_NO_START_OPTIMIZE) == 0)
2850        {        {
   
2851        /* Advance to a known first byte. */        /* Advance to a known first byte. */
2852    
2853        if (first_byte >= 0)        if (first_byte >= 0)
# Line 2873  for (;;) Line 2913  for (;;)
2913      /* Restore fudged end_subject */      /* Restore fudged end_subject */
2914    
2915      end_subject = save_end_subject;      end_subject = save_end_subject;
     }  
   
   /* If req_byte is set, we know that that character must appear in the subject  
   for the match to succeed. If the first character is set, req_byte must be  
   later in the subject; otherwise the test starts at the match point. This  
   optimization can save a huge amount of work in patterns with nested unlimited  
   repeats that aren't going to match. Writing separate code for cased/caseless  
   versions makes it go faster, as does using an autoincrement and backing off  
   on a match.  
   
   HOWEVER: when the subject string is very, very long, searching to its end can  
   take a long time, and give bad performance on quite ordinary patterns. This  
   showed up when somebody was matching /^C/ on a 32-megabyte string... so we  
   don't do this when the string is sufficiently long.  
   
   ALSO: this processing is disabled when partial matching is requested, and can  
   also be explicitly deactivated. Furthermore, we have to disable when  
   restarting after a partial match, because the required character may have  
   already been matched. */  
   
   if ((options & PCRE_NO_START_OPTIMIZE) == 0 &&  
       req_byte >= 0 &&  
       end_subject - current_subject < REQ_BYTE_MAX &&  
       (options & (PCRE_PARTIAL_HARD|PCRE_PARTIAL_SOFT|PCRE_DFA_RESTART)) == 0)  
     {  
     register const uschar *p = current_subject + ((first_byte >= 0)? 1 : 0);  
   
     /* We don't need to repeat the search if we haven't yet reached the  
     place we found it at last time. */  
2916    
2917      if (p > req_byte_ptr)      /* The following two optimizations are disabled for partial matching or if
2918        {      disabling is explicitly requested (and of course, by the test above, this
2919        if (req_byte_caseless)      code is not obeyed when restarting after a partial match). */
2920          {  
2921          while (p < end_subject)      if ((options & PCRE_NO_START_OPTIMIZE) == 0 &&
2922            {          (options & (PCRE_PARTIAL_HARD|PCRE_PARTIAL_SOFT)) == 0)
2923            register int pp = *p++;        {
2924            if (pp == req_byte || pp == req_byte2) { p--; break; }        /* If the pattern was studied, a minimum subject length may be set. This
2925            }        is a lower bound; no actual string of that length may actually match the
2926          }        pattern. Although the value is, strictly, in characters, we treat it as
2927        else        bytes to avoid spending too much time in this optimization. */
2928          {  
2929          while (p < end_subject)        if (study != NULL && (study->flags & PCRE_STUDY_MINLEN) != 0 &&
2930              end_subject - current_subject < study->minlength)
2931            return PCRE_ERROR_NOMATCH;
2932    
2933          /* If req_byte is set, we know that that character must appear in the
2934          subject for the match to succeed. If the first character is set, req_byte
2935          must be later in the subject; otherwise the test starts at the match
2936          point. This optimization can save a huge amount of work in patterns with
2937          nested unlimited repeats that aren't going to match. Writing separate
2938          code for cased/caseless versions makes it go faster, as does using an
2939          autoincrement and backing off on a match.
2940    
2941          HOWEVER: when the subject string is very, very long, searching to its end
2942          can take a long time, and give bad performance on quite ordinary
2943          patterns. This showed up when somebody was matching /^C/ on a 32-megabyte
2944          string... so we don't do this when the string is sufficiently long. */
2945    
2946          if (req_byte >= 0 && end_subject - current_subject < REQ_BYTE_MAX)
2947            {
2948            register const uschar *p = current_subject + ((first_byte >= 0)? 1 : 0);
2949    
2950            /* We don't need to repeat the search if we haven't yet reached the
2951            place we found it at last time. */
2952    
2953            if (p > req_byte_ptr)
2954            {            {
2955            if (*p++ == req_byte) { p--; break; }            if (req_byte_caseless)
2956                {
2957                while (p < end_subject)
2958                  {
2959                  register int pp = *p++;
2960                  if (pp == req_byte || pp == req_byte2) { p--; break; }
2961                  }
2962                }
2963              else
2964                {
2965                while (p < end_subject)
2966                  {
2967                  if (*p++ == req_byte) { p--; break; }
2968                  }
2969                }
2970    
2971              /* If we can't find the required character, break the matching loop,
2972              which will cause a return or PCRE_ERROR_NOMATCH. */
2973    
2974              if (p >= end_subject) break;
2975    
2976              /* If we have found the required character, save the point where we
2977              found it, so that we don't search again next time round the loop if
2978              the start hasn't passed this character yet. */
2979    
2980              req_byte_ptr = p;
2981            }            }
2982          }          }
   
       /* If we can't find the required character, break the matching loop,  
       which will cause a return or PCRE_ERROR_NOMATCH. */  
   
       if (p >= end_subject) break;  
   
       /* If we have found the required character, save the point where we  
       found it, so that we don't search again next time round the loop if  
       the start hasn't passed this character yet. */  
   
       req_byte_ptr = p;  
2983        }        }
2984      }      }   /* End of optimizations that are done when not restarting */
2985    
2986    /* OK, now we can do the business */    /* OK, now we can do the business */
2987    
2988      md->start_used_ptr = current_subject;
2989    
2990    rc = internal_dfa_exec(    rc = internal_dfa_exec(
2991      md,                                /* fixed match data */      md,                                /* fixed match data */
2992      md->start_code,                    /* this subexpression's code */      md->start_code,                    /* this subexpression's code */

Legend:
Removed from v.428  
changed lines
  Added in v.459

  ViewVC Help
Powered by ViewVC 1.1.5