com php-src: Optmized array_rand: ext/standard/array.c
| From: | Xinchen Hui | Date: | Wed, 03 Aug 2016 04:06:04 +0000 |
| Subject: | com php-src: Optmized array_rand: ext/standard/array.c | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-93544@lists.php.net to get a copy of this message | ||
Commit: be00b4ed13819b464f77c6dc6f72734f34e5bcc6
Author: Xinchen Hui <laruence@gmail.com> Wed, 3 Aug 2016 12:06:04 +0800
Parents: 501d24f81bc94343e90fc8d0003aed45ebce52b9
Branches: PHP-7.1
Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=be00b4ed13819b464f77c6dc6f72734f34e5bcc6
Log:
Optmized array_rand
Changed paths:
M ext/standard/array.c
Diff:
diff --git a/ext/standard/array.c b/ext/standard/array.c
index e76eee3..d07e8c6 100644
--- a/ext/standard/array.c
+++ b/ext/standard/array.c
@@ -5177,19 +5177,25 @@ PHP_FUNCTION(array_rand)
i--;
}
}
+ /* i = 0; */
- /* We can't use zend_hash_index_find() because the array may have string keys or gaps. */
- i = 0;
- ZEND_HASH_FOREACH_KEY(Z_ARRVAL_P(input), num_key, string_key) {
- if (zend_bitset_in(bitset, i) ^ negative_bitset) {
- if (string_key) {
- add_next_index_str(return_value, zend_string_copy(string_key));
- } else {
- add_next_index_long(return_value, num_key);
+ zend_hash_real_init(Z_ARRVAL_P(return_value), 1);
+ ZEND_HASH_FILL_PACKED(Z_ARRVAL_P(return_value)) {
+ zval zv;
+ /* We can't use zend_hash_index_find()
+ * because the array may have string keys or gaps. */
+ ZEND_HASH_FOREACH_KEY(Z_ARRVAL_P(input), num_key, string_key) {
+ if (zend_bitset_in(bitset, i) ^ negative_bitset) {
+ if (string_key) {
+ ZVAL_STR_COPY(&zv, string_key);
+ } else {
+ ZVAL_LONG(&zv, num_key);
+ }
+ ZEND_HASH_FILL_ADD(&zv);
}
- }
- i++;
- } ZEND_HASH_FOREACH_END();
+ i++;
+ } ZEND_HASH_FOREACH_END();
+ } ZEND_HASH_FILL_END();
free_alloca(bitset, use_heap);
}