Mirror of the Vim source from https://github.com/vim/vim
Revision | cd6175cc27d9c8b4d4f4dadc7c38f6c490f3fa62 (tree) |
---|---|
Zeit | 2008-01-19 04:37:23 |
Autor | vimboss |
Commiter | vimboss |
updated for version 7.1-235
@@ -378,14 +378,6 @@ | ||
378 | 378 | |
379 | 379 | static char_u *reg_prev_sub = NULL; |
380 | 380 | |
381 | -#if defined(EXITFREE) || defined(PROTO) | |
382 | - void | |
383 | -free_regexp_stuff() | |
384 | -{ | |
385 | - vim_free(reg_prev_sub); | |
386 | -} | |
387 | -#endif | |
388 | - | |
389 | 381 | /* |
390 | 382 | * REGEXP_INRANGE contains all characters which are always special in a [] |
391 | 383 | * range after '\'. |
@@ -3206,12 +3198,39 @@ | ||
3206 | 3198 | } backpos_T; |
3207 | 3199 | |
3208 | 3200 | /* |
3209 | - * regstack and backpos are used by regmatch(). They are kept over calls to | |
3210 | - * avoid invoking malloc() and free() often. | |
3201 | + * "regstack" and "backpos" are used by regmatch(). They are kept over calls | |
3202 | + * to avoid invoking malloc() and free() often. | |
3203 | + * "regstack" is a stack with regitem_T items, sometimes preceded by regstar_T | |
3204 | + * or regbehind_T. | |
3205 | + * "backpos_T" is a table with backpos_T for BACK | |
3211 | 3206 | */ |
3212 | -static garray_T regstack; /* stack with regitem_T items, sometimes | |
3213 | - preceded by regstar_T or regbehind_T. */ | |
3214 | -static garray_T backpos; /* table with backpos_T for BACK */ | |
3207 | +static garray_T regstack = {0, 0, 0, 0, NULL}; | |
3208 | +static garray_T backpos = {0, 0, 0, 0, NULL}; | |
3209 | + | |
3210 | +/* | |
3211 | + * Both for regstack and backpos tables we use the following strategy of | |
3212 | + * allocation (to reduce malloc/free calls): | |
3213 | + * - Initial size is fairly small. | |
3214 | + * - When needed, the tables are grown bigger (8 times at first, double after | |
3215 | + * that). | |
3216 | + * - After executing the match we free the memory only if the array has grown. | |
3217 | + * Thus the memory is kept allocated when it's at the initial size. | |
3218 | + * This makes it fast while not keeping a lot of memory allocated. | |
3219 | + * A three times speed increase was observed when using many simple patterns. | |
3220 | + */ | |
3221 | +#define REGSTACK_INITIAL 2048 | |
3222 | +#define BACKPOS_INITIAL 64 | |
3223 | + | |
3224 | +#if defined(EXITFREE) || defined(PROTO) | |
3225 | + void | |
3226 | +free_regexp_stuff() | |
3227 | +{ | |
3228 | + ga_clear(®stack); | |
3229 | + ga_clear(&backpos); | |
3230 | + vim_free(reg_tofree); | |
3231 | + vim_free(reg_prev_sub); | |
3232 | +} | |
3233 | +#endif | |
3215 | 3234 | |
3216 | 3235 | /* |
3217 | 3236 | * Get pointer to the line "lnum", which is relative to "reg_firstlnum". |
@@ -3346,15 +3365,25 @@ | ||
3346 | 3365 | char_u *s; |
3347 | 3366 | long retval = 0L; |
3348 | 3367 | |
3349 | - reg_tofree = NULL; | |
3350 | - | |
3351 | - /* Init the regstack empty. Use an item size of 1 byte, since we push | |
3352 | - * different things onto it. Use a large grow size to avoid reallocating | |
3353 | - * it too often. */ | |
3354 | - ga_init2(®stack, 1, 10000); | |
3355 | - | |
3356 | - /* Init the backpos table empty. */ | |
3357 | - ga_init2(&backpos, sizeof(backpos_T), 10); | |
3368 | + /* Create "regstack" and "backpos" if they are not allocated yet. | |
3369 | + * We allocate *_INITIAL amount of bytes first and then set the grow size | |
3370 | + * to much bigger value to avoid many malloc calls in case of deep regular | |
3371 | + * expressions. */ | |
3372 | + if (regstack.ga_data == NULL) | |
3373 | + { | |
3374 | + /* Use an item size of 1 byte, since we push different things | |
3375 | + * onto the regstack. */ | |
3376 | + ga_init2(®stack, 1, REGSTACK_INITIAL); | |
3377 | + ga_grow(®stack, REGSTACK_INITIAL); | |
3378 | + regstack.ga_growsize = REGSTACK_INITIAL * 8; | |
3379 | + } | |
3380 | + | |
3381 | + if (backpos.ga_data == NULL) | |
3382 | + { | |
3383 | + ga_init2(&backpos, sizeof(backpos_T), BACKPOS_INITIAL); | |
3384 | + ga_grow(&backpos, BACKPOS_INITIAL); | |
3385 | + backpos.ga_growsize = BACKPOS_INITIAL * 8; | |
3386 | + } | |
3358 | 3387 | |
3359 | 3388 | if (REG_MULTI) |
3360 | 3389 | { |
@@ -3525,9 +3554,17 @@ | ||
3525 | 3554 | } |
3526 | 3555 | |
3527 | 3556 | theend: |
3528 | - vim_free(reg_tofree); | |
3529 | - ga_clear(®stack); | |
3530 | - ga_clear(&backpos); | |
3557 | + /* Free "reg_tofree" when it's a bit big. | |
3558 | + * Free regstack and backpos if they are bigger than their initial size. */ | |
3559 | + if (reg_tofreelen > 400) | |
3560 | + { | |
3561 | + vim_free(reg_tofree); | |
3562 | + reg_tofree = NULL; | |
3563 | + } | |
3564 | + if (regstack.ga_maxlen > REGSTACK_INITIAL) | |
3565 | + ga_clear(®stack); | |
3566 | + if (backpos.ga_maxlen > BACKPOS_INITIAL) | |
3567 | + ga_clear(&backpos); | |
3531 | 3568 | |
3532 | 3569 | return retval; |
3533 | 3570 | } |
@@ -3717,8 +3754,8 @@ | ||
3717 | 3754 | #define RA_MATCH 4 /* successful match */ |
3718 | 3755 | #define RA_NOMATCH 5 /* didn't match */ |
3719 | 3756 | |
3720 | - /* Init the regstack and backpos table empty. They are initialized and | |
3721 | - * freed in vim_regexec_both() to reduce malloc()/free() calls. */ | |
3757 | + /* Make "regstack" and "backpos" empty. They are allocated and freed in | |
3758 | + * vim_regexec_both() to reduce malloc()/free() calls. */ | |
3722 | 3759 | regstack.ga_len = 0; |
3723 | 3760 | backpos.ga_len = 0; |
3724 | 3761 |
@@ -667,6 +667,8 @@ | ||
667 | 667 | static int included_patches[] = |
668 | 668 | { /* Add new patch number below this line */ |
669 | 669 | /**/ |
670 | + 235, | |
671 | +/**/ | |
670 | 672 | 234, |
671 | 673 | /**/ |
672 | 674 | 233, |