summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGustavo Lopes <glopes@nebm.ist.utl.pt>2013-01-09 22:29:28 +0100
committerGustavo Lopes <gustavo@icemobile.com>2013-01-14 12:22:42 +0100
commitcddbb98ada6bdba1596ea82386401edf9b680d47 (patch)
tree244677439f709dd8d8b225b4e0188a8320cb3c24
parent2111ee3df54e890c9e2f14b09c01d68445389540 (diff)
downloadphp-git-cddbb98ada6bdba1596ea82386401edf9b680d47.tar.gz
strtr() with 2nd param array - optimization
About a 1.25x speedup in my test script by writing the result string only when a match is found and at the end instead of on each iteration.
-rw-r--r--ext/standard/string.c11
1 files changed, 5 insertions, 6 deletions
diff --git a/ext/standard/string.c b/ext/standard/string.c
index 827f9dec22..4947a67f86 100644
--- a/ext/standard/string.c
+++ b/ext/standard/string.c
@@ -3028,6 +3028,7 @@ static void php_strtr_array_destroy_ppres(PPRES *d)
static void php_strtr_array_do_repl(STR *text, PPRES *d, zval *return_value)
{
STRLEN pos = 0,
+ nextwpos = 0,
lastpos = L(text) - d->m;
smart_str result = {0};
@@ -3036,7 +3037,6 @@ static void php_strtr_array_do_repl(STR *text, PPRES *d, zval *return_value)
STRLEN shift = d->shift->entries[h];
if (shift > 0) {
- smart_str_appendl(&result, &S(text)[pos], MIN(shift, L(text) - pos));
pos += shift;
} else {
HASH h2 = h & d->hash->table_mask,
@@ -3056,20 +3056,19 @@ static void php_strtr_array_do_repl(STR *text, PPRES *d, zval *return_value)
memcmp(S(&pnr->pat), &S(text)[pos], L(&pnr->pat)) != 0)
continue;
- smart_str_appendl(&result, S(&pnr->repl), (int)L(&pnr->repl));
+ smart_str_appendl(&result, &S(text)[nextwpos], pos - nextwpos);
+ smart_str_appendl(&result, S(&pnr->repl), L(&pnr->repl));
pos += L(&pnr->pat);
+ nextwpos = pos;
goto end_outer_loop;
}
- smart_str_appendc(&result, S(text)[pos]);
pos++;
end_outer_loop: ;
}
}
- if (pos < L(text)) {
- smart_str_appendl(&result, &S(text)[pos], (int)(L(text) - pos));
- }
+ smart_str_appendl(&result, &S(text)[nextwpos], L(text) - nextwpos);
if (result.c != NULL) {
smart_str_0(&result);