9c34900
To: vim-dev@vim.org
9c34900
Subject: Patch 7.1.235
9c34900
Fcc: outbox
9c34900
From: Bram Moolenaar <Bram@moolenaar.net>
9c34900
Mime-Version: 1.0
9c34900
Content-Type: text/plain; charset=ISO-8859-1
9c34900
Content-Transfer-Encoding: 8bit
9c34900
------------
9c34900
9c34900
Patch 7.1.235
9c34900
Problem:    Pattern matching is slow when using a lot of simple patterns.
9c34900
Solution:   Avoid allocating memory by not freeing it when it's not so much.
9c34900
	    (Alexei Alexandrov)
9c34900
Files:	    src/regexp.c
9c34900
9c34900
9c34900
*** ../vim-7.1.234/src/regexp.c	Wed Jan  2 15:34:48 2008
9c34900
--- src/regexp.c	Fri Jan 18 20:35:21 2008
9c34900
***************
9c34900
*** 378,391 ****
9c34900
  
9c34900
  static char_u		*reg_prev_sub = NULL;
9c34900
  
9c34900
- #if defined(EXITFREE) || defined(PROTO)
9c34900
-     void
9c34900
- free_regexp_stuff()
9c34900
- {
9c34900
-     vim_free(reg_prev_sub);
9c34900
- }
9c34900
- #endif
9c34900
- 
9c34900
  /*
9c34900
   * REGEXP_INRANGE contains all characters which are always special in a []
9c34900
   * range after '\'.
9c34900
--- 378,383 ----
9c34900
***************
9c34900
*** 3206,3217 ****
9c34900
  } backpos_T;
9c34900
  
9c34900
  /*
9c34900
!  * regstack and backpos are used by regmatch().  They are kept over calls to
9c34900
!  * avoid invoking malloc() and free() often.
9c34900
   */
9c34900
! static garray_T	regstack;	/* stack with regitem_T items, sometimes
9c34900
! 				   preceded by regstar_T or regbehind_T. */
9c34900
! static garray_T	backpos;	/* table with backpos_T for BACK */
9c34900
  
9c34900
  /*
9c34900
   * Get pointer to the line "lnum", which is relative to "reg_firstlnum".
9c34900
--- 3198,3236 ----
9c34900
  } backpos_T;
9c34900
  
9c34900
  /*
9c34900
!  * "regstack" and "backpos" are used by regmatch().  They are kept over calls
9c34900
!  * to avoid invoking malloc() and free() often.
9c34900
!  * "regstack" is a stack with regitem_T items, sometimes preceded by regstar_T
9c34900
!  * or regbehind_T.
9c34900
!  * "backpos_T" is a table with backpos_T for BACK
9c34900
!  */
9c34900
! static garray_T	regstack = {0, 0, 0, 0, NULL};
9c34900
! static garray_T	backpos = {0, 0, 0, 0, NULL};
9c34900
! 
9c34900
! /*
9c34900
!  * Both for regstack and backpos tables we use the following strategy of
9c34900
!  * allocation (to reduce malloc/free calls):
9c34900
!  * - Initial size is fairly small.
9c34900
!  * - When needed, the tables are grown bigger (8 times at first, double after
9c34900
!  *   that).
9c34900
!  * - After executing the match we free the memory only if the array has grown.
9c34900
!  *   Thus the memory is kept allocated when it's at the initial size.
9c34900
!  * This makes it fast while not keeping a lot of memory allocated.
9c34900
!  * A three times speed increase was observed when using many simple patterns.
9c34900
   */
9c34900
! #define REGSTACK_INITIAL	2048
9c34900
! #define BACKPOS_INITIAL		64
9c34900
! 
9c34900
! #if defined(EXITFREE) || defined(PROTO)
9c34900
!     void
9c34900
! free_regexp_stuff()
9c34900
! {
9c34900
!     ga_clear(&regstack);
9c34900
!     ga_clear(&backpos);
9c34900
!     vim_free(reg_tofree);
9c34900
!     vim_free(reg_prev_sub);
9c34900
! }
9c34900
! #endif
9c34900
  
9c34900
  /*
9c34900
   * Get pointer to the line "lnum", which is relative to "reg_firstlnum".
9c34900
***************
9c34900
*** 3346,3360 ****
9c34900
      char_u	*s;
9c34900
      long	retval = 0L;
9c34900
  
9c34900
!     reg_tofree = NULL;
9c34900
! 
9c34900
!     /* Init the regstack empty.  Use an item size of 1 byte, since we push
9c34900
!      * different things onto it.  Use a large grow size to avoid reallocating
9c34900
!      * it too often. */
9c34900
!     ga_init2(&regstack, 1, 10000);
9c34900
! 
9c34900
!     /* Init the backpos table empty. */
9c34900
!     ga_init2(&backpos, sizeof(backpos_T), 10);
9c34900
  
