On 22/06/2024 21:15, Robert Landers wrote:
To me, that sounds kinda silly. PHP does reference counting and while there is an overhead, it doesn't prevent us from using it...
The reference count is a single pre-allocated integer on the C struct holding the value; and even then, a lot of effort was put in during the "PHP-NG" project (which landed in PHP 7.0) to avoid reference counting values that didn't need it. (Nikita wrote a great post explaining the changes here: https://www.npopov.com/2015/05/05/Internal-value-representation-in-PHP-7-part-1.html)
The problem with type counters is not just that there's more than one per array, it's that there's a *variable* number, so you can't preallocate memory for them, they need to reference some extra resizable hash-table. That's the "indirection" that Arnaud is talking about.
I've thought in the past about caching type checks that had passed, so that a series of array<int> checks could be made "for the price of one" if the array didn't change between. That might at least mean the additional table of types would only need accessing on type checks, not every write, but a lot of checks would still need to iterate the whole array.
Having a structure that only allows one type could potentially be much more efficient, because you only need to allocate space for the type identifier, and check against it on write.
-- Rowan Tommins [IMSoP]