Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:116061 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 68712 invoked from network); 17 Sep 2021 07:56:20 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 17 Sep 2021 07:56:20 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id D2CB1180505 for ; Fri, 17 Sep 2021 01:36:14 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,HTML_MESSAGE,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS2639 31.186.226.0/24 X-Spam-Virus: No X-Envelope-From: Received: from sender11-op-o11.zoho.eu (sender11-op-o11.zoho.eu [31.186.226.225]) (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, 17 Sep 2021 01:36:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1631867765; cv=none; d=zohomail.eu; s=zohoarc; b=GW+umA4Lf9eCGJY9BjPJyp3PlN/1OvRYXiTugzm1vyrfQuQHtkZFBGflCluyLcUaoD63RGsE8g0ODUgB6mKvDVPBGQsf1VDqXl4w3lTtbFPU+6Le1gRkq85JwySiGAPRsVb5PQVPsJgUTZSyb5LyGxhpYcI8L1wu21aPUJkVFSs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.eu; s=zohoarc; t=1631867765; h=Content-Type:Cc:Date:From:In-Reply-To:MIME-Version:Message-ID:References:Subject:To; bh=fcAUR2tS4+HBgcNbfMOuA/LvC8sJUWLPjX8PQpyxxgk=; b=iMdn0RgFINquiNzpSrSuPEviTgyjTK+JkuSj8bb5lt+YYxzy1TkbvmJo5Cmhv4Bttm/sKpvggsALeE6ubg2v7bQbrXAFTALTW7NvbKaHWWEUCL4TZh/Fck4B7MhRbxG0VNY31NiEMwfy18aVGl+NIfXmsAxbZPGLH0uZJ9yOz5g= ARC-Authentication-Results: i=1; mx.zohomail.eu; dkim=pass header.i=limesurvey.org; spf=pass smtp.mailfrom=olle.haerstedt@limesurvey.org; dmarc=pass header.from= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1631867765; s=zoho; d=limesurvey.org; i=olle.haerstedt@limesurvey.org; h=Date:From:To:Cc:Message-Id:In-Reply-To:References:Subject:MIME-Version:Content-Type; bh=fcAUR2tS4+HBgcNbfMOuA/LvC8sJUWLPjX8PQpyxxgk=; b=gd4T4leucCFLbc6ZsBOcfY9xfxp/zUO8u1V+ceKvi20r8rAa8OYjkPeEF3/bjHMp /EH87Bhteagjqhedb26he7Ip/j4hhRL96GUENq/KkHCKxJ35E8sQJAIBSyrNsPSBran yhDjEV5TL2pTLVmTPNoelUtr7JlDgzJYR9lYVhqU= Received: from mail.zoho.eu by mx.zoho.eu with SMTP id 163186776517218.793041391223937; Fri, 17 Sep 2021 10:36:05 +0200 (CEST) Date: Fri, 17 Sep 2021 10:36:05 +0200 To: "Levi Morrison" Cc: "internals" , "tyson andre" Message-ID: <17bf2e601b0.f6c8830d819209.1233730022805275149@limesurvey.org> In-Reply-To: References: MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_2804753_576663640.1631867765169" Importance: Medium User-Agent: Zoho Mail X-Mailer: Zoho Mail Subject: Re: [PHP-DEV] RFC: Add `final class Vector` to PHP From: olle.haerstedt@limesurvey.org (=?UTF-8?Q?Olle_H=C3=A4rstedt?=) ------=_Part_2804753_576663640.1631867765169 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 7bit On Thu, Sep 16, 2021 at 8:10 PM tyson andre wrote: > > Hi internals, > > I've created a new RFC https://wiki.php.net/rfc/vector proposing to add `final class Vector` to PHP. > > PHP's native `array` type is rare among programming language in that it is used as an associative map of values, but also needs to support lists of values. > In order to support both use cases while also providing a consistent internal array HashTable API to the PHP's internals and PECLs, additional memory is needed to track keys (https://www.npopov.com/2014/12/22/PHPs-new-hashtable-implementation.html - around twice as much as is needed to just store the values due to needing space both for the string pointer and int key in a Bucket, for non-reference counted values)). > Additionally, creating non-constant arrays will allocate space for at least 8 elements to make the initial resizing more efficient, potentially wasting memory. > > It would be useful to have an efficient variable-length container in the standard library for the following reasons: > > 1. To save memory in applications or libraries that may need to store many lists of values and/or run as a CLI or embedded process for long periods of time > (in modules identified as using the most memory or potentially exceeding memory limits in the worst case) > (both in userland and in native code written in php-src/PECLs) > 2. To provide a better alternative to `ArrayObject` and `SplFixedArray` for use cases > where objects are easier to use than arrays - e.g. variable sized collections (For lists of values) that can be passed by value to be read and modified. > 3. To give users the option of stronger runtime guarantees that property, parameter, or return values really contain a list of values without gaps, that array modifications don't introduce gaps or unexpected indexes, etc. > > Thoughts on Vector? > > P.S. The functionality in this proposal can be tested/tried out at https://pecl.php.net/teds (under the class name `\Teds\Vector` instead of `\Vector`). > (That is a PECL I created earlier this year for future versions of iterable proposals, common data structures such as Vector/Deque, and less commonly used data structures that may be of use in future work on implementing other data structures) > > Thanks, > Tyson > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: https://www.php.net/unsub.php > I'm okay with a final Vector class in general. I don't love the proposed API but don't hate it either. Feedback on that at the end. What I would _love_ is a `vec` type from hacklang, which is similar to this but pass-by-value, copy-on-write like an array. Of course, this would require engine work and I understand it isn't as simple to add. Feedback on API: - `indexOf` returning `false` instead of `null` when it cannot be found. If we are keeping this method (which I don't like, because there's no comparator), please return `null` instead of false. The language has facilities for working with null like `??`, so please prefer that when it isn't needed for BC (like this, this is a new API). - `contains` also doesn't have a comparator. - Similarly but less strongly, I don't like the filter callable returning `mixed` -- please just make it `bool`. - I don't know what `setSize(int $size)` does. What does it do if the current size is less than `$size`? What about if its current size is greater? I suspect this is about capacity, not size, but without docs I am just guessing. -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: https://www.php.net/unsub.php use SplFixedArray as vec; Done. ;) Olle ------=_Part_2804753_576663640.1631867765169--