Re: RFC: Add `final class Vector` to PHP

From: Date: Fri, 17 Sep 2021 02:50:09 +0000
Subject: Re: RFC: Add `final class Vector` to PHP
References: 1  Groups: php.internals 
Request: Send a blank email to internals+get-116050@lists.php.net to get a copy of this message
On Thu, Sep 16, 2021 at 8:10 PM tyson andre <tysonandre775@hotmail.com> 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.

« previous php.internals (#116050) next »