Re: New RFC

From: Date: Tue, 02 Aug 2016 10:10:02 +0000
Subject: Re: New RFC
References: 1 2 3 4  Groups: php.internals 
Request: Send a blank email to internals+get-94801@lists.php.net to get a copy of this message
On 02/08/2016 09:56, Christoph Becker wrote:
For the record: there has been the "Array Of" RFC[1], but that has been declined. [1] <https://wiki.php.net/rfc/arrayof>
If I remember rightly, the biggest problem raised with typed arrays is that type constraints in PHP are checked dynamically every time a function is called, so the cost of iterating the array to test its type can be rather costly: function foo(Thing[] $things) { } $data = array_fill(0, 10000000, new Thing); foo($data); // performs 10000000 instanceof checks foo($data); // performs another 10000000 instanceof checks $data[] = new NotAThing; foo($data); // will throw error only after 10000001 instanceof checks # TOTAL: 30000001 instanceof checks This is not the same with generics, because the container itself performs the check, as items are added: function foo(TypedArray<Thing> $things) { } $data = new TypedArray<Thing>; foreach ( array_fill(0, 10000000, new Thing) as $thing ) {
    $data->add($thing); // 10000000 instanceof checks here
} foo($data); // performs one instanceof check on the container foo($data); // performs one instanceof check on the container $data->add(new NotAThing); // performs 1 instanceof check and errors # TOTAL: 10000003 instanceof checks In this trivial example, you could replace the foreach loop with something like $data->fill(0, 10000000, new Thing) and reduce the whole thing to 4 instanceof checks. There is no equivalent shortcut for the Thing[] version. This is a fundamental problem with PHP's type constraints - because they are not attached to the value itself, they act as assertions that must be executed repeatedly. The more complex we make them, and the more places they're added, the more this becomes a limiting factor. Regards, -- Rowan Collins [IMSoP]

« previous php.internals (#94801) next »