On 20/01/2020 00:53, Mike Schinkel wrote:
One approach mentioned by Andrea Faulds was to extend the hashtable (ref: your 
article[1]) and count types as assigned just like we currently count references.  So a 
10,240 element array of ints could have an internal tracker showing that the array 
contains type(s): ['int' => 10240].   Append a string value to the array and then the 
types would be ['int' => 10240, 'string' => 1].


This would work really well for simple types like 'int' and 'string', but loses its advantage fast with things like interfaces and pseudo-types. For instance, if you have an array with objects of 20 different classes, and need to check it against a constraint of SomeInterface[], you still have to test all 20 classes to see if they implement that interface.

The overhead is also rather high, because you have to allocate memory for this list on every array, and keep it up to date on every write, even if it's never used.

I've had a similar idea in the past, but rather than trying to list the types in advance, just cache them after passing (or failing) a type check, so more like [ 'SomeInterface[]' => true, 'SomeOtherInterface[]' => false ]. Even if you just wiped the cache completely on every write, I think that would give a decent boost, because there will often be cases where a value is passed through a series of related functions all expecting the same type.

The worst case pseudotype is probably "callable[]", because it's actually context-dependent (e.g. [$object, 'privateMethod] is only "callable" inside the same class as $object) so can't be pre-calculated or cached. That would be problematic even with full generics - logically, List<callable> would check each member was callable when it was added to the list, but it might turn out not to be callable when it was accessed later.

Regards,

--
Rowan Tommins (né Collins)
[IMSoP]

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to