[INTERPRETER]

* OPT: Optimization in Replace() when the replace string is longer than 
  the search string. String[].Join() becomes faster too.


git-svn-id: svn://localhost/gambas/trunk@1683 867c0c6c-44f3-4631-809d-bfa615b0a4ec
This commit is contained in:
Benoît Minisini 2008-11-08 23:42:08 +00:00
parent 155dab4b86
commit db6b81aa59
4 changed files with 66 additions and 79 deletions

1
TODO
View file

@ -57,6 +57,7 @@ DEVELOPMENT ENVIRONMENT
- Generates an index control->component to suggest components for missing controls.
- Be able to open a .tar.gz project, and compress it back when the project is closed.
- Ability to merge projects.
- A checkbox in the property sheet to make controls public individually
GUI RELATED STUFF

View file

@ -608,65 +608,38 @@ void SUBR_replace(void)
if (NPARAM == 4)
nocase = SUBR_get_integer(&PARAM[3]) == GB_COMP_TEXT;
SUBST_init();
if (lp >= lr)
SUBST_init_max(ls);
else
SUBST_init_ext(ls, (lr - lp) * 16);
if (ls > 0 && lp > 0)
{
is = 0;
if (lp >= lr)
SUBST_init_max(ls);
/* if (lp == lr)
{
STRING_new(&SUBST_buffer, ps, ls);
ps = SUBST_buffer;
for(;;)
{
pos = STRING_search(ps, ls, pp, lp, 1, FALSE, nocase);
if (pos == 0)
break;
for(;;)
{
pos = STRING_search(ps, ls, pp, lp, 1, FALSE, nocase);
if (pos == 0)
break;
pos--;
memcpy(&ps[pos], pr, lr);
pos--;
pos += lp;
if (pos > 0)
SUBST_add(ps, pos);
ps += pos;
ls -= pos;
SUBST_add(pr, lr);
if (ls <= 0)
break;
}
}
else*/
{
for(;;)
{
pos = STRING_search(ps, ls, pp, lp, 1, FALSE, nocase);
if (pos == 0)
break;
pos += lp;
pos--;
ps += pos;
ls -= pos;
if (pos > 0)
SUBST_add(ps, pos);
SUBST_add(pr, lr);
pos += lp;
ps += pos;
ls -= pos;
if (ls <= 0)
break;
}
SUBST_add(ps, ls);
}
if (ls <= 0)
break;
}
SUBST_add(ps, ls);
}
SUBST_exit();

View file

@ -36,8 +36,10 @@ char *SUBST_buffer = NULL;
char SUBST_temp[SUBST_TEMP_SIZE];
int SUBST_ntemp;
static bool _prealloc;
static char *_prealloc_ptr;
static int _inc = 0;
static int _len = 0;
static int _max = 0;
static char *_ptr;
void SUBST_dump_temp(void)
{
@ -50,26 +52,26 @@ void SUBST_dump_temp(void)
}
}
void SUBST_init(void)
void SUBST_init_ext(int len, int inc)
{
SUBST_buffer = NULL;
if (inc == 0)
inc = 32;
if (len == 0)
len = inc;
STRING_new(&SUBST_buffer, NULL, len);
_inc = inc;
_max = len;
_len = 0;
_ptr = SUBST_buffer;
SUBST_ntemp = 0;
_prealloc = FALSE;
}
void SUBST_init_max(int max)
{
SUBST_ntemp = 0;
STRING_new(&SUBST_buffer, NULL, max);
_prealloc = TRUE;
_prealloc_ptr = SUBST_buffer;
}
// len == 0 est possible ! On peut vouloir ajouter une chaîne vide.
void SUBST_add(const char *src, int len)
{
int old_len;
int pos;
if (!src)
return;
@ -82,28 +84,36 @@ void SUBST_add(const char *src, int len)
SUBST_dump_temp();
if (_prealloc)
_len += len;
if (_len >= _max)
{
memcpy(_prealloc_ptr, src, len);
_prealloc_ptr += len;
}
else
{
old_len = STRING_length(SUBST_buffer);
STRING_extend(&SUBST_buffer, old_len + len);
memcpy(&SUBST_buffer[old_len], src, len);
pos = (_len - _max) * 4;
if (pos < _inc)
pos = _inc;
else
{
_inc = (pos + 31) & ~31;
if (_inc > 1024)
_inc = 1024;
}
_max += pos;
//fprintf(stderr, "STRING_extend: %d\n", _max - STRING_length(SUBST_buffer));
pos = _ptr - SUBST_buffer;
STRING_extend(&SUBST_buffer, _max);
_ptr = &SUBST_buffer[pos];
}
memcpy(_ptr, src, len);
_ptr += len;
}
void SUBST_exit(void)
{
SUBST_dump_temp();
if (_prealloc)
{
STRING_extend(&SUBST_buffer, _prealloc_ptr - SUBST_buffer);
_prealloc = FALSE;
}
STRING_extend(&SUBST_buffer, _len);
STRING_extend_end(&SUBST_buffer);
}

View file

@ -39,8 +39,8 @@ typedef
typedef
void (*SUBST_ADD_FUNC)(int);
void SUBST_init(void);
void SUBST_init_max(int max);
void SUBST_init(int len, int inc);
void SUBST_init_ext(int len, int inc);
void SUBST_add(const char *src, int len);
void SUBST_exit(void);
void SUBST_dump_temp(void);
@ -52,4 +52,7 @@ void SUBST_dump_temp(void);
SUBST_temp[SUBST_ntemp++]= (_c); \
}
#define SUBST_init() SUBST_init_ext(0, 0)
#define SUBST_init_max(_max) SUBST_init_ext((_max), 0)
#endif