]> granicus.if.org Git - php/commitdiff
strtr() with 2nd param array - optimization
authorGustavo Lopes <glopes@nebm.ist.utl.pt>
Wed, 9 Jan 2013 21:29:28 +0000 (22:29 +0100)
committerGustavo Lopes <gustavo@icemobile.com>
Mon, 14 Jan 2013 11:22:42 +0000 (12:22 +0100)
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.

ext/standard/string.c

index 827f9dec22ab8e95747c1dfa1e4f4c774dc0dd41..4947a67f860e669b9e36d8333ecd8da4161adc4f 100644 (file)
@@ -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);