Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:104243 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 61853 invoked from network); 6 Feb 2019 16:42:56 -0000 Received: from unknown (HELO mail-it1-f169.google.com) (209.85.166.169) by pb1.pair.com with SMTP; 6 Feb 2019 16:42:56 -0000 Received: by mail-it1-f169.google.com with SMTP id b5so6312893iti.2 for ; Wed, 06 Feb 2019 05:24:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=iiUq8okLo+mDc++tnWdZxGl0eCexaVSEbidusGuiTCc=; b=RW7dkjsJwQ5m1qFJ//NbgoZ9tpKEunEmGtDnOfwIqMp1ZbXsrlXkdXz3ZXXJeGI6xU 7E/PRcCQRnv3kaJMIrqihKzd8NgH/x9FnDVOhTXGk4w2lVqTyytAvmnW3lU0HaR2nN5O r5tVS9VGB3Q3idnlmQkXItuBuDNFlGaOGyeuxNss9EvPo+0DkgOCWLMp2g1TwGvU5F4P Rawr3aRpSjiGCx3ePmPLnV/CAyxoV4Hl4PxdsnObWKNrtitCMTD/WgQK+qVlMzRzX2q2 F2pEG93RkeyN3FkHxt5b5IeApXgWq00w6uEeGZtD+GcAnTQJ1hE0bzR1CLe7zURpGU4K TgiA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=iiUq8okLo+mDc++tnWdZxGl0eCexaVSEbidusGuiTCc=; b=tzLJcYJpgQU+I4D0xabfurjX/VuWyRri53oBHKyA9o4ud8HE/AW1iSWFYCM9yrj2QO tQMRiENhSHBn1c52CiuNZKg++0RI9Me+cmnVz+T/C1iqPFazafxVLs7iGR17pitG9MO9 ZiXPr6sES6ZE+AcoGYugHotZSa37mM5sWc+aVgbYVq+pwIY8gMWGZ4tBUf+AO3Z3jmVL xDkou3EapbEiiIKLLHgV70GvOZAe74Ijg77HtkV3ZZDfePSwyXOwoRXZ46Xm9WhTwvck lJbqbSY1jr24B+6YEPr40S8xbB/YP/hWSYWn5a59CgEB/6hiyPWWw8JoSfWOGI8CZmBR GbNA== X-Gm-Message-State: AHQUAuYu35iRPuSyLpHxcQM7YpwCFmh+sqFUG+5HgMFT5FzTu1TYk8qS +AKxgY+9SYgE3SWndGgNwzB/rQZhPNx6CPKGTL0= X-Google-Smtp-Source: AHgI3IZDLFgdFhvKqiAlSjVlYTeLykrB7DFWgyc6n1kKMm1M57HqKfYBVOvnFUTZwQg0nRvaPQo+WLM2/JdBqTPrscs= X-Received: by 2002:a02:9089:: with SMTP id x9mr5105357jaf.72.1549459459420; Wed, 06 Feb 2019 05:24:19 -0800 (PST) MIME-Version: 1.0 References: <595b374c-bc4f-9b8a-0013-6485abbfb477@php.net> In-Reply-To: Date: Wed, 6 Feb 2019 13:24:07 +0000 Message-ID: To: Benjamin Morel Cc: Nikita Popov , PHP Internals List Content-Type: multipart/alternative; boundary="000000000000ac3765058139a454" Subject: Re: [PHP-DEV] Re: [RFC] [VOTE] Typed properties v2 From: ocramius@gmail.com (Marco Pivetta) --000000000000ac3765058139a454 Content-Type: text/plain; charset="UTF-8" Heya, Reflection is not slow: it is only slow if you instantiate it continuously. On Wed, 6 Feb 2019, 12:38 Benjamin Morel Hi Nikita, > > While playing with typed properties, something that annoys me a lot, in the > context of a data mapper, is not to be able to use isset() to check whether > an object's property is initialized. The issue was already there with > properties that have been explicitly unset(), but will be exacerbated by > typed properties, which will be unset *by default*. > > Sure, we have ReflectionProperty::isInitialized(), but reflection is slow > compared to direct property access, when reading a lot of objects. > > For example, check these 3 ways of reading initialized public/protected > object properties by name, along with their timings for 100,000 iterations > (benchmark here > < > https://gist.github.com/BenMorel/9a920538862e4df0d7041f8812f069e5#file-reflection-vs-array-cast-benchmark-php > > > ): > > Using reflection: (890 ms, down to 384 ms using pre-instantiated > ReflectionProperty objects) > > $r = new \ReflectionClass($class); > > foreach ($props as $prop) { > > $p = $r->getProperty($prop); > > $p->setAccessible(true); > > if ($p->isInitialized($object)) { > > $values[$p->getName()] = $p->getValue($object); > > } > > } > > > Using array cast: (193 ms) > > foreach ((array) $object as $key => $value) { > > // Remove the "\0*\0" in front of protected properties > > $pos = strrpos($key, "\0"); > > if ($pos !== false) { > > $key = substr($key, $pos + 1); > > } > > $values[$key] = $value; > > } > > > Using a bound closure and isset(): (145 ms) > > > > (function() use ($props, & $values) { > > foreach ($props as $prop) { > > if (isset($this->{$prop})) { // skips NULL values as well :-( > > $values[$prop] = $this->{$prop}; > > } > > } > > })->bindTo($object, $object)(); > > > Unfortunately, while the last approach is the fastest, and IMO the cleanest > one, it is currently unusable because there is no way to differentiate > between an uninitialized property and a NULL property. > > Would it be possible to introduce another isset() operator, that would > return true for NULL values? > Note that this would also be useful when checking if an array key exists, > instead of having to switch from isset() to array_key_exists() when the > array may contain NULL values. > > Ben > > On Fri, 11 Jan 2019 at 17:38, Nikita Popov wrote: > > > On Mon, Oct 22, 2018 at 1:58 PM Sebastian Bergmann > > wrote: > > > > > Am 26.09.2018 um 15:46 schrieb Nikita Popov: > > > > I'm pleased to announce that the typed properties RFC has been > accepted > > > > with 70 votes in favor and one vote against. We will work to finalize > > and > > > > merge the implementation in the next few days. > > > > > > Any update on when this will be merged? Thanks! > > > > > > > Sorry for the long delay here. I've spent the last week fixing the > > remaining issues and cleaning up the patch, and... > > > > ...Typed properties are now merged into master! [1] > > > > It would be great if people could start experimenting with typed > properties > > (e.g. by migrating over existing @var annotations and seeing what > happens). > > The earlier we find issues in the implementation or semantics, the > better. > > > > Thanks to everyone who worked on this RFC and participated in > discussions. > > Special thanks to Bob Weinand who did most of the initial implementation > > work on this RFC. > > > > Regards, > > Nikita > > > > [1]: > > > > > https://github.com/php/php-src/commit/e219ec144ef6682b71e135fd18654ee1bb4676b4 > > > --000000000000ac3765058139a454--