Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:116981 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 62120 invoked from network); 4 Feb 2022 14:09:19 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 4 Feb 2022 14:09:19 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 0D7581804A7 for ; Fri, 4 Feb 2022 07:24:16 -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 NAM12-MW2-obe.outbound.protection.outlook.com (mail-mw2nam12olkn2044.outbound.protection.outlook.com [40.92.23.44]) (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 ; Fri, 4 Feb 2022 07:24:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=iIjcGmGxUNpvHXbWia+sQZpeeVtaGdmwiFA1aqnOL2vz3iSDJo86nTrcZaH1U9xo9im6o87bSJEa8LTVocIFCzTh27E+ZZk8ndC6rzRV0nWK6+x2JFtOna8GNG8E+2WiIp3Uv8CopRsV3HO84JHf0nytCDEV1qgJk0K/jNqkx7tQzwViuASNacpmxjD4r8f5SlXW8oBcjKrz8EZz+oA6q2ETR6IVz5GNk+AGNetFV/ARXBJz7IoUPMn0F8KtBGz+WVht+SZrUsXidosItHGqELtOXvCi/wx7ZTTAH9XuJNarNgA7PyWIe68T9cCW6LZSWZnAEPHW3jAlLKi40nWZiw== 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=gN4taBsABWkiHTRjTZHJHrPQNyzoucZdAXvkytKCJTM=; b=aEqj/3/UW7g8aW/z4NxhchuxT+nVvPmy7ndO1Sea6RltXR7a8WdHlZH+e8DTAPkVOTu46Dz7QDqF6BHEdiezs20/XtTq/nAx4WdD5/u1KP/01PrS+iJj40tDEyvEP79/JEtrL1mw1xSfHJGiNSJ17luLNr11vWrwtRvMmxHbyzUwuNUmhau1RYG2kyQZH1jzRNTeC0s1wX1VnDWi722ihWyB02BIJLsdgmK0D8khMHS3I5vN7Y9m9Cu/7UwpiAq00/jZs6SUGcJw8LQ2GjGr94Nb7TJsXUICmxtLD/kpArUFxx5wWZM1izI/Y3j7FhLWySXCblf9zggQbbAGTVS17g== 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=gN4taBsABWkiHTRjTZHJHrPQNyzoucZdAXvkytKCJTM=; b=b8EVkNusP00NsOcgWciMyf+Byk5fGUjq3q1jDER9qEuYtGOObjgAXd3k8Ysh1UHGpv4g3w6GXzDAm6YGUI3Dx1NSM6p0fjrRYK34rj8jDkp7L8mVwZrUGIX9bkyzlJxUMkFVYkjQFzbu/piWRQN+GBKyM4YEjQLStbfubRbCpFGBPwIs/rWzw3DlEcLXcMwhSpHxZW+x93G/mP9uCHo8B1vi/W0NXxW9ML2WURVHXUSPgusGc8Ci0U8YcDobr+auyaEEeqyvbjmQ3YhUN7mqPOmVJziw3GAkreoDr1ExsUEmTDFas3OcCOnNXw+0HsJBcv7bXLWpEMKcvDYewUhSRg== Received: from DM6PR14MB4155.namprd14.prod.outlook.com (2603:10b6:5:21e::11) by BYAPR14MB2487.namprd14.prod.outlook.com (2603:10b6:a03:ab::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4951.12; Fri, 4 Feb 2022 15:24:13 +0000 Received: from DM6PR14MB4155.namprd14.prod.outlook.com ([fe80::1ddb:6eeb:a96d:9846]) by DM6PR14MB4155.namprd14.prod.outlook.com ([fe80::1ddb:6eeb:a96d:9846%3]) with mapi id 15.20.4951.013; Fri, 4 Feb 2022 15:24:13 +0000 To: "internals@lists.php.net" Thread-Topic: Adding `final class Deque` to PHP Thread-Index: AQHXraUxaK1dZ0niuECOJlLUpHNcpax/lZKygATD6ZU= Date: Fri, 4 Feb 2022 15:24:13 +0000 Message-ID: References: In-Reply-To: Accept-Language: en-CA, en-US Content-Language: en-CA X-MS-Has-Attach: X-MS-TNEF-Correlator: suggested_attachment_session_id: 7c786e75-190b-ec75-aef4-67ab36d64dd5 x-ms-exchange-messagesentrepresentingtype: 1 x-tmn: [qz178fL/d00ZpGs2Wg5KBIlDxaqAExSu7cqpPL4ECU0I0kXkbWAw8KFHaTnBeW3YreT5uHGS3tI=] x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: 938ff056-cff4-4813-d16b-08d9e7f26625 x-ms-traffictypediagnostic: BYAPR14MB2487:EE_ x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: iXkSS1WmQavrgQR1Hm+zL3ZyE148XXwQMx5Ryz6mVojrlJNWjMdKsPA9ONoaz1OESKoS7xBwwo9BXHFze9fR7SbKErL4mig3AKO0+HhIXwBGAlCtYLVvPtQ/mqb2ZaA/BZwwmnHn1P87Abv3dX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10mR27YTGa3aotUbR+6d/9kH7JVRqf?= =?iso-8859-1?Q?VgVeINvHH+o2YM+6VqHGyjr6t/f3tc/WJPaIfBmKtzGj9VoUT1zN1nm0zC?= =?iso-8859-1?Q?6eKGClpGx/ZuEwDgXr2sGSQk4g/radU7wZ5mBHxYO/tOuegBzs6ZE7ed+u?= =?iso-8859-1?Q?soS9WTFT1UNXjsXNjdh5ZxtT+wj5CFwBkMXeORB5lK2tgeBNbnKfb/kr+G?= =?iso-8859-1?Q?ZrM5zpL+WKbyVClHoTZ5ZQjqHldXJFDs6ZDGLjzvQeK1MsUbzdFw2cyjYz?= =?iso-8859-1?Q?HlKT6J+u9itOWfML48BvH6lY1yAkmRzOYSUpkUpWUL/n1C6Ro8vl51LHb5?= =?iso-8859-1?Q?vtf4cSlNsBkL3FVRLSR3RYwBDGkA4VvyHah6zpXw2A4o3XiiQX+crFIAhZ?= =?iso-8859-1?Q?TQpzfScz5k/OvKO/4xECl71lBCD79vLjkzgCU8OP+tpq06PiU5xlqAtHlV?= =?iso-8859-1?Q?PzNunWLu6WrFS8D6LL/9tDuwP4DzO8bu2dDksnDFa43EcI1+VUAJdNLIrU?= =?iso-8859-1?Q?fK1vKuLWK0ah1zlYAA4hTpTowEHQ67tluWZ1pJxMpwBMZLBD9DbxkbUEku?= =?iso-8859-1?Q?Gwpd6sYLeOOcS0D0a38FpvHXo5WHzaxeeVYdq2Pt/mP7tfKx25nQomb2hA?= =?iso-8859-1?Q?HgM1sqCpOczYXUqtJtOiu4hhkYebmdIHIHhm2Tr0bQs23EKz2LbULNjdTq?= =?iso-8859-1?Q?XA=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: DM6PR14MB4155.namprd14.prod.outlook.com X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-CrossTenant-Network-Message-Id: 938ff056-cff4-4813-d16b-08d9e7f26625 X-MS-Exchange-CrossTenant-originalarrivaltime: 04 Feb 2022 15:24:13.0554 (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: BYAPR14MB2487 Subject: Re: Adding `final class Deque` to PHP From: tysonandre775@hotmail.com (tyson andre) Hi internals,=0A= =0A= > > I've created a new RFC https://wiki.php.net/rfc/deque to add a `final c= lass Deque`=0A= > >=0A= > > This is based on the `Teds\Deque` implementation I've worked on=0A= > > for the https://github.com/TysonAndre/pecl-teds PECL.=0A= > >=0A= > > While `SplDoublyLinkedList` and its subclass `SplQueue`/`SplStack` exis= t in the SPL, they have several drawbacks=0A= > > that are addressed by this RFC to add a `Deque` class (to use instead o= f those):=0A= > >=0A= > > 1. `SplDoublyLinkedList` is internally represented by a doubly linked l= ist,=0A= > > =A0=A0 making it use roughly twice as much memory as the proposed `Dequ= e`=0A= > > 2. `push`/`pop`/`unshift`/`shift` from `SplDoublyLinkedList` are slower= due to=0A= > > =A0=A0 needing to allocate or free the linked list nodes.=0A= > > 3. Reading values in the middle of the `SplDoublyLinkedList` is proport= ional to the length of the list,=0A= > > =A0=A0 due to needing to traverse the linked list nodes.=0A= > > 4. `foreach` Iteration behavior cannot be understood without knowing wh= at constructed the=0A= > > =A0=A0 `SplDoublyLinkedList` instance or set the flags.=0A= > >=0A= > > It would be useful to have an efficient `Deque` container in the standa= rd library=0A= > > to provide an alternative without those drawbacks,=0A= > > as well as for the following reasons:=0A= > >=0A= > > 1. To save memory in applications or libraries that may need to store m= any lists of values or run for long periods of time.=0A= > > =A0=A0 Notably, PHP's `array` type will never release allocated capacit= y.=0A= > > =A0=A0 See https://www.npopov.com/2014/12/22/PHPs-new-hashtable-impleme= ntation.html=0A= > > 2. To provide a better alternative to `SplDoublyLinkedList`, `SplStack`= , and `SplQueue`=0A= > > =A0=A0 for use cases that require stacks or queues.=0A= > > 3. As a more efficient option than `array` and `SplDoublyLinkedList`=0A= > > =A0=A0 as a queue or `Deque`, especially for `unshift`.=0A= > >=0A= > > A `Deque` is more efficient than an `array` when used as a queue, more = readable, and easier to use correctly.=0A= > > While it is possible to efficiently remove elements from the start of a= n `array` (in terms of insertion order) (though this makes reset()/array_ke= y_first() inefficient),=0A= > > it is very inefficient to prepend elements to the start of a large `arr= ay` due to needing to either copy the array=0A= > > or move all elements in the internal array representation,=0A= > > and an `array` would use much more memory than a `Deque` when used that= way (and be slower).=0A= > >=0A= > > There are also several pitfalls to using an array as a queue for larger= queue sizes,=0A= > > some of which are not obvious and discovered while writing the benchmar= ks.=0A= > > (Having a better (double-ended) queue datastructure (`Deque`) than the = `SplDoublyLinkedList`=0A= > > would save users from needing to write code with these pitfalls):=0A= > >=0A= > > 1. `array_key_first()` and reset()`takes time proportional to the numbe= r of elements `unset` from the start of an array,=0A= > > =A0=A0 causing it to unexpectedly be extremely slow (quadratic time) af= ter unsetting many elements at the start of the queue.=0A= > > =A0=A0 (when the array infrequently runs out of capacity, buckets are m= oved to the front)=0A= > > 2. `reset()` or `end()` will convert a variable to a reference,=0A= > > =A0=A0 and php is less efficient at reading or writing to reference.=0A= > > =A0=A0 Opcache is also less efficient at optimizing uses of variables u= sing references.=0A= > > 3. More obviously, `array_unshift` and `array_shift` will take time pro= portional to the number of elements in the array=0A= > > =A0=A0 (to reindex and move existing/remaining elements).=0A= >=0A= > I plan to start voting on https://wiki.php.net/rfc/deque on Friday, Febru= ary 4th.=0A= >=0A= > Several changes have been made to https://wiki.php.net/rfc/deque#changelo= g=0A= > after the feedback in https://externals.io/message/116100=0A= >=0A= > - The class is now named `Collections\Deque`=0A= > - The api documentation in https://wiki.php.net/rfc/deque#proposal was ex= panded for methods.=0A= > - Benchmarks were updated.=0A= > - Like other standard datastructures, iteration over the deque is now ove= r the original object (instead of creating a copy),=0A= > =A0 and mutating the deque will be reflected in `$iterator->current()` (a= nd moving the end with push()/pop() will affect where iteration ends).=0A= > - Iteration will account for calls to shift/unshift moving the start of t= he deque.=0A= > =A0 the offsets will be corrected and values won't be skipped or iterated= over multiple times.=0A= > =A0 (no matter how many iterators were created by `Deque->getIterator()`)= =0A= > =A0 See https://wiki.php.net/rfc/deque#iteration_behavior=0A= > - The get()/set() methods were removed, after feedback in https://externa= ls.io/message/116100#116214=0A= >=0A= > A WebAssembly demo is available at https://tysonandre.github.io/php-rfc-d= emo/deque/=0A= =0A= I've updated the RFC https://wiki.php.net/rfc/deque yet again (no implement= ation changes). I now plan to start voting on Saturday, February 5th.=0A= =0A= I've also updated the WebAssembly demo at https://tysonandre.github.io/php-= rfc-demo/deque/ to include a benchmark to illustrate the differences in per= formance of shift/unshift at various deque/array sizes.=0A= A more detailed section on the performance of unshift/shift compared to arr= ay and existing data structures was added to the RFC.=0A= =0A= I've also added a discussion section for the request to `return $this` (htt= ps://externals.io/message/116100#116967), removed duplicated quotes, and cl= arified some parts of the RFC.=0A= =0A= Thanks,=0A= Tyson=0A=