[prev in list] [next in list] [prev in thread] [next in thread] 

List:       php-internals
Subject:    Re: [PHP-DEV] Optimizing array_reverse for packed hash tables (when preserve_keys=false)
From:       Xinchen Hui <xinchen.h () zend ! com>
Date:       2016-07-28 3:38:56
Message-ID: CABBJUpe+FiQaFo3N3u0ra6phqsPVJSB75Bv7k0Fo=VouRGJ-1Q () mail ! gmail ! com
[Download RAW message or body]


Hey:

On Wed, Jul 27, 2016 at 10:42 PM, Benjamin Coutu <ben.coutu@zeyos.com>
wrote:

> Hello Xinchen,
>
> Analogue to the array_slice implementation, the array_reverse function
> could also be optimized for packed hash tables when preserve_keys is false
> (most common).
> The following patch should do just that:
>
> if (!preserve_keys && (Z_ARRVAL_P(input)->u.flags & HASH_FLAG_PACKED)) {
>         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);
>                         zval_add_ref(entry);
>                 } ZEND_HASH_FOREACH_END();
>         } ZEND_HASH_FILL_END();
> }
>
> Please let me know your thoughts.
>
I think it's a good idea,

one suggestion is  you should check HASH_FLAG_PACKED first, as
!perserve_keys is most common.

and maybe you should use Z_TRY_ADDREF instead of zval_add_ref, check
REFCOUNTED in place, to save one function call if it's not TYPE_REFCOUNTED.

btw, do you want to make a PR yourself, or let me to commit your idea?

thanks

>
> Thanks,
>
> Ben
>
> --
>
> Bejamin Coutu
> ben.coutu@zeyos.com
>
> ZeyOS, Inc.
> http://www.zeyos.com
>
>


-- 
Xinchen Hui
@Laruence
http://www.laruence.com/


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic