From: Ilia Alshanetsky Date: Sun, 11 Jun 2006 21:55:49 +0000 (+0000) Subject: Improved performance of the implode() function on associated arrays by X-Git-Tag: php-5.2.0RC1~325 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=d74f9ff5506c7f120b3158c960ce62f91308b389;p=php Improved performance of the implode() function on associated arrays by 200-300%. --- diff --git a/NEWS b/NEWS index 4135c94e4f..ff017bf50c 100644 --- a/NEWS +++ b/NEWS @@ -1,6 +1,8 @@ PHP NEWS ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| ?? ??? 2006, PHP 5.2.0 +- Improved performance of the implode() function on associated arrays by + 200-300%. (Ilia) - Improved performance of str_replace() when doing 1 char to 1 char or 1 char to many chars replacement by 30-40%. (Ilia) - Added memory_get_peak_usage() function for retrieving peak memory usage of diff --git a/ext/standard/string.c b/ext/standard/string.c index a3707bad21..e28b7c1419 100644 --- a/ext/standard/string.c +++ b/ext/standard/string.c @@ -894,11 +894,23 @@ PHPAPI void php_implode(zval *delim, zval *arr, zval *return_value) while (zend_hash_get_current_data_ex(Z_ARRVAL_P(arr), (void **) &tmp, &pos) == SUCCESS) { if ((*tmp)->type != IS_STRING) { - SEPARATE_ZVAL(tmp); - convert_to_string(*tmp); + if ((*tmp)->type == IS_OBJECT) { + int copy; + zval expr; + zend_make_printable_zval(*tmp, &expr, ©); + smart_str_appendl(&implstr, Z_STRVAL(expr), Z_STRLEN(expr)); + if (copy) { + zval_dtor(&expr); + } + goto next; + } else { + SEPARATE_ZVAL(tmp); + convert_to_string(*tmp); + } } smart_str_appendl(&implstr, Z_STRVAL_PP(tmp), Z_STRLEN_PP(tmp)); +next: if (++i != numelems) { smart_str_appendl(&implstr, Z_STRVAL_P(delim), Z_STRLEN_P(delim)); } @@ -916,6 +928,7 @@ PHP_FUNCTION(implode) { zval **arg1 = NULL, **arg2 = NULL, *delim, *arr; int argc = ZEND_NUM_ARGS(); + HashPosition pos; if (argc < 1 || argc > 2 || zend_get_parameters_ex(argc, &arg1, &arg2) == FAILURE) { @@ -936,12 +949,10 @@ PHP_FUNCTION(implode) arr = *arg1; } else { if (Z_TYPE_PP(arg1) == IS_ARRAY) { - SEPARATE_ZVAL(arg1); arr = *arg1; convert_to_string_ex(arg2); delim = *arg2; } else if (Z_TYPE_PP(arg2) == IS_ARRAY) { - SEPARATE_ZVAL(arg2); arr = *arg2; convert_to_string_ex(arg1); delim = *arg1; @@ -951,8 +962,12 @@ PHP_FUNCTION(implode) } } + pos = Z_ARRVAL_P(arr)->pInternalPointer; + php_implode(delim, arr, return_value); + Z_ARRVAL_P(arr)->pInternalPointer = pos; + if (argc == 1) { FREE_ZVAL(delim); }