Re: com php-src: Optimized array_reverse: ext/standard/array.c

From: Date: Mon, 01 Aug 2016 12:31:06 +0000
Subject: Re: com php-src: Optimized array_reverse: ext/standard/array.c
References: 1 2  Groups: php.cvs 
Request: Send a blank email to php-cvs+get-93509@lists.php.net to get a copy of this message
On Mon, Aug 1, 2016 at 2:20 PM, Nikita Popov <nikita.ppv@gmail.com> wrote: > On Mon, Aug 1, 2016 at 2:11 PM, Xinchen Hui <laruence@php.net> wrote: > >> Commit: 4d9195927a4c8c8adce84b57125e5b8d52461358 >> Author: Xinchen Hui <laruence@gmail.com> Mon, 1 Aug 2016 >> 20:11:08 +0800 >> Parents: 318102fc609295df70a7cc7dcfe2bae854ca32df >> Branches: PHP-7.1 master >> >> Link: >> >> http://git.php.net/?p=php-src.git;a=commitdiff;h=4d9195927a4c8c8adce84b57125e5b8d52461358 >> >> Log: >> Optimized array_reverse >> >> Changed paths: >> M ext/standard/array.c >> >> >> Diff: >> diff --git a/ext/standard/array.c b/ext/standard/array.c >> index 38e275a..2680ac6 100644 >> --- a/ext/standard/array.c >> +++ b/ext/standard/array.c >> @@ -3000,7 +3000,7 @@ PHP_FUNCTION(array_slice) >> >> /* Start at the beginning and go until we hit offset */ >> pos = 0; >> - if (!preserve_keys && (Z_ARRVAL_P(input)->u.flags & >> HASH_FLAG_PACKED)) { >> + if ((Z_ARRVAL_P(input)->u.flags & HASH_FLAG_PACKED) && >> !preserve_keys) { >> zend_hash_real_init(Z_ARRVAL_P(return_value), 1); >> ZEND_HASH_FILL_PACKED(Z_ARRVAL_P(return_value)) { >> ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(input), entry) { >> @@ -3012,7 +3012,7 @@ PHP_FUNCTION(array_slice) >> break; >> } >> ZEND_HASH_FILL_ADD(entry); >> - zval_add_ref(entry); >> + Z_TRY_ADDREF_P(entry); > > } ZEND_HASH_FOREACH_END(); >> } ZEND_HASH_FILL_END(); >> } else { >> @@ -3034,7 +3034,7 @@ PHP_FUNCTION(array_slice) >> entry = >> zend_hash_next_index_insert_new(Z_ARRVAL_P(return_value), entry); >> } >> } >> - zval_add_ref(entry); >> + Z_TRY_ADDREF_P(entry); >> } ZEND_HASH_FOREACH_END(); >> } >> } >> @@ -3653,20 +3653,28 @@ PHP_FUNCTION(array_reverse) >> >> /* Initialize return array */ >> array_init_size(return_value, >> zend_hash_num_elements(Z_ARRVAL_P(input))); >> - >> - ZEND_HASH_REVERSE_FOREACH_KEY_VAL(Z_ARRVAL_P(input), num_key, >> string_key, entry) { >> - if (string_key) { >> - entry = >> zend_hash_add_new(Z_ARRVAL_P(return_value), string_key, entry); >> - } else { >> - if (preserve_keys) { >> - entry = >> zend_hash_index_add_new(Z_ARRVAL_P(return_value), num_key, entry); >> + if ((Z_ARRVAL_P(input)->u.flags & HASH_FLAG_PACKED) && >> !preserve_keys) { >> + zend_hash_real_init(Z_ARRVAL_P(return_value), 1); >> + ZEND_HASH_FILL_PACKED(Z_ARRVAL_P(return_value)) { >> + ZEND_HASH_REVERSE_FOREACH_VAL(Z_ARRVAL_P(input), >> entry) { >> + ZEND_HASH_FILL_ADD(entry); >> + Z_TRY_ADDREF_P(entry); >> + } ZEND_HASH_FOREACH_END(); >> + } ZEND_HASH_FILL_END(); >> + } else { >> + ZEND_HASH_REVERSE_FOREACH_KEY_VAL(Z_ARRVAL_P(input), >> num_key, string_key, entry) { >> + if (string_key) { >> + entry = >> zend_hash_add_new(Z_ARRVAL_P(return_value), string_key, entry); >> } else { >> - entry = >> zend_hash_next_index_insert_new(Z_ARRVAL_P(return_value), entry); >> + if (preserve_keys) { >> + entry = >> zend_hash_index_add_new(Z_ARRVAL_P(return_value), num_key, entry); >> + } else { >> + entry = >> zend_hash_next_index_insert_new(Z_ARRVAL_P(return_value), entry); >> + } >> } >> - } >> - >> - zval_add_ref(entry); >> - } ZEND_HASH_FOREACH_END(); >> + Z_TRY_ADDREF_P(entry); >> + } ZEND_HASH_FOREACH_END(); >> + } >> } >> /* }}} */ >> > > The replacement of zval_add_ref() with Z_TRY_ADDREF() will probably break > handling of references. rc=1 references must be unwrapped, otherwise there > will be a reference between the two array elements. > > Nikita > Example: <?php $i = 42; $a = [&$i]; unset($a); $b = array_reverse($a); $a[0] = 24; var_dump($b[0]); // should be 42, but is 24 Nikita

« previous php.cvs (#93509) next »