4 From: Bram Moolenaar <Bram@moolenaar.net>
6 Content-Type: text/plain; charset=ISO-8859-1
7 Content-Transfer-Encoding: 8bit
11 Problem: Pattern matching is slow when using a lot of simple patterns.
12 Solution: Avoid allocating memory by not freeing it when it's not so much.
17 *** ../vim-7.1.234/src/regexp.c Wed Jan 2 15:34:48 2008
18 --- src/regexp.c Fri Jan 18 20:35:21 2008
22 static char_u *reg_prev_sub = NULL;
24 - #if defined(EXITFREE) || defined(PROTO)
28 - vim_free(reg_prev_sub);
33 * REGEXP_INRANGE contains all characters which are always special in a []
41 ! * regstack and backpos are used by regmatch(). They are kept over calls to
42 ! * avoid invoking malloc() and free() often.
44 ! static garray_T regstack; /* stack with regitem_T items, sometimes
45 ! preceded by regstar_T or regbehind_T. */
46 ! static garray_T backpos; /* table with backpos_T for BACK */
49 * Get pointer to the line "lnum", which is relative to "reg_firstlnum".
54 ! * "regstack" and "backpos" are used by regmatch(). They are kept over calls
55 ! * to avoid invoking malloc() and free() often.
56 ! * "regstack" is a stack with regitem_T items, sometimes preceded by regstar_T
58 ! * "backpos_T" is a table with backpos_T for BACK
60 ! static garray_T regstack = {0, 0, 0, 0, NULL};
61 ! static garray_T backpos = {0, 0, 0, 0, NULL};
64 ! * Both for regstack and backpos tables we use the following strategy of
65 ! * allocation (to reduce malloc/free calls):
66 ! * - Initial size is fairly small.
67 ! * - When needed, the tables are grown bigger (8 times at first, double after
69 ! * - After executing the match we free the memory only if the array has grown.
70 ! * Thus the memory is kept allocated when it's at the initial size.
71 ! * This makes it fast while not keeping a lot of memory allocated.
72 ! * A three times speed increase was observed when using many simple patterns.
74 ! #define REGSTACK_INITIAL 2048
75 ! #define BACKPOS_INITIAL 64
77 ! #if defined(EXITFREE) || defined(PROTO)
81 ! ga_clear(®stack);
83 ! vim_free(reg_tofree);
84 ! vim_free(reg_prev_sub);
89 * Get pointer to the line "lnum", which is relative to "reg_firstlnum".
97 ! /* Init the regstack empty. Use an item size of 1 byte, since we push
98 ! * different things onto it. Use a large grow size to avoid reallocating
100 ! ga_init2(®stack, 1, 10000);
102 ! /* Init the backpos table empty. */
103 ! ga_init2(&backpos, sizeof(backpos_T), 10);
111 ! /* Create "regstack" and "backpos" if they are not allocated yet.
112 ! * We allocate *_INITIAL amount of bytes first and then set the grow size
113 ! * to much bigger value to avoid many malloc calls in case of deep regular
115 ! if (regstack.ga_data == NULL)
117 ! /* Use an item size of 1 byte, since we push different things
118 ! * onto the regstack. */
119 ! ga_init2(®stack, 1, REGSTACK_INITIAL);
120 ! ga_grow(®stack, REGSTACK_INITIAL);
121 ! regstack.ga_growsize = REGSTACK_INITIAL * 8;
124 ! if (backpos.ga_data == NULL)
126 ! ga_init2(&backpos, sizeof(backpos_T), BACKPOS_INITIAL);
127 ! ga_grow(&backpos, BACKPOS_INITIAL);
128 ! backpos.ga_growsize = BACKPOS_INITIAL * 8;
138 ! vim_free(reg_tofree);
139 ! ga_clear(®stack);
140 ! ga_clear(&backpos);
148 ! /* Free "reg_tofree" when it's a bit big.
149 ! * Free regstack and backpos if they are bigger than their initial size. */
150 ! if (reg_tofreelen > 400)
152 ! vim_free(reg_tofree);
155 ! if (regstack.ga_maxlen > REGSTACK_INITIAL)
156 ! ga_clear(®stack);
157 ! if (backpos.ga_maxlen > BACKPOS_INITIAL)
158 ! ga_clear(&backpos);
164 #define RA_MATCH 4 /* successful match */
165 #define RA_NOMATCH 5 /* didn't match */
167 ! /* Init the regstack and backpos table empty. They are initialized and
168 ! * freed in vim_regexec_both() to reduce malloc()/free() calls. */
173 #define RA_MATCH 4 /* successful match */
174 #define RA_NOMATCH 5 /* didn't match */
176 ! /* Make "regstack" and "backpos" empty. They are allocated and freed in
177 ! * vim_regexec_both() to reduce malloc()/free() calls. */
181 *** ../vim-7.1.234/src/version.c Fri Jan 18 17:39:10 2008
182 --- src/version.c Fri Jan 18 20:33:26 2008
186 { /* Add new patch number below this line */
192 NEIL INNES PLAYED: THE FIRST SELF-DESTRUCTIVE MONK, ROBIN'S LEAST FAVORITE
193 MINSTREL, THE PAGE CRUSHED BY A RABBIT, THE OWNER OF A DUCK
194 "Monty Python and the Holy Grail" PYTHON (MONTY) PICTURES LTD
196 /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net \\\
197 /// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
198 \\\ download, build and distribute -- http://www.A-A-P.org ///
199 \\\ help me help AIDS victims -- http://ICCF-Holland.org ///