Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:71175 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 555 invoked from network); 16 Jan 2014 10:49:23 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 16 Jan 2014 10:49:23 -0000 X-Host-Fingerprint: 90.29.78.18 ALyon-654-1-198-18.w90-29.abo.wanadoo.fr Received: from [90.29.78.18] ([90.29.78.18:13943] helo=localhost.localdomain) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 0B/B1-24763-139B7D25 for ; Thu, 16 Jan 2014 05:49:22 -0500 Message-ID: <0B.B1.24763.139B7D25@pb1.pair.com> To: internals@lists.php.net Date: Thu, 16 Jan 2014 11:49:17 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.2.0 MIME-Version: 1.0 References: <001301cf1227$6d082ab0$47188010$@tutteli.ch> <002d01cf1234$01e6dc60$05b49520$@tutteli.ch> <004c01cf123d$35730870$a0591950$@tutteli.ch> <52D71748.1090402@googlemail.com> <52D71FAE.8030002@ajf.me> <005001cf124f$3a40df00$aec29d00$@tutteli.ch> <20140116110127.202079vzjsj76n7b@webmail.tutteli.ch> In-Reply-To: <20140116110127.202079vzjsj76n7b@webmail.tutteli.ch> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Posted-By: 90.29.78.18 Subject: Re: [PHP-DEV] Introducing "Array Of" RFC From: matthieu@mnapoli.fr (Matthieu Napoli) Le 16/01/2014 11:01, php@tutteli.ch a écrit : >> + 1 for the RFC >> >> But if i'm not completely wrong, there is performance decrease instead of >> increase in your tests? >> https://gist.github.com/krakjoe/8444591 >> >> [joe@fiji php-src]$ sapi/cli/php isTest.php 10000 >> objects: 10000 >> arrayof: 0.00645113 >> instanceof: 0.00560713 > > I gave the RFC a few more thoughts and came to the conclusion that it > does not fit to PHP (due to the performance penalties involved). The performance penalty is the same as if you were type-checking yourself. And on the gist linked, we can see that the results vary a lot (not enough iterations probably), it's easy to take only the worst one. Here, I'll take the one where the numbers are inversed to "prove" my point: [joe@fiji php-src]$ sapi/cli/php isTest.php 1000 objects: 1000 arrayof: 0.00080991 instanceof: 0.00086308 > The syntax merely hide an ugly design. If you would write the same in > normal PHP then probably everyone would agree that it is bad design. How is type checking a bad design? Between: function (array $foos) { foreach ($foos as $foo) { if (! $foo instanceof Foo) { throw new InvalidArgumentException("..."); } $foo->bar(); } } and: function (array $foos) { foreach ($foos as $foo) { $foo->bar(); // BOOM, fatal error if not correct object given } } which one is worse? It's the same problem than this: function ($foo) { $foo->bar(); // fatal error if not correct object given } and this: function (Foo $foo) { $foo->bar(); } > Following an example (doesn't really make sense but outlines the bad > design) > > function foo(Foo[] $foos){ > if(count($foos) > 0){ > $a = array_pop($foos); > //do something > foo($foos); > } > } > > Is equivalent to > > function foo(array $arr){ > foreach($arr as $v){ > if(!($v instanceof Foo)){ > //trigger_error > } > } > if(count($foos) > 0){ > $a = array_pop($foos); > //do something > foo($foos); > } > } > > The check adds runtime complexity to the function for each call which is > obviously not necessary (once would be good enough). And I doubt that > users would actually make sure that the check is only performed once and > even if you would argue that one should write it as follows: > > function bar(Foo[] $foos){ > foo($foos); > } > function foo(array $foos){ > if(count($foos) > 0){ > $a = array_pop($foos); > //do something > foo($foos); > } > } > > Then it is still bad design compared to a collection which actually does > the check during the addition of elements. > Although, I like the effort to make PHP somewhat more type safe I am -1 > for this RFC, the implementation respectively. If we want typed arrays > then the array needs to carry the type to be efficient. > Matthieu