Re: Why does count() make copies of arrays?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Mon, 2006-08-14 at 18:35 -0400, Adam Zey wrote:
>
> Further experimentation shows that the problem only occurs if the 
> variable being count'd is a static variable inside a function. Of 
> course, the original point still stands, static or no, count shouldn't 
> make a copy. Here is a sample script that I can confirm reproduces the 
> issue:
> 
> <?php
> 
> use_mem();
> 
> function use_mem()
> {
>          static $foo = "";
> 
>          for ( $x=0; $x <= 70000; $x++ )
>                  $foo[] = "BwaHA" . mt_rand(0, 1000000);
> 
>          echo memory_get_usage();
>          $numrows = count($foo);
> }
> 
> ?>
> 
> PHP's default memory limit is 8MB. This script creates an array that's 
> about 5.5MB. That part's fine. But it fails on that last line with the 
> count($foo).
> 
> I realize that this particular function doesn't need the variable to be 
> static, but it's just a demonstration. My actual script had a function 
> that needed some data to be read in from a file into an array. Rather 
> than reading it in in the main script and passing it to the function, or 
>   having the function read the data in every execution, I simply made 
> the variable static and had the function check if the variable was empty 
> to see if it was the first time the function had been called.
> 
> Sorry for not realizing that the static variable is the key to 
> reproducing this issue. Does the script that I've pasted here give you 
> any better luck in reproducing?

Yep looks like a bug or a design choice based on static variables. I'm
not sure what the reasoning is, but it sounds like static vars currently
take a huge performance hit when containing large chunks of memory :/ I
just tested your code using is_array() and got the same failure.

Cheers,
Rob.
-- 
.------------------------------------------------------------.
| InterJinn Application Framework - http://www.interjinn.com |
:------------------------------------------------------------:
| An application and templating framework for PHP. Boasting  |
| a powerful, scalable system for accessing system services  |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for       |
| creating re-usable components quickly and easily.          |
`------------------------------------------------------------'

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux