Re: INDRECT in arrays causes count() to become unpredictable
| From: | Xinchen Hui | Date: | Sun, 22 Nov 2015 05:01:58 +0000 |
| Subject: | Re: INDRECT in arrays causes count() to become unpredictable | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-89290@lists.php.net to get a copy of this message | ||
Hey:
On Sun, Nov 22, 2015 at 6:47 AM, Anthony Ferrara <ircmaxell@gmail.com>
wrote:
> All,
>
> It appears that in our efforts to optimize PHP 7 we've introduced an
> inconsistency into array handling. This is demonstrated by this
> script: https://3v4l.org/hVcAB
>
> $a = 1;
> unset($a);
> var_dump(count($GLOBALS), $GLOBALS);
>
> The result is that the count doesn't match the contents of the array.
>
> It also affects objects casted to arrays in certain cases.
>
> Here, the array has 1 element, but should have 0. Trying to access the
> "1" element "a" results in a notice.
>
> This means that the count is incorrect. This is due to the fact that
> the "a" element follows an IS_INDIRECT type pointing to the other
> symbol table.
>
> A potential solution would be to make count() an O(n) operation in the
> case that the hashtable is a symbol table or if it has INDIRECT
> elements. Meaning that we'd compute the real count at runtime.
>
> This obviously has performance ramifications, but we shouldn't
> sacrifice consistency and behavior for performance IMHO. Especially
> when it comes to core data structures.
>
> I think this is significant enough to be a blocker to gold and that we
> should fix it prior to release.
>
I don't think so, it's only affects symbol tables. means, it won't affects
normal arrays. like:
<?php
$array = range(1, 10);
unset($array[2]);
var_dump(count($array));
$array = array("foo" => 1, "bar" => 2, "dummy" => 3);
unset($array["bar"]);
var_dump(count($array));
?>
anyway, I've fixed this here:
https://github.com/php/php-src/commit/eada2aa91add2de69bc5783b840a21243a7b0678
and, we maybe move this new functions into zend_hash(as a ZEND_API),
anyway I am not sure whether it's okey for PHP-7.0.
will discuss with welting and Dmitry on Monday.
thanks
>
> Thoughts?
> Anthony
>
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: http://www.php.net/unsub.php
>
--
Xinchen Hui
@Laruence
http://www.laruence.com/