9c34900
      if (REG_MULTI)
9c34900
      {
9c34900
--- 3365,3389 ----
9c34900
      char_u	*s;
9c34900
      long	retval = 0L;
9c34900
  
9c34900
!     /* Create "regstack" and "backpos" if they are not allocated yet.
9c34900
!      * We allocate *_INITIAL amount of bytes first and then set the grow size
9c34900
!      * to much bigger value to avoid many malloc calls in case of deep regular
9c34900
!      * expressions.  */
9c34900
!     if (regstack.ga_data == NULL)
9c34900
!     {
9c34900
! 	/* Use an item size of 1 byte, since we push different things
9c34900
! 	 * onto the regstack. */
9c34900
! 	ga_init2(&regstack, 1, REGSTACK_INITIAL);
9c34900
! 	ga_grow(&regstack, REGSTACK_INITIAL);
9c34900
! 	regstack.ga_growsize = REGSTACK_INITIAL * 8;
9c34900
!     }
9c34900
! 
9c34900
!     if (backpos.ga_data == NULL)
9c34900
!     {
9c34900
! 	ga_init2(&backpos, sizeof(backpos_T), BACKPOS_INITIAL);
9c34900
! 	ga_grow(&backpos, BACKPOS_INITIAL);
9c34900
! 	backpos.ga_growsize = BACKPOS_INITIAL * 8;
9c34900
!     }
9c34900
  
9c34900
      if (REG_MULTI)
9c34900
      {
9c34900
***************
9c34900
*** 3525,3533 ****
9c34900
      }
9c34900
  
9c34900
  theend:
9c34900
!     vim_free(reg_tofree);
9c34900
!     ga_clear(&regstack);
9c34900
!     ga_clear(&backpos);
9c34900
  
9c34900
      return retval;
9c34900
  }
9c34900
--- 3554,3570 ----
9c34900
      }
9c34900
  
9c34900
  theend:
9c34900
!     /* Free "reg_tofree" when it's a bit big.
9c34900
!      * Free regstack and backpos if they are bigger than their initial size. */
9c34900
!     if (reg_tofreelen > 400)
9c34900
!     {
9c34900
! 	vim_free(reg_tofree);
9c34900
! 	reg_tofree = NULL;
9c34900
!     }
9c34900
!     if (regstack.ga_maxlen > REGSTACK_INITIAL)
9c34900
! 	ga_clear(&regstack);
9c34900
!     if (backpos.ga_maxlen > BACKPOS_INITIAL)
9c34900
! 	ga_clear(&backpos);
9c34900
  
9c34900
      return retval;
9c34900
  }
9c34900
***************
9c34900
*** 3717,3724 ****
9c34900
  #define RA_MATCH	4	/* successful match */
9c34900
  #define RA_NOMATCH	5	/* didn't match */
9c34900
  
9c34900
!   /* Init the regstack and backpos table empty.  They are initialized and
9c34900
!    * freed in vim_regexec_both() to reduce malloc()/free() calls. */
9c34900
    regstack.ga_len = 0;
9c34900
    backpos.ga_len = 0;
9c34900
  
9c34900
--- 3754,3761 ----
9c34900
  #define RA_MATCH	4	/* successful match */
9c34900
  #define RA_NOMATCH	5	/* didn't match */
9c34900
  
9c34900
!   /* Make "regstack" and "backpos" empty.  They are allocated and freed in
9c34900
!    * vim_regexec_both() to reduce malloc()/free() calls. */
9c34900
    regstack.ga_len = 0;
9c34900
    backpos.ga_len = 0;
9c34900
  
9c34900
*** ../vim-7.1.234/src/version.c	Fri Jan 18 17:39:10 2008
9c34900
--- src/version.c	Fri Jan 18 20:33:26 2008
9c34900
***************
9c34900
*** 668,669 ****
9c34900
--- 668,671 ----
9c34900
  {   /* Add new patch number below this line */
9c34900
+ /**/
9c34900
+     235,
9c34900
  /**/
9c34900
9c34900
-- 
9c34900
NEIL INNES PLAYED: THE FIRST SELF-DESTRUCTIVE MONK, ROBIN'S LEAST FAVORITE
9c34900
                   MINSTREL, THE PAGE CRUSHED BY A RABBIT, THE OWNER OF A DUCK
9c34900
                 "Monty Python and the Holy Grail" PYTHON (MONTY) PICTURES LTD
9c34900
9c34900
 /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net   \\\
9c34900
///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
9c34900
\\\        download, build and distribute -- http://www.A-A-P.org        ///
9c34900
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///