/[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 406 by ph10, Mon Mar 23 12:05:43 2009 UTC revision 455 by ph10, Sat Sep 26 19:12:32 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 454  for (;;) Line 487  for (;;)
487    int i, j;    int i, j;
488    int clen, dlen;    int clen, dlen;
489    unsigned int c, d;    unsigned int c, d;
490      int forced_fail = 0;
491      int reached_end = 0;
492    
493    /* Make the new state list into the active state list and empty the    /* Make the new state list into the active state list and empty the
494    new state list. */    new state list. */
# Line 543  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 610  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 624  for (;;) Line 662  for (;;)
662            ADD_ACTIVE(state_offset - GET(code, 1), 0);            ADD_ACTIVE(state_offset - GET(code, 1), 0);
663            }            }
664          }          }
665        else if (ptr > current_subject || (md->moptions & PCRE_NOTEMPTY) == 0)        else
666          {          {
667          if (match_count < 0) match_count = (offsetcount >= 2)? 1 : 0;          reached_end++;    /* Count branches that reach the end */
668            else if (match_count > 0 && ++match_count * 2 >= offsetcount)          if (ptr > current_subject ||
669              match_count = 0;              ((md->moptions & PCRE_NOTEMPTY) == 0 &&
670          count = ((match_count == 0)? offsetcount : match_count * 2) - 2;                ((md->moptions & PCRE_NOTEMPTY_ATSTART) == 0 ||
671          if (count > 0) memmove(offsets + 2, offsets, count * sizeof(int));                  current_subject > start_subject + md->start_offset)))
672          if (offsetcount >= 2)            {
673            {            if (match_count < 0) match_count = (offsetcount >= 2)? 1 : 0;
674            offsets[0] = current_subject - start_subject;              else if (match_count > 0 && ++match_count * 2 >= offsetcount)
675            offsets[1] = ptr - start_subject;                match_count = 0;
676            DPRINTF(("%.*sSet matched string = \"%.*s\"\n", rlevel*2-2, SP,            count = ((match_count == 0)? offsetcount : match_count * 2) - 2;
677              offsets[1] - offsets[0], current_subject));            if (count > 0) memmove(offsets + 2, offsets, count * sizeof(int));
678            }            if (offsetcount >= 2)
679          if ((md->moptions & PCRE_DFA_SHORTEST) != 0)              {
680            {              offsets[0] = current_subject - start_subject;
681            DPRINTF(("%.*sEnd of internal_dfa_exec %d: returning %d\n"              offsets[1] = ptr - start_subject;
682              "%.*s---------------------\n\n", rlevel*2-2, SP, rlevel,              DPRINTF(("%.*sSet matched string = \"%.*s\"\n", rlevel*2-2, SP,
683              match_count, rlevel*2-2, SP));                offsets[1] - offsets[0], current_subject));
684            return match_count;              }
685            }            if ((md->moptions & PCRE_DFA_SHORTEST) != 0)
686                {
687                DPRINTF(("%.*sEnd of internal_dfa_exec %d: returning %d\n"
688                  "%.*s---------------------\n\n", rlevel*2-2, SP, rlevel,
689                  match_count, rlevel*2-2, SP));
690                return match_count;
691                }
692              }
693          }          }
694        break;        break;
695    
# Line 794  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 802  for (;;) Line 848  for (;;)
848            }            }
849          else left_word = 0;          else left_word = 0;
850    
851          if (clen > 0) right_word = c < 256 && (ctypes[c] & ctype_word) != 0;          if (clen > 0)
852            else right_word = 0;            right_word = c < 256 && (ctypes[c] & ctype_word) != 0;
853            else              /* This is a fudge to ensure that if this is the */
854              {               /* last item in the pattern, we don't count it as */
855              reached_end--;  /* reached, thus disabling a partial match. */
856              right_word = 0;
857              }
858    
859          if ((left_word == right_word) == (codevalue == OP_NOT_WORD_BOUNDARY))          if ((left_word == right_word) == (codevalue == OP_NOT_WORD_BOUNDARY))
860            { ADD_ACTIVE(state_offset + 1, 0); }            { ADD_ACTIVE(state_offset + 1, 0); }
# Line 2157  for (;;) Line 2208  for (;;)
2208    
2209  /* ========================================================================== */  /* ========================================================================== */
2210        /* These are the opcodes for fancy brackets of various kinds. We have        /* These are the opcodes for fancy brackets of various kinds. We have
2211        to use recursion in order to handle them. The "always failing" assersion        to use recursion in order to handle them. The "always failing" assertion
2212        (?!) is optimised when compiling to OP_FAIL, so we have to support that,        (?!) is optimised to OP_FAIL when compiling, so we have to support that,
2213        though the other "backtracking verbs" are not supported. */        though the other "backtracking verbs" are not supported. */
2214    
2215        case OP_FAIL:        case OP_FAIL:
2216          forced_fail++;    /* Count FAILs for multiple states */
2217        break;        break;
2218    
2219        case OP_ASSERT:        case OP_ASSERT:
# Line 2469  for (;;) Line 2521  for (;;)
2521    /* We have finished the processing at the current subject character. If no    /* We have finished the processing at the current subject character. If no
2522    new states have been set for the next character, we have found all the    new states have been set for the next character, we have found all the
2523    matches that we are going to find. If we are at the top level and partial    matches that we are going to find. If we are at the top level and partial
2524    matching has been requested, check for appropriate conditions. */    matching has been requested, check for appropriate conditions. The "forced_
2525      fail" variable counts the number of (*F) encountered for the character. If it
2526      is equal to the original active_count (saved in workspace[1]) it means that
2527      (*F) was found on every active state. In this case we don't want to give a
2528      partial match. */
2529    
2530    if (new_count <= 0)    if (new_count <= 0)
2531      {      {
2532      if (match_count < 0 &&                     /* No matches found */      if (rlevel == 1 &&                               /* Top level, and */
2533          rlevel == 1 &&                         /* Top level match function */          reached_end != workspace[1] &&               /* Not all reached end */
2534          (md->moptions & PCRE_PARTIAL) != 0 &&  /* Want partial matching */          forced_fail != workspace[1] &&               /* Not all forced fail & */
2535          ptr >= end_subject &&                  /* Reached end of subject */          (                                            /* either... */
2536          ptr > current_subject)                 /* Matched non-empty string */          (md->moptions & PCRE_PARTIAL_HARD) != 0      /* Hard partial */
2537            ||                                           /* or... */
2538            ((md->moptions & PCRE_PARTIAL_SOFT) != 0 &&  /* Soft partial and */
2539             match_count < 0)                            /* no matches */
2540            ) &&                                         /* And... */
2541            ptr >= end_subject &&                     /* Reached end of subject */
2542            ptr > current_subject)                    /* Matched non-empty string */
2543        {        {
2544        if (offsetcount >= 2)        if (offsetcount >= 2)
2545          {          {
2546          offsets[0] = current_subject - start_subject;          offsets[0] = md->start_used_ptr - start_subject;
2547          offsets[1] = end_subject - start_subject;          offsets[1] = end_subject - start_subject;
2548          }          }
2549        match_count = PCRE_ERROR_PARTIAL;        match_count = PCRE_ERROR_PARTIAL;
# Line 2589  if (extra_data != NULL) Line 2651  if (extra_data != NULL)
2651    if ((flags & PCRE_EXTRA_TABLES) != 0)    if ((flags & PCRE_EXTRA_TABLES) != 0)
2652      md->tables = extra_data->tables;      md->tables = extra_data->tables;
2653    }    }
2654    
2655  /* 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,
2656  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
2657  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 2623  md->start_code = (const uschar *)argumen Line 2685  md->start_code = (const uschar *)argumen
2685      re->name_table_offset + re->name_count * re->name_entry_size;      re->name_table_offset + re->name_count * re->name_entry_size;
2686  md->start_subject = (const unsigned char *)subject;  md->start_subject = (const unsigned char *)subject;
2687  md->end_subject = end_subject;  md->end_subject = end_subject;
2688    md->start_offset = start_offset;
2689  md->moptions = options;  md->moptions = options;
2690  md->poptions = re->options;  md->poptions = re->options;
2691    
# Line 2727  if (!anchored) Line 2790  if (!anchored)
2790      }      }
2791    else    else
2792      {      {
2793      if (startline && study != NULL &&      if (!startline && study != NULL &&
2794           (study->options & PCRE_STUDY_MAPPED) != 0)           (study->flags & PCRE_STUDY_MAPPED) != 0)
2795        start_bits = study->start_bits;        start_bits = study->start_bits;
2796      }      }
2797    }    }
# Line 2779  for (;;) Line 2842  for (;;)
2842        }        }
2843    
2844      /* There are some optimizations that avoid running the match if a known      /* There are some optimizations that avoid running the match if a known
2845      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
2846      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. */  
2847    
2848      if ((options & PCRE_NO_START_OPTIMIZE) == 0)      if ((options & PCRE_NO_START_OPTIMIZE) == 0)
2849        {        {
   
2850        /* Advance to a known first byte. */        /* Advance to a known first byte. */
2851    
2852        if (first_byte >= 0)        if (first_byte >= 0)
# Line 2851  for (;;) Line 2912  for (;;)
2912      /* Restore fudged end_subject */      /* Restore fudged end_subject */
2913    
2914      end_subject = save_end_subject;      end_subject = save_end_subject;
     }  
2915    
2916    /* If req_byte is set, we know that that character must appear in the subject      /* The following two optimizations are disabled for partial matching or if
2917    for the match to succeed. If the first character is set, req_byte must be      disabling is explicitly requested (and of course, by the test above, this
2918    later in the subject; otherwise the test starts at the match point. This      code is not obeyed when restarting after a partial match). */
2919    optimization can save a huge amount of work in patterns with nested unlimited  
2920    repeats that aren't going to match. Writing separate code for cased/caseless      if ((options & PCRE_NO_START_OPTIMIZE) == 0 &&
2921    versions makes it go faster, as does using an autoincrement and backing off          (options & (PCRE_PARTIAL_HARD|PCRE_PARTIAL_SOFT)) == 0)
2922    on a match.        {
2923          /* If the pattern was studied, a minimum subject length may be set. This
2924    HOWEVER: when the subject string is very, very long, searching to its end can        is a lower bound; no actual string of that length may actually match the
2925    take a long time, and give bad performance on quite ordinary patterns. This        pattern. Although the value is, strictly, in characters, we treat it as
2926    showed up when somebody was matching /^C/ on a 32-megabyte string... so we        bytes to avoid spending too much time in this optimization. */
2927    don't do this when the string is sufficiently long.  
2928          if (study != NULL && (study->flags & PCRE_STUDY_MINLEN) != 0 &&
2929    ALSO: this processing is disabled when partial matching is requested, and can            end_subject - current_subject < study->minlength)
2930    also be explicitly deactivated. */          return PCRE_ERROR_NOMATCH;
2931    
2932    if ((options & PCRE_NO_START_OPTIMIZE) == 0 &&        /* If req_byte is set, we know that that character must appear in the
2933        req_byte >= 0 &&        subject for the match to succeed. If the first character is set, req_byte
2934        end_subject - current_subject < REQ_BYTE_MAX &&        must be later in the subject; otherwise the test starts at the match
2935        (options & PCRE_PARTIAL) == 0)        point. This optimization can save a huge amount of work in patterns with
2936      {        nested unlimited repeats that aren't going to match. Writing separate
2937      register const uschar *p = current_subject + ((first_byte >= 0)? 1 : 0);        code for cased/caseless versions makes it go faster, as does using an
2938          autoincrement and backing off on a match.
2939      /* We don't need to repeat the search if we haven't yet reached the  
2940      place we found it at last time. */        HOWEVER: when the subject string is very, very long, searching to its end
2941          can take a long time, and give bad performance on quite ordinary
2942      if (p > req_byte_ptr)        patterns. This showed up when somebody was matching /^C/ on a 32-megabyte
2943        {        string... so we don't do this when the string is sufficiently long. */
2944        if (req_byte_caseless)  
2945          {        if (req_byte >= 0 && end_subject - current_subject < REQ_BYTE_MAX)
2946          while (p < end_subject)          {
2947            {          register const uschar *p = current_subject + ((first_byte >= 0)? 1 : 0);
2948            register int pp = *p++;  
2949            if (pp == req_byte || pp == req_byte2) { p--; break; }          /* We don't need to repeat the search if we haven't yet reached the
2950            }          place we found it at last time. */
2951          }  
2952        else          if (p > req_byte_ptr)
         {  
         while (p < end_subject)  
2953            {            {
2954            if (*p++ == req_byte) { p--; break; }            if (req_byte_caseless)
2955                {
2956                while (p < end_subject)
2957                  {
2958                  register int pp = *p++;
2959                  if (pp == req_byte || pp == req_byte2) { p--; break; }
2960                  }
2961                }
2962              else
2963                {
2964                while (p < end_subject)
2965                  {
2966                  if (*p++ == req_byte) { p--; break; }
2967                  }
2968                }
2969    
2970              /* If we can't find the required character, break the matching loop,
2971              which will cause a return or PCRE_ERROR_NOMATCH. */
2972    
2973              if (p >= end_subject) break;
2974    
2975              /* If we have found the required character, save the point where we
2976              found it, so that we don't search again next time round the loop if
2977              the start hasn't passed this character yet. */
2978    
2979              req_byte_ptr = p;
2980            }            }
2981          }          }
   
       /* 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;  
2982        }        }
2983      }      }   /* End of optimizations that are done when not restarting */
2984    
2985    /* OK, now we can do the business */    /* OK, now we can do the business */
2986    
2987      md->start_used_ptr = current_subject;
2988    
2989    rc = internal_dfa_exec(    rc = internal_dfa_exec(
2990      md,                                /* fixed match data */      md,                                /* fixed match data */
2991      md->start_code,                    /* this subexpression's code */      md->start_code,                    /* this subexpression's code */

Legend:
Removed from v.406  
changed lines
  Added in v.455

  ViewVC Help
Powered by ViewVC 1.1.5