Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:116976 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 62412 invoked from network); 3 Feb 2022 01:59:31 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 3 Feb 2022 01:59:31 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 51E6F1804DF for ; Wed, 2 Feb 2022 19:14:08 -0800 (PST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-1.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FORGED_HOTMAIL_RCVD2, FREEMAIL_ENVFROM_END_DIGIT,FREEMAIL_FROM,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS8075 40.80.0.0/12 X-Spam-Virus: No X-Envelope-From: Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10olkn2030.outbound.protection.outlook.com [40.92.41.30]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Wed, 2 Feb 2022 19:14:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=LB0Mvd1pBAEgpS4HS62Ir0G2kBcRvFe5mASPs5OczB60nNFyZaF61oOjCV0LX+QSWXGyhfn1DSRuZI+IqxvjUtpKwmB164GueDBBhWA+qWEvoctMPpy7C4yTmmhS4j0/l8Yn7mhUxnKqIcCg+0ZdpyzYt+6G7z4SCm1SRpys9h9gaRlG0RE1mEPQ0VJTIJzCECK/WpwaIvjwdR3g7QGI9aXBlfbIl99ZD+Omaeaof4q/wfdUAgSZOy2akOu4yIyCt9MjaZ09kd0+BhwX8xP7wU3v8I1KM2u/fwBp8HPDhYF1b/kd8hWEHwXTqIE+9yZZq+VTS53uOITnuLhBRldhHA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=DOH8Jq0hqtDSw5U2mI4SmKW+NRtyt853tIWsDrXvRQU=; b=LcI3iPVu3yBx0NHNaC6+2cZJjhQ5jH4QxyqmUnobErCvkQXm9iB7w6WL7erR00bs41QgviRgYYujuKI9bY5iYK3o1v+4QDFNAFSmagu7dWc/j6F4+2yLlmzN3teS52llKZZjBl1kukoyVotOKPES8P72fq+IV31eV6m+4NvRQablxC9WQjr1+/kr3xDi+aBDsrg0HTBgqU7aDBpnaK5+KT0IEPSviliciVzffeP+7cNon1ECo2OCieLV+S+gMUz0t70yMFHYDnuV9zKHaA6K8M+qKouGrYCcX+3Fwd9Q/n/rRUIRBMV7WLVz9ip2Bo7OCBocckl77tF5rN8tU8+bqQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=hotmail.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=DOH8Jq0hqtDSw5U2mI4SmKW+NRtyt853tIWsDrXvRQU=; b=b9hUjuns5Ir4YBDpK9zm4GMmo5V5fG2UNJE/kVUm8IabVR9BVqXllodAebSBXg4ygiLBklEifXtDHtkkB4kOPEjxTbQqot79+ymewdeyFmwroSrOl4taD7mAAc3gOSV4GQgpUiUzO23AQn/s62V6ddgbF2F011LOLCCkYhUwQRJ+47oKAkUiWeseUgHFwAgQKs7JQv6q57JAi9NhqznmSFmqj/5VyMsl1Xv6cgAlsDENyFZha5ApO50O9vf0QUyIaE2RAE6Kw4lKvqSZ7AvW8I11OREhycfhV0yBFc17WnpuySnlsWSRUQhCr+CO5jroH7f9q8l5rKYOP+QqENJyVQ== Received: from CH2PR14MB4151.namprd14.prod.outlook.com (2603:10b6:610:7b::22) by MN2PR14MB2845.namprd14.prod.outlook.com (2603:10b6:208:c9::33) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4930.19; Thu, 3 Feb 2022 03:14:05 +0000 Received: from CH2PR14MB4151.namprd14.prod.outlook.com ([fe80::99e7:5900:3bd3:6fa6]) by CH2PR14MB4151.namprd14.prod.outlook.com ([fe80::99e7:5900:3bd3:6fa6%7]) with mapi id 15.20.4951.012; Thu, 3 Feb 2022 03:14:05 +0000 To: php internals Thread-Topic: [PHP-DEV] Re: Adding `final class Deque` to PHP Thread-Index: AQHXraUxaK1dZ0niuECOJlLUpHNcpax/lZKygAAgIQCAAXQbEIAAIukAgACdooY= Date: Thu, 3 Feb 2022 03:14:05 +0000 Message-ID: References: <1c9e79d5-fd79-45a8-9c77-4228d770062d@www.fastmail.com> <3ae1a3f5-b4ea-40a0-b03f-3fa69ed6b9d2@www.fastmail.com> In-Reply-To: <3ae1a3f5-b4ea-40a0-b03f-3fa69ed6b9d2@www.fastmail.com> Accept-Language: en-CA, en-US Content-Language: en-CA X-MS-Has-Attach: X-MS-TNEF-Correlator: suggested_attachment_session_id: 580115f0-d8b0-9ef1-7f14-61087e6bce44 x-ms-exchange-messagesentrepresentingtype: 1 x-tmn: [BtM7mNDyW9ySQYh3A05GJXKXFkP/R/i9usaylYuZwiFE6B3IRlq4W0RwHp46mItKlk5F4dx7380=] x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: ac8e4592-297d-439c-fcd6-08d9e6c33c6b x-ms-traffictypediagnostic: MN2PR14MB2845:EE_ x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: xGAnXRW/LqPluuzOLiZed+k/pwdHpbsszBeiGOIqR6DPUIsR1XvDGXvRiIz94j775fxwSnzNrKKjWLWeKkzLeaY1fyjFAtSZqsMZja3vT9gP7G29lsbDEbSbWngmLTn8rmWonDqb2+/kbgAhhTvt4LvKiomO+bORGGG3aWev+7RwVC4BOaxVjO92Nj2xK/qBPrmEdGENpMe1bnzT5S/CpixhxleqMUZmt9pNv0I1etKbptA0FRnJOXr6meJmAPtBAhJtAKVeJasREcuUklDgGtR5MHwDU7smhDFbwf5kTOmGbDAiwhvh8PWGZWpdidLohap1+aEAgCacc/h7SUzXf8YGsl9+mK09S3eCFgMTzgLqArm4jTDBWFkbPGi04KMT9Jbm7NKKwgp+E1BmFolXyK2EBsw1v3uWc24PR58B3bgdnEkyhYYq8E9cHBYCot0TFXoXvJcjJZVT9bbMqouZ6XwEQodR4hteG62ASMAGYddqxESBTkiACXpgRQjSzngeDojQg8FGzxGmUlw0LpS4Lsqp5abjZa8zvzU6iA7BahskoxCRAMozASMsB/a/KDXLSgu+kePJOoNEHOKIeqjVd9pLEwTHe4SuYqHKHwKwHx4e+0mlr6fkLNTti/NUWd2KmRZAEp594OR7nC3nfH3RgJXLh7EaFGQBzdNQPQsRlQw= x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?iso-8859-1?Q?a0xip/cjlNwOQxthINXt4GBzV9qUtCUQe4/iXW54QNwVwT7RP6j0RrRu9m?= =?iso-8859-1?Q?k7i7ZPiG5z/e/y0reOw4esCfY674gAPP8pIkr2i9SXFQReNudyl8F1Rv8j?= =?iso-8859-1?Q?BvhNP0lpqGqX54uQzolE1E/sT+IZG2qLedfmXJYP2yrN5P9Kw5jZLpaLtN?= =?iso-8859-1?Q?8rQHTF1FKvv3PvjY5KIo7eQwG1VSrWtatBrHSkociauO0P5G7oF74syoeG?= =?iso-8859-1?Q?uMzyQcIl1Vfpj7sTHEoyOkmzXIlUqoctYSWgFX9QN6HG8cadiBzfmiAv70?= =?iso-8859-1?Q?7cOsH6jx0QlfFmH9yOlrglIcE6zwKYzEafcDY23M8609/pHNW78QyCt9RN?= =?iso-8859-1?Q?irLtbyTO7fp36BaGFttSHJHnRg82mD6dRV/vZB4LIac+W/arrxNih0VqEh?= =?iso-8859-1?Q?HAFGVTx8JRwVnyegWkp19VLo22sW73FU/5Cvj/X+Oos7Hid8lM/50CR0+d?= =?iso-8859-1?Q?13vfE61Wd65UALiW9GBmvB1rmIebIVIBmkf5kYpNyZY3hncLRTaAMgWNNt?= =?iso-8859-1?Q?cAP/X9grZq1BZexH5MHgz7sKkn9n/LcasOdvYHaR8SmdUp0LADeWKnBFbB?= =?iso-8859-1?Q?SxFXbRw0H8H+IEwk5Ul/aa7PjglpVuboZXNuo3wVa8mKfuQO5dH0nIGkl7?= =?iso-8859-1?Q?OTgwD0l5WS7xPOssrbA9t6ilHHoRS/0DveNpoI2wsYTAEsWVLTkl9hjoI7?= =?iso-8859-1?Q?vx6y1iRVJyqBJ3jdWt8IziQqnYj6XTi/jWDrd2xQ8/MZFXxaJu92gWgUGZ?= =?iso-8859-1?Q?q9MhPn9ixwplFK6+KHyHuSX63DEy0duSwUnzkxGo4+TjCi6h5+5Nww9JvI?= =?iso-8859-1?Q?UCOzOIO7N8bUqI2O6MwBIYGO/0fdPV3CZmfU70jqUnfEj6V5R+jJoXff4C?= =?iso-8859-1?Q?BS1J5+VuQMiKvHqWOkpGePfPwMi50PazALqSOwXV+tba+IEd/3LNbzymcT?= =?iso-8859-1?Q?hJjUbxMte7pyqE1BpYyHk4nIGyOTgcZQNs45EpXsWHSVy6mxXxsXNS3EiT?= =?iso-8859-1?Q?ylPAFLgY8/gvfWDLNIPB7u5FuDyLIPUzhMNu67wJAVISbZ9wymPeZbwPp9?= =?iso-8859-1?Q?dA=3D=3D?= Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-OriginatorOrg: sct-15-20-4755-11-msonline-outlook-cd57b.templateTenant X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: CH2PR14MB4151.namprd14.prod.outlook.com X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-CrossTenant-Network-Message-Id: ac8e4592-297d-439c-fcd6-08d9e6c33c6b X-MS-Exchange-CrossTenant-originalarrivaltime: 03 Feb 2022 03:14:05.5888 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-rms-persistedconsumerorg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN2PR14MB2845 Subject: Re: [PHP-DEV] Re: Adding `final class Deque` to PHP From: tysonandre775@hotmail.com (tyson andre) Hi Larry Garfield,=0A= =0A= > >> Returning $this would resolve that, I think.=A0 (Making it return a ne= w, immutable copy of the Deque would be even nicer, but I realize that's pr= obably not an argument I'm going to win at this point on this RFC.)=0A= > >=0A= > > Technically, you still can have single-expression usages in=0A= > > readable/unreadable ways=0A= > >=0A= > > - `[$deque->shift('value'), $deque][1]`, or=0A= > > - `($deque->shift('value') ?: $deque)`, or=0A= > > - `my_userland_helper_shift_and_return($deque, 'value')`=0A= >=0A= > ... Those are all grotesque.=0A= =0A= True, I wasn't familiar with that event and whether your goal was to write = as few lines as possible or as quickly as possible vs in a readable way to = people familiar with php.=0A= That clears that up.=0A= =0A= > > My personal preference is against making this fluent.=0A= > > I'd rather expose an efficient datastructure that's consistent with the= =0A= > > rest of PHP's functionality to the extent possible,=0A= > > which userland can use to write their own fluent/non-fluent classes.=0A= > > There's drawbacks to returning `$this`, including:=0A= > >=0A= > > 1. Inconsistency with existing APIs making remembering what does what= =0A= > > harder. Barely anything in php that I remember returns $this.=0A= > >=0A= > >=A0=A0=A0 https://www.php.net/manual/en/arrayobject.append.php returns v= oid.=0A= > >=0A= > >=A0=A0=A0 https://www.php.net/manual/en/function.array-push returns an i= nt.=0A= >=0A= > So it's already inconsistent, and frankly neither of those returns is use= ful.=A0 If the existing convention is "inconsistently unhelpful", then let'= s go ahead and make it helpful since "it's lame but at least it's consisten= t" is not an argument here.=A0 (Sometimes that is a valid argument, but not= in this case.)=0A= =0A= I won't know if anyone is strongly against fluent interfaces belonging in c= ore, or strongly against adopting fluent interfaces in an ad-hoc way that w= ould make it unclear if the vote was for/against the overall functionality = or the new design choice.=0A= =0A= I'd rather not do this without a policy RFC such as the one that passed for= namespaces https://wiki.php.net/rfc/namespaces_in_bundled_extensions=0A= =0A= If you expect this to be widely considered a better design php should adopt= , please create an RFC after the vote finishes (if it passes) before the fe= ature freeze outlining which methods of `Collection\Deque` would change and= how.=0A= =0A= > > 2. Inconsistency with possible new datastructures/methods=0A= > >=0A= > >=A0=A0=A0 If a `Map`/`Set` function were to be added, then methods for= =0A= > > add/remove would return booleans (or the old value), not $this=0A= >=0A= > Removal methods need to return the value being removed, sure.=A0 That mak= es sense across the board.=A0 But I don't see why Map::add($k, $v) or Set::= add($v) shouldn't also return $this.=A0 I would make the exact same ask the= re, for the exact same reason: To aid functional code, which (when the synt= ax is conducive to it) can be more readable in many cases than procedural a= pproaches.=0A= >=0A= > > 3. Slight additional performance overhead for functionality I assume=0A= > > will be used relatively infrequently=0A= >=0A= > I would assume the opposite, at least in my own code.=A0 I'm writing a lo= t of recursive or reduction-based routines these days, so if I had reason t= o use a queue/stack in them in the first place, they'd almost certainly get= used in this fashion.=0A= >=0A= > >=A0=A0=A0 (php has to increment reference counts and opcache can't elimi= nate=0A= > > the opcode to decrease reference counts and possibly free the return=0A= > > value of `$deque->shift()` with the return type info being an object)= =0A= >=0A= > I cannot speak to this one; how much of a difference is it in practice?= =A0=0A= =0A= Adding `RETURN_OBJ_COPY(Z_OBJ_P(ZEND_THIS));` and a different method with t= he `: Deque` return type:=0A= Around 6-8% for large arrays where push is the most frequent method call an= d the array reads are taken out since they'd be the same?=0A= (and similar for shift, I'd expect)=0A= (Intel CPU security mitigations/power management and so on have made benchm= arking less convenient, I ran the functions twice to see if it was consiste= nt)=0A= =0A= I'd still object to this for other stated reasons even if you think 6-8% is= worth it for your use cases for this or future additions to php in general= .=0A= =0A= ```=0A= Results for php 8.2.0-dev debug=3Dfalse with opcache enabled=3Dtrue=0A= =0A= Appending to Collections\Deque []=3D : n=3D 4 iterations=3D10000000= , create+destroy time=3D1.204 result=3D0=0A= Appending to Collections\Deque push : n=3D 4 iterations=3D10000000, = create+destroy time=3D1.551 result=3D0=0A= Appending to Collections\Deque fluent: n=3D 4 iterations=3D10000000, = create+destroy time=3D1.640 result=3D0=0A= Appending to Collections\Deque []=3D : n=3D 4 iterations=3D10000000= , create+destroy time=3D1.213 result=3D0=0A= Appending to Collections\Deque push : n=3D 4 iterations=3D10000000, = create+destroy time=3D1.549 result=3D0=0A= Appending to Collections\Deque fluent: n=3D 4 iterations=3D10000000, = create+destroy time=3D1.636 result=3D0=0A= =0A= Appending to Collections\Deque []=3D : n=3D 8 iterations=3D 5000000= , create+destroy time=3D0.923 result=3D0=0A= Appending to Collections\Deque push : n=3D 8 iterations=3D 5000000, = create+destroy time=3D1.263 result=3D0=0A= Appending to Collections\Deque fluent: n=3D 8 iterations=3D 5000000, = create+destroy time=3D1.335 result=3D0=0A= Appending to Collections\Deque []=3D : n=3D 8 iterations=3D 5000000= , create+destroy time=3D0.921 result=3D0=0A= Appending to Collections\Deque push : n=3D 8 iterations=3D 5000000, = create+destroy time=3D1.261 result=3D0=0A= Appending to Collections\Deque fluent: n=3D 8 iterations=3D 5000000, = create+destroy time=3D1.335 result=3D0=0A= =0A= Appending to Collections\Deque []=3D : n=3D 1024 iterations=3D 100000= , create+destroy time=3D1.246 result=3D0=0A= Appending to Collections\Deque push : n=3D 1024 iterations=3D 100000, = create+destroy time=3D1.972 result=3D0=0A= Appending to Collections\Deque fluent: n=3D 1024 iterations=3D 100000, = create+destroy time=3D2.150 result=3D0=0A= Appending to Collections\Deque []=3D : n=3D 1024 iterations=3D 100000= , create+destroy time=3D1.251 result=3D0=0A= Appending to Collections\Deque push : n=3D 1024 iterations=3D 100000, = create+destroy time=3D1.969 result=3D0=0A= Appending to Collections\Deque fluent: n=3D 1024 iterations=3D 100000, = create+destroy time=3D2.146 result=3D0=0A= =0A= Appending to Collections\Deque []=3D : n=3D 1048576 iterations=3D 100= , create+destroy time=3D2.450 result=3D0=0A= Appending to Collections\Deque push : n=3D 1048576 iterations=3D 100, = create+destroy time=3D3.172 result=3D0=0A= Appending to Collections\Deque fluent: n=3D 1048576 iterations=3D 100, = create+destroy time=3D3.371 result=3D0=0A= Appending to Collections\Deque []=3D : n=3D 1048576 iterations=3D 100= , create+destroy time=3D2.455 result=3D0=0A= Appending to Collections\Deque push : n=3D 1048576 iterations=3D 100, = create+destroy time=3D3.183 result=3D0=0A= Appending to Collections\Deque fluent: n=3D 1048576 iterations=3D 100, = create+destroy time=3D3.376 result=3D0=0A= =0A= ```=0A= =0A= =0A= > > 4.=A0 Returning $this makes code easier to write at some cost to=0A= > > readability - Developers new to php or using `Collections\Deque` for=0A= > > the first time would not immediately know what the code they're reading= =0A= > > is doing.=0A= > >=A0=A0=A0 (less of a problem with a good IDE, typechecker, and a typed= =0A= > > codebase, but this isn't universal)=0A= > >=A0=A0=A0 Having it return void, `return $deque->push()` would be less c= ommon=0A= > > and this would force the meaning to be clear.=0A= >=0A= > I disagree that it's less readable.=A0 Compare it with the alternatives y= ou showed at the start of your reply.=A0 None of those would qualify as "re= adable", IMO.=0A= =0A= True, I wasn't sure if your goal was readability (for php developers deeply= familiar with libraries) or to write as few lines as possible.=0A= Suggesting using `function` and adding several lines seemed like something = you were trying to avoid.=0A= =0A= =0A= > >=A0=A0=A0 Developers might have several guesses/assumptions based on the= ir=0A= > > experience with other methods in php/elsewhere=0A= > >=0A= > >=A0=A0=A0 - It returns the new count (JavaScript Array.push, array_push)= =0A= > >=A0=A0=A0 - It returns $this (Ruby)=0A= > >=A0=A0=A0 - It returns a lazy copy, like you'd wanted, not modifying the= =0A= > > original=0A= > >=A0=A0=A0 - It's returning void and the code in question is shorthand fo= r=0A= > > `return null`.=0A= > >=A0=A0=A0=A0=A0 (Python, C++=0A= > > https://www.cplusplus.com/reference/vector/vector/push_back/ ,=0A= > > offsetSet and spl push()/shift() methods)=0A= >=0A= > Once again, the lack of a consistent pattern means we don't need to follo= w the pattern.=A0 As with the first point, "it's silly but at least it's co= nsistent" is a valid argument in many cases, and we've changed the language= accordingly before.=A0 (Ternary associativity order comes to mind.)=A0 Tha= t's not the case here.=A0 There is no broad-based ingrained training that p= eople have that would lead them to expect a given return, so we can and sho= uld go for whichever one is most powerful/flexible/conducive to good code.= =A0 IMO, that's returning $this.=A0=0A= =0A= I continue to disagree for my previously stated reasons.=0A= =0A= > (Again, since a purely functional version is apparently off the table.)= =0A= =0A= I'd be in favor of immutable datastructures in core, but I don't think I'd = have much success in an RFC.=0A= I've only heard from a few voters that are passionately for them, I'm not s= ure if that translates to widespread interest.=0A= =0A= Though interestingly (and unrelated to this RFC https://wiki.php.net/rfc/de= que ), they do seem possible to implement with better asymptotic performanc= e than I'd have expected.=0A= Google searching mentioned Relaxed Radix Binary Trees, an immutable datastr= ucture surprisingly claiming performance near arrays in most operations.=0A= https://hypirion.com/musings/thesis https://github.com/hyPiRion/c-rrb=0A= =0A= Aside: An Immutable Map/Set would probably have a stronger argument than im= mutable sequeunces/deques/linked lists (arrays support copy on write alread= y so the original array is immutable, but arbitrary keys are something arra= ys don't support),=0A= but voters that didn't see a use case for immutables in core might be uncon= vinced.=0A= =0A= Thanks,=0A= Tyson=0A=