Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:116050 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 14966 invoked from network); 17 Sep 2021 02:10:32 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 17 Sep 2021 02:10:32 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 723B41804CF for ; Thu, 16 Sep 2021 19:50:24 -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,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-qk1-f180.google.com (mail-qk1-f180.google.com [209.85.222.180]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Thu, 16 Sep 2021 19:50:23 -0700 (PDT) Received: by mail-qk1-f180.google.com with SMTP id p4so12962280qki.3 for ; Thu, 16 Sep 2021 19:50:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=datadoghq.com; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=6Umjjggc0RQMDOqx4VzjQ74p4dPWtfIg8KzlX6Zdo1E=; b=C5cTGR81k1cdkNZe174XlJBLUJbe8vj/R1spxzw+SDXB6wAM5ZExGJwPqkKecQ4juH j4CaKlpao6ftOlNqzxSo3VEthysEn0412HoUOAQxAGXrweeiwr7V4UqjzWSEZ4w0tMeK 89TxJCFPUh/deP4C/zfXaKJdR6eQfY34tu1/8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=6Umjjggc0RQMDOqx4VzjQ74p4dPWtfIg8KzlX6Zdo1E=; b=Bb4vj+RIKkxhCGTBioNvTLz3X5vK5jkMcm507tdkqcW0KOGMoQzolNTjWQO9bqYQQq ntkCTTKknoWpX4LH8nMxbgJFcjpFhym8sP8iwlErx1KKczhZum2Ng0DSXZhniWdA3ut3 WFSnUBXFOswM70JECRHzPaH4oCpUqYepl1tXssHsBr4wLPAG3zb1a73XjobvCVb6D345 3gZPh2EyixgWTnaojuWg1ShPD+SewH/lotAVax59DGySF/n3L8EoScTJqedJ3jp/NFWD WCLLf488J/Sv/NkySWfujjMHm5lnPH5r8TKFpb2BIByiMJ3AEXsJDoMCm9azPbh7wSG6 h57Q== X-Gm-Message-State: AOAM5309ILgY2ZpX47puLzc/fARI8ZQmyt7VU9BDGRRFD3rzFHnZ+t7Q qORfaclquhy6DtovZrGAwHS1Otjar1H6hjnJknUXIxJGte2VcA== X-Google-Smtp-Source: ABdhPJwsRZ44ZDKEOOKalwmTgBpqrBjvzzhqN7jE2bG4joHcsV1Mh7w5eHyL1Qh3DA3VIuaoJpoxBgbi2XnjCdR036o= X-Received: by 2002:a25:3604:: with SMTP id d4mr10195324yba.4.1631847020693; Thu, 16 Sep 2021 19:50:20 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Reply-To: Levi Morrison Date: Thu, 16 Sep 2021 20:50:09 -0600 Message-ID: To: tyson andre Cc: "internals@lists.php.net" Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] RFC: Add `final class Vector` to PHP From: internals@lists.php.net ("Levi Morrison via internals") On Thu, Sep 16, 2021 at 8:10 PM tyson andre wro= te: > > 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 i= s 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 inte= rnal array HashTable API to the PHP's internals and PECLs, additional memor= y is needed to track keys (https://www.npopov.com/2014/12/22/PHPs-new-hasht= able-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 lea= st 8 elements to make the initial resizing more efficient, potentially wast= ing 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 man= y 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 exceedi= ng 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` f= or use cases > where objects are easier to use than arrays - e.g. variable sized coll= ections (For lists of values) that can be passed by value to be read and mo= dified. > 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 `\Vecto= r`). > (That is a PECL I created earlier this year for future versions of iterab= le proposals, common data structures such as Vector/Deque, and less commonl= y used data structures that may be of use in future work on implementing ot= her 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.