Index: Zend/zend_API.c =================================================================== RCS file: /repository/ZendEngine2/zend_API.c,v retrieving revision 1.471 diff -u -r1.471 zend_API.c --- Zend/zend_API.c 12 May 2008 07:13:47 -0000 1.471 +++ Zend/zend_API.c 12 May 2008 14:32:11 -0000 @@ -1197,11 +1197,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_u_hash_init(Z_ARRVAL_P(arg), 0, NULL, ZVAL_PTR_DTOR, 0, 0); + zend_u_hash_init(Z_ARRVAL_P(arg), size, NULL, ZVAL_PTR_DTOR, 0, 0); Z_TYPE_P(arg) = IS_ARRAY; return SUCCESS; } Index: Zend/zend_API.h =================================================================== RCS file: /repository/ZendEngine2/zend_API.h,v retrieving revision 1.300 diff -u -r1.300 zend_API.h --- Zend/zend_API.h 12 May 2008 07:13:47 -0000 1.300 +++ Zend/zend_API.h 12 May 2008 14:32:12 -0000 @@ -354,11 +354,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); Index: Zend/zend_builtin_functions.c =================================================================== RCS file: /repository/ZendEngine2/zend_builtin_functions.c,v retrieving revision 1.371 diff -u -r1.371 zend_builtin_functions.c --- Zend/zend_builtin_functions.c 12 May 2008 07:13:47 -0000 1.371 +++ Zend/zend_builtin_functions.c 23 May 2008 13:20:00 -0000 @@ -250,7 +250,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= 0) { Index: Zend/zend_object_handlers.c =================================================================== RCS file: /repository/ZendEngine2/zend_object_handlers.c,v retrieving revision 1.200 diff -u -r1.200 zend_object_handlers.c --- Zend/zend_object_handlers.c 3 May 2008 00:36:54 -0000 1.200 +++ Zend/zend_object_handlers.c 12 May 2008 14:32:13 -0000 @@ -662,7 +662,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); @@ -867,7 +867,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); Index: ext/standard/array.c =================================================================== RCS file: /repository/php-src/ext/standard/array.c,v retrieving revision 1.450 diff -u -r1.450 array.c --- ext/standard/array.c 18 May 2008 15:52:42 -0000 1.450 +++ ext/standard/array.c 23 May 2008 12:30:00 -0000 @@ -1625,7 +1625,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 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) { @@ -2257,19 +2264,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) { @@ -2284,11 +2295,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)); @@ -2329,7 +2361,7 @@ /* Get number of entries in the input hash */ num_in = zend_hash_num_elements(Z_ARRVAL_P(input)); - /* We want all entries from offset to the end if length is not passed or length is null */ + /* We want all entries from offset to the end if length is not passed or is null */ if (ZEND_NUM_ARGS() < 3 || Z_TYPE_PP(z_length) == IS_NULL) { length = num_in; } else { @@ -2337,11 +2369,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; @@ -2354,7 +2384,10 @@ length = num_in - offset; } - if (length == 0) { + /* Initialize returned array */ + array_init_size(return_value, length > 0 ? length : 0); + + if (length <= 0) { return; } @@ -2549,7 +2582,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 */ @@ -2599,7 +2636,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); @@ -2676,7 +2713,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) { @@ -2793,7 +2830,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) { @@ -2842,7 +2879,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) { @@ -2902,7 +2939,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 */ @@ -4101,7 +4138,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. */ @@ -4426,7 +4463,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); @@ -4442,7 +4479,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++) { @@ -4558,7 +4595,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; zstr str_key; uint str_key_len; @@ -4578,14 +4615,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. */ @@ -4655,7 +4698,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); Index: ext/standard/string.c =================================================================== RCS file: /repository/php-src/ext/standard/string.c,v retrieving revision 1.673 diff -u -r1.673 string.c --- ext/standard/string.c 9 May 2008 12:58:50 -0000 1.673 +++ ext/standard/string.c 12 May 2008 14:28:08 -0000 @@ -40,8 +40,6 @@ # include /* For LC_MESSAGES */ #endif -#include - #include "scanf.h" #include "zend_API.h" #include "zend_execute.h" @@ -7865,14 +7863,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_zstrl(return_value, str_type, str, str_len, 1); return; } - n_reg_segments = floor(str_len / split_length); + n_reg_segments = str_len / split_length; p = str.s; if (str_type == IS_UNICODE) { charsize = 2; Index: ext/standard/var_unserializer.re =================================================================== RCS file: /repository/php-src/ext/standard/var_unserializer.re,v retrieving revision 1.73 diff -u -r1.73 var_unserializer.re --- ext/standard/var_unserializer.re 19 Mar 2008 03:12:39 -0000 1.73 +++ ext/standard/var_unserializer.re 24 Apr 2008 09:35:00 -0000 @@ -651,10 +651,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;