cvs: php-src(PHP_5_3) /ext/standard array.c string.c var_unserializer.re ZendEngine2 zend_API.c zend_API.h zend_builtin_functions.c
zend_object_handlers.c

From: Date: Tue, 27 May 2008 10:29:34 +0000
Subject: cvs: php-src(PHP_5_3) /ext/standard array.c string.c var_unserializer.re ZendEngine2 zend_API.c zend_API.h zend_builtin_functions.c
zend_object_handlers.c
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-50169@lists.php.net to get a copy of this message
mattwil Tue May 27 10:29:34 2008 UTC Modified files: (Branch: PHP_5_3) /ZendEngine2 zend_API.c zend_API.h zend_builtin_functions.c zend_object_handlers.c /php-src/ext/standard array.c string.c var_unserializer.re Log: MFH: Add array_init_size() and use it where array size is known at initialization

http://cvs.php.net/viewvc.cgi/ZendEngine2/zend_API.c?r1=1.296.2.27.2.34.2.36&r2=1.296.2.27.2.34.2.37&diff_format=u Index: ZendEngine2/zend_API.c diff -u ZendEngine2/zend_API.c:1.296.2.27.2.34.2.36 ZendEngine2/zend_API.c:1.296.2.27.2.34.2.37 --- ZendEngine2/zend_API.c:1.296.2.27.2.34.2.36 Mon May 12 07:11:54 2008 +++ ZendEngine2/zend_API.c Tue May 27 10:29:33 2008 @@ -18,7 +18,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: zend_API.c,v 1.296.2.27.2.34.2.36 2008/05/12 07:11:54 dmitry Exp $ */ +/* $Id: zend_API.c,v 1.296.2.27.2.34.2.37 2008/05/27 10:29:33 mattwil Exp $ */ #include "zend.h" #include "zend_execute.h" @@ -922,11 +922,11 @@ /* }}} */ /* Argument parsing API -- andrei */ -ZEND_API int _array_init(zval *arg ZEND_FILE_LINE_DC) /* {{{ */ +ZEND_API int _array_init(zval *arg, uint size ZEND_FILE_LINE_DC) /* {{{ */ { ALLOC_HASHTABLE_REL(Z_ARRVAL_P(arg)); - _zend_hash_init(Z_ARRVAL_P(arg), 0, NULL, ZVAL_PTR_DTOR, 0 ZEND_FILE_LINE_RELAY_CC); + _zend_hash_init(Z_ARRVAL_P(arg), size, NULL, ZVAL_PTR_DTOR, 0 ZEND_FILE_LINE_RELAY_CC); Z_TYPE_P(arg) = IS_ARRAY; return SUCCESS; } http://cvs.php.net/viewvc.cgi/ZendEngine2/zend_API.h?r1=1.207.2.8.2.8.2.11&r2=1.207.2.8.2.8.2.12&diff_format=u Index: ZendEngine2/zend_API.h diff -u ZendEngine2/zend_API.h:1.207.2.8.2.8.2.11 ZendEngine2/zend_API.h:1.207.2.8.2.8.2.12 --- ZendEngine2/zend_API.h:1.207.2.8.2.8.2.11 Mon May 12 07:11:55 2008 +++ ZendEngine2/zend_API.h Tue May 27 10:29:33 2008 @@ -18,7 +18,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: zend_API.h,v 1.207.2.8.2.8.2.11 2008/05/12 07:11:55 dmitry Exp $ */ +/* $Id: zend_API.h,v 1.207.2.8.2.8.2.12 2008/05/27 10:29:33 mattwil Exp $ */ #ifndef ZEND_API_H #define ZEND_API_H @@ -316,11 +316,12 @@ #define DLEXPORT #endif -#define array_init(arg) _array_init((arg) ZEND_FILE_LINE_CC) +#define array_init(arg) _array_init((arg), 0 ZEND_FILE_LINE_CC) +#define array_init_size(arg, size) _array_init((arg), (size) ZEND_FILE_LINE_CC) #define object_init(arg) _object_init((arg) ZEND_FILE_LINE_CC TSRMLS_CC) #define object_init_ex(arg, ce) _object_init_ex((arg), (ce) ZEND_FILE_LINE_CC TSRMLS_CC) #define object_and_properties_init(arg, ce, properties) _object_and_properties_init((arg), (ce), (properties) ZEND_FILE_LINE_CC TSRMLS_CC) -ZEND_API int _array_init(zval *arg ZEND_FILE_LINE_DC); +ZEND_API int _array_init(zval *arg, uint size ZEND_FILE_LINE_DC); ZEND_API int _object_init(zval *arg ZEND_FILE_LINE_DC TSRMLS_DC); ZEND_API int _object_init_ex(zval *arg, zend_class_entry *ce ZEND_FILE_LINE_DC TSRMLS_DC); ZEND_API int _object_and_properties_init(zval *arg, zend_class_entry *ce, HashTable *properties ZEND_FILE_LINE_DC TSRMLS_DC); http://cvs.php.net/viewvc.cgi/ZendEngine2/zend_builtin_functions.c?r1=1.277.2.12.2.25.2.16&r2=1.277.2.12.2.25.2.17&diff_format=u Index: ZendEngine2/zend_builtin_functions.c diff -u ZendEngine2/zend_builtin_functions.c:1.277.2.12.2.25.2.16 ZendEngine2/zend_builtin_functions.c:1.277.2.12.2.25.2.17 --- ZendEngine2/zend_builtin_functions.c:1.277.2.12.2.25.2.16 Mon May 12 07:11:55 2008 +++ ZendEngine2/zend_builtin_functions.c Tue May 27 10:29:33 2008 @@ -17,7 +17,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: zend_builtin_functions.c,v 1.277.2.12.2.25.2.16 2008/05/12 07:11:55 dmitry Exp $ */ +/* $Id: zend_builtin_functions.c,v 1.277.2.12.2.25.2.17 2008/05/27 10:29:33 mattwil Exp $ */ #include "zend.h" #include "zend_API.h" @@ -285,7 +285,7 @@ p = ex->function_state.arguments; arg_count = (int)(zend_uintptr_t) *p; /* this is the amount of arguments passed to func_get_args(); */ - array_init(return_value); + array_init_size(return_value, arg_count); for (i=0; i<arg_count; i++) { zval *element; @@ -1526,16 +1526,14 @@ Returns an associative array of names and values of all currently defined variable names (variables in the current scope) */ ZEND_FUNCTION(get_defined_vars) { - zval *tmp; - - array_init(return_value); - if (!EG(active_symbol_table)) { zend_rebuild_symbol_table(TSRMLS_C); } + array_init_size(return_value, zend_hash_num_elements(EG(active_symbol_table))); + zend_hash_copy(Z_ARRVAL_P(return_value), EG(active_symbol_table), - (copy_ctor_func_t)zval_add_ref, &tmp, sizeof(zval *)); + (copy_ctor_func_t)zval_add_ref, NULL, sizeof(zval *)); } /* }}} */ @@ -1764,7 +1762,7 @@ int arg_count = (int)(zend_uintptr_t) *p; MAKE_STD_ZVAL(arg_array); - array_init(arg_array); + array_init_size(arg_array, arg_count); p -= arg_count; while (--arg_count >= 0) { http://cvs.php.net/viewvc.cgi/ZendEngine2/zend_object_handlers.c?r1=1.135.2.6.2.22.2.15&r2=1.135.2.6.2.22.2.16&diff_format=u Index: ZendEngine2/zend_object_handlers.c diff -u ZendEngine2/zend_object_handlers.c:1.135.2.6.2.22.2.15 ZendEngine2/zend_object_handlers.c:1.135.2.6.2.22.2.16 --- ZendEngine2/zend_object_handlers.c:1.135.2.6.2.22.2.15 Sat May 3 00:38:55 2008 +++ ZendEngine2/zend_object_handlers.c Tue May 27 10:29:33 2008 @@ -17,7 +17,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: zend_object_handlers.c,v 1.135.2.6.2.22.2.15 2008/05/03 00:38:55 felipe Exp $ */ +/* $Id: zend_object_handlers.c,v 1.135.2.6.2.22.2.16 2008/05/27 10:29:33 mattwil Exp $ */ #include "zend.h" #include "zend_globals.h" @@ -654,7 +654,7 @@ ALLOC_ZVAL(method_args_ptr); INIT_PZVAL(method_args_ptr); - array_init(method_args_ptr); + array_init_size(method_args_ptr, ZEND_NUM_ARGS()); if (zend_copy_parameters_array(ZEND_NUM_ARGS(), method_args_ptr TSRMLS_CC) == FAILURE) { zval_dtor(method_args_ptr); @@ -853,7 +853,7 @@ ALLOC_ZVAL(method_args_ptr); INIT_PZVAL(method_args_ptr); - array_init(method_args_ptr); + array_init_size(method_args_ptr, ZEND_NUM_ARGS()); if (zend_copy_parameters_array(ZEND_NUM_ARGS(), method_args_ptr TSRMLS_CC) == FAILURE) { zval_dtor(method_args_ptr); http://cvs.php.net/viewvc.cgi/php-src/ext/standard/array.c?r1=1.308.2.21.2.37.2.30&r2=1.308.2.21.2.37.2.31&diff_format=u Index: php-src/ext/standard/array.c diff -u php-src/ext/standard/array.c:1.308.2.21.2.37.2.30 php-src/ext/standard/array.c:1.308.2.21.2.37.2.31 --- php-src/ext/standard/array.c:1.308.2.21.2.37.2.30 Sun May 18 15:42:08 2008 +++ php-src/ext/standard/array.c Tue May 27 10:29:33 2008 @@ -21,7 +21,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: array.c,v 1.308.2.21.2.37.2.30 2008/05/18 15:42:08 felipe Exp $ */ +/* $Id: array.c,v 1.308.2.21.2.37.2.31 2008/05/27 10:29:33 mattwil Exp $ */ #include "php.h" #include "php_ini.h" @@ -1501,7 +1501,14 @@ zend_rebuild_symbol_table(TSRMLS_C); } - array_init(return_value); + /* compact() is probably most used with a single array of var_names + or multiple string names, rather than a combination of both. + So quickly guess a minimum result size based on that */ + if (ZEND_NUM_ARGS() == 1 && Z_TYPE_PP(args[0]) == IS_ARRAY) { + array_init_size(return_value, zend_hash_num_elements(Z_ARRVAL_PP(args[0]))); + } else { + array_init_size(return_value, ZEND_NUM_ARGS()); + } for (i=0; i<ZEND_NUM_ARGS(); i++) { php_compact_var(EG(active_symbol_table), return_value, *args[i] TSRMLS_CC); @@ -1522,15 +1529,15 @@ return; } - num--; - if (num < 0) { + if (num < 1) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Number of elements must be positive"); RETURN_FALSE; } /* allocate an array for return */ - array_init(return_value); + array_init_size(return_value, num); + num--; zval_add_ref(&val); zend_hash_index_update(Z_ARRVAL_P(return_value), start_key, &val, sizeof(zval *), NULL); @@ -1553,7 +1560,7 @@ } /* Initialize return array */ - array_init(return_value); + array_init_size(return_value, zend_hash_num_elements(Z_ARRVAL_P(keys))); zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(keys), &pos); while (zend_hash_get_current_data_ex(Z_ARRVAL_P(keys), (void **)&entry, &pos) == SUCCESS) { @@ -1832,7 +1839,7 @@ /* Create and initialize output hash */ ALLOC_HASHTABLE(out_hash); - zend_hash_init(out_hash, 0, NULL, ZVAL_PTR_DTOR, 0); + zend_hash_init(out_hash, (length > 0 ? num_in - length : 0) + list_count, NULL, ZVAL_PTR_DTOR, 0); /* Start at the beginning of the input hash and copy entries to output hash until offset is reached */ for (pos = 0, p = in_hash->pListHead; pos < offset && p ; pos++, p = p->pListNext) { @@ -2070,19 +2077,23 @@ zval *array, /* Input array */ *repl_array, /* Replacement array */ ***repl = NULL; /* Replacement elements */ - HashTable *new_hash = NULL; /* Output array's hash */ + HashTable *new_hash = NULL, /* Output array's hash */ + **rem_hash = NULL; /* Removed elements' hash */ Bucket *p; /* Bucket used for traversing hash */ long i, offset, length, repl_num = 0; /* Number of replacement elements */ + int num_in; /* Number of elements in the input array */ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "al|lz/", &array, &offset, &length, &repl_array) == FAILURE) { return; } + num_in = zend_hash_num_elements(Z_ARRVAL_P(array)); + if (ZEND_NUM_ARGS() < 3) { - length = zend_hash_num_elements(Z_ARRVAL_P(array)); + length = num_in; } if (ZEND_NUM_ARGS() == 4) { @@ -2097,11 +2108,32 @@ } } - /* Initialize return value */ - array_init(return_value); + /* Don't create the array of removed elements if it's not going + * to be used; e.g. only removing and/or replacing elements */ + if (return_value_used) { + int size; + + /* Clamp the offset.. */ + if (offset > num_in) { + offset = num_in; + } else if (offset < 0 && (offset = (num_in + offset)) < 0) { + offset = 0; + } + + /* ..and the length */ + if (length < 0) { + size = num_in - offset + length; + } else if (((unsigned long) offset + (unsigned long) length) > (unsigned) num_in) { + size = num_in - offset; + } + + /* Initialize return value */ + array_init_size(return_value, size > 0 ? size : 0); + rem_hash = &Z_ARRVAL_P(return_value); + } /* Perform splice */ - new_hash = php_splice(Z_ARRVAL_P(array), offset, length, repl, repl_num, &Z_ARRVAL_P(return_value)); + new_hash = php_splice(Z_ARRVAL_P(array), offset, length, repl, repl_num, rem_hash); /* Replace input array's hashtable with the new one */ zend_hash_destroy(Z_ARRVAL_P(array)); @@ -2150,11 +2182,9 @@ length = Z_LVAL_PP(z_length); } - /* Initialize returned array */ - array_init(return_value); - /* Clamp the offset.. */ if (offset > num_in) { + array_init(return_value); return; } else if (offset < 0 && (offset = (num_in + offset)) < 0) { offset = 0; @@ -2167,7 +2197,10 @@ length = num_in - offset; } - if (length == 0) { + /* Initialize returned array */ + array_init_size(return_value, length > 0 ? length : 0); + + if (length <= 0) { return; } @@ -2349,7 +2382,11 @@ } /* Initialize return array */ - array_init(return_value); + if (search_value != NULL) { + array_init(return_value); + } else { + array_init_size(return_value, zend_hash_num_elements(Z_ARRVAL_P(input))); + } add_key = 1; /* Go through input array and add keys to the return array */ @@ -2395,7 +2432,7 @@ } /* Initialize return array */ - array_init(return_value); + array_init_size(return_value, zend_hash_num_elements(Z_ARRVAL_P(input))); /* Go through input array and add values to the return array */ zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(input), &pos); @@ -2472,7 +2509,7 @@ } /* Initialize return array */ - array_init(return_value); + array_init_size(return_value, zend_hash_num_elements(Z_ARRVAL_P(input))); zend_hash_internal_pointer_end_ex(Z_ARRVAL_P(input), &pos); while (zend_hash_get_current_data_ex(Z_ARRVAL_P(input), (void **)&entry, &pos) == SUCCESS) { @@ -2582,7 +2619,7 @@ } target_hash = HASH_OF(array); - array_init(return_value); + array_init_size(return_value, zend_hash_num_elements(target_hash)); zend_hash_internal_pointer_reset_ex(target_hash, &pos); while (zend_hash_get_current_data_ex(target_hash, (void **)&entry, &pos) == SUCCESS) { @@ -2627,7 +2664,7 @@ return; } - array_init(return_value); + array_init_size(return_value, zend_hash_num_elements(Z_ARRVAL_P(array))); zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(array), &pos); while (zend_hash_get_current_data_ex(Z_ARRVAL_P(array), (void **)&entry, &pos) == SUCCESS) { @@ -2678,7 +2715,7 @@ RETURN_FALSE; } - array_init(return_value); + array_init_size(return_value, zend_hash_num_elements(target_hash)); zend_hash_copy(Z_ARRVAL_P(return_value), target_hash, (copy_ctor_func_t) zval_add_ref, (void *)&tmp, sizeof(zval*)); if (target_hash->nNumOfElements <= 1) { /* nothing to do */ @@ -3879,7 +3916,7 @@ /* Make the return value an array only if we need to pass back more than one result. */ if (num_req > 1) { - array_init(return_value); + array_init_size(return_value, num_req); } /* We can't use zend_hash_index_find() because the array may have string keys or gaps. */ @@ -4193,7 +4230,7 @@ return; } - array_init(return_value); + array_init_size(return_value, maxlen); params = (zval ***)safe_emalloc(n_arrays, sizeof(zval **), 0); MAKE_STD_ZVAL(null); ZVAL_NULL(null); @@ -4209,7 +4246,7 @@ * entries from all arrays. */ if (!ZEND_FCI_INITIALIZED(fci)) { MAKE_STD_ZVAL(result); - array_init(result); + array_init_size(result, n_arrays); } for (i = 0; i < n_arrays; i++) { @@ -4315,7 +4352,7 @@ Split array into chunks */ PHP_FUNCTION(array_chunk) { - int argc = ZEND_NUM_ARGS(), key_type; + int argc = ZEND_NUM_ARGS(), key_type, num_in; long size, current = 0; char *str_key; uint str_key_len; @@ -4335,14 +4372,20 @@ return; } - array_init(return_value); + num_in = zend_hash_num_elements(Z_ARRVAL_P(input)); + + if (size > num_in) { + size = num_in > 0 ? num_in : 1; + } + + array_init_size(return_value, ((num_in - 1) / size) + 1); zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(input), &pos); while (zend_hash_get_current_data_ex(Z_ARRVAL_P(input), (void**)&entry, &pos) == SUCCESS) { /* If new chunk, create and initialize it. */ if (!chunk) { MAKE_STD_ZVAL(chunk); - array_init(chunk); + array_init_size(chunk, size); } /* Add entry to the chunk, preserving keys if necessary. */ @@ -4405,7 +4448,7 @@ RETURN_FALSE; } - array_init(return_value); + array_init_size(return_value, num_keys); zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(keys), &pos_keys); zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(values), &pos_values); http://cvs.php.net/viewvc.cgi/php-src/ext/standard/string.c?r1=1.445.2.14.2.69.2.21&r2=1.445.2.14.2.69.2.22&diff_format=u Index: php-src/ext/standard/string.c diff -u php-src/ext/standard/string.c:1.445.2.14.2.69.2.21 php-src/ext/standard/string.c:1.445.2.14.2.69.2.22 --- php-src/ext/standard/string.c:1.445.2.14.2.69.2.21 Fri May 9 12:59:16 2008 +++ php-src/ext/standard/string.c Tue May 27 10:29:33 2008 @@ -18,7 +18,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: string.c,v 1.445.2.14.2.69.2.21 2008/05/09 12:59:16 scottmac Exp $ */ +/* $Id: string.c,v 1.445.2.14.2.69.2.22 2008/05/27 10:29:33 mattwil Exp $ */ /* Synced with php 3.0 revision 1.193 1999-06-16 [ssb] */ @@ -40,8 +40,6 @@ # include <libintl.h> /* For LC_MESSAGES */ #endif -#include <math.h> - #include "scanf.h" #include "zend_API.h" #include "zend_execute.h" @@ -5178,14 +5176,14 @@ RETURN_FALSE; } - array_init(return_value); + array_init_size(return_value, ((str_len - 1) / split_length) + 1); if (split_length >= str_len) { add_next_index_stringl(return_value, str, str_len, 1); return; } - n_reg_segments = floor(str_len / split_length); + n_reg_segments = str_len / split_length; p = str; while (n_reg_segments-- > 0) { http://cvs.php.net/viewvc.cgi/php-src/ext/standard/var_unserializer.re?r1=1.52.2.2.2.6.2.4&r2=1.52.2.2.2.6.2.5&diff_format=u Index: php-src/ext/standard/var_unserializer.re diff -u php-src/ext/standard/var_unserializer.re:1.52.2.2.2.6.2.4 php-src/ext/standard/var_unserializer.re:1.52.2.2.2.6.2.5 --- php-src/ext/standard/var_unserializer.re:1.52.2.2.2.6.2.4 Wed Mar 19 03:05:34 2008 +++ php-src/ext/standard/var_unserializer.re Tue May 27 10:29:33 2008 @@ -16,7 +16,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: var_unserializer.re,v 1.52.2.2.2.6.2.4 2008/03/19 03:05:34 felipe Exp $ */ +/* $Id: var_unserializer.re,v 1.52.2.2.2.6.2.5 2008/05/27 10:29:33 mattwil Exp $ */ #include "php.h" #include "ext/standard/php_var.h" @@ -557,10 +557,8 @@ } INIT_PZVAL(*rval); - Z_TYPE_PP(rval) = IS_ARRAY; - ALLOC_HASHTABLE(Z_ARRVAL_PP(rval)); - zend_hash_init(Z_ARRVAL_PP(rval), elements + 1, NULL, ZVAL_PTR_DTOR, 0); + array_init_size(*rval, elements); if (!process_nested_data(UNSERIALIZE_PASSTHRU, Z_ARRVAL_PP(rval), elements)) { return 0;
« previous php.cvs (#50169) next »