Commit:    cddbb98ada6bdba1596ea82386401edf9b680d47
Author:    Gustavo Lopes <glo...@nebm.ist.utl.pt>         Wed, 9 Jan 2013 
22:29:28 +0100
Committer: Gustavo Lopes <gust...@icemobile.com>      Mon, 14 Jan 2013 12:22:42 
+0100
Parents:   2111ee3df54e890c9e2f14b09c01d68445389540
Branches:  PHP-5.4 PHP-5.5 master

Link:       
http://git.php.net/?p=php-src.git;a=commitdiff;h=cddbb98ada6bdba1596ea82386401edf9b680d47

Log:
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.

Changed paths:
  M  ext/standard/string.c


Diff:
diff --git a/ext/standard/string.c b/ext/standard/string.c
index 827f9de..4947a67 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);


--
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to