Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:112762 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 13803 invoked from network); 5 Jan 2021 13:51:10 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 5 Jan 2021 13:51:10 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 8F4EA1804E4 for ; Tue, 5 Jan 2021 05:27:22 -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=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail-lf1-f46.google.com (mail-lf1-f46.google.com [209.85.167.46]) (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 ; Tue, 5 Jan 2021 05:27:22 -0800 (PST) Received: by mail-lf1-f46.google.com with SMTP id h22so72610254lfu.2 for ; Tue, 05 Jan 2021 05:27:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=ts8058zJKxkLnktRkh2Dgl7fU6NZVyX3hly0mDh5fFU=; b=izxluVCba3qqwyQhL+GCDFiOIBbRrZqKpVjXCKJPJvhWAMJNeurqARBoROthaaf0o0 ktT8Ed5A7i97LNBj3YcjZZxs3Wj6TuAmV2S3Pi18bQRJZGmb/1M7ADirJImLJVobGWKO Gy6d23d1JwHlxG3n1E3LwYk3tgANeJaTICHn4alAwcD5RnGd9uejB3jjKyMheNx+BHN3 vYedkByucj5o7YIPY1xKSlTuD1zMOrw2p0LZCtQfE84fVJn+S34g65hfz0ZRfqsjsqq9 xig7GCaFaNjYJj7hOUSlo4ULQP6vpq9n7m6DeJfIrQyYYcgWjtsCj87HNsW93YaGR/Ip tH+Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=ts8058zJKxkLnktRkh2Dgl7fU6NZVyX3hly0mDh5fFU=; b=VrpV/HblvyGfUXpWxs9Q72R5EESXesHLpAjtc7exW7PHsY/COSY9CtOmWeuZmM1T4p GJWZoahGPCyy9HbtRWeohhqJZmuPqgHuDfNOe+fvvK1wPXybf093Qnj+JRIsIhY+18Tj XhwEwwrIqmGw8YB/UlWe1oLxhaY5UD4zIDK35UnJuC2bM03akbv1/XFbrMfKUoK3SIOd FCBDWewmX5oMdeKaJ67H4bmwrDSpGFAy6t5PnQ/EWVdj5hfdv9KO0AkdXtBI+TciOO0d Dzg4OWvodQ8b5m+hVVcWIyW3ro4fC1t0lQ8R0m1zuAcm1G4jjZrLz66Bb1BkOgUmhTpg eWcQ== X-Gm-Message-State: AOAM531f/ncu14kR2AZLMuwg6//rqaytH/ibGTYCnV27ch1Q7c9MkW0H 4Geov1BikcPWssQhX/NXCo1Lr2Wi5UlyGqmb4jQ= X-Google-Smtp-Source: ABdhPJw8SGMwM5/h0mChqUFB8H3pFIGY9Eq+qq0K3EO91SOTrCm0f3nOonDjmLFgCuLixmn/6GBMtNrcHh7v+mbe0CY= X-Received: by 2002:a05:6512:108b:: with SMTP id j11mr36698095lfg.44.1609853240520; Tue, 05 Jan 2021 05:27:20 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: Date: Tue, 5 Jan 2021 14:27:03 +0100 Message-ID: To: Levi Morrison Cc: tyson andre , "internals@lists.php.net" Content-Type: multipart/alternative; boundary="0000000000008ab5d905b8272a0c" Subject: Re: [PHP-DEV] Proposal: Adding SplFixedArray->push() and SplFixedArray->pop() From: nikita.ppv@gmail.com (Nikita Popov) --0000000000008ab5d905b8272a0c Content-Type: text/plain; charset="UTF-8" On Mon, Jan 4, 2021 at 4:58 PM Levi Morrison via internals < internals@lists.php.net> wrote: > On Tue, Dec 29, 2020 at 10:04 AM tyson andre > wrote: > > > > Hi Internals, > > > > Currently, PHP doesn't have a built in memory-efficient array type with > convenient push, pop, and other operations, similar to a list/vector in > other languages. > > The closest built in in SPL is [SplFixedArray]( > https://www.php.net/manual/en/class.splfixedarray.php) > > > > https://www.php.net/manual/en/class.splstack.php uses a doubly linked > list, which uses much more memory and time than sequential memory. > > > > Contrary to the name and description in > https://www.php.net/manual/en/class.splfixedarray.php, the class is > already resizable. > > The resize method is setSize - > https://www.php.net/manual/en/splfixedarray.setsize.php > > (increasing size is efficient - erealloc will extend the underlying > array into free memory if there is nothing in the way) > > > > Many programming languages have a memory-efficient list/vector type that > can be conveniently appended to and popped from. > > > > https://docs.python.org/3/tutorial/datastructures.html#more-on-lists > > https://www.cplusplus.com/reference/vector/vector/ > > https://docs.ruby-lang.org/en/2.0.0/Array.html#method-i-push > > > > Is there any interest in adding this? It would be much more efficient to > add these in C. > > The size of SplFixedArray is fixed, as its name suggests. Pushing and > popping don't really make sense; it's not the point of the data > structure. It does have a resize, but it's something of an escape > hatch, not a main use-case. > > As is, I think the SplStack should be soft-deprecated. I don't think > it was built with specific use-cases in mind; it was added because > "hey a stack can be implemented via linked list and so can queue, how > easy let's do it". I mean no disrespect to its original authors, but > exposing all those implementation details via inheritance means we > can't change it without breaking code. Note that Scala also deprecated > their Stack that was implemented around a List: > https://www.scala-lang.org/api/2.12.0/scala/collection/mutable/Stack.html. > > I also understand why the principal author of ext-ds wants to develop > and maintain it out of core; there are a lot of benefits to doing it > that way. > > ----- > > This is actually a common issue with the SPL. For instance, > ArrayIterator is basically ArrayObject and will almost always > duplicate the array, which is costly. It should only duplicate in a > write scenario, either through the iterator (which shouldn't be done, > but the API is there) or to the original array. However, it's > difficult to achieve because of the exposed API. > > I have a work-in-progress branch for adding `Spl\ForwardArrayIterator` > and `Spl\ReverseArrayIterator` which only increase the refcount of the > array, rather than duplicating it: > > https://github.com/php/php-src/compare/master...morrisonlevi:spl/ForwardArrayIterator > . > > If you can think about what stack operations you really need (for > instance, do you need to reserve capacity ahead of time?), I wouldn't > be opposed to adding `Spl\ArrayStack` or something similar depending > on your requirements. The key is that it needs to either expose > implementation details and own it in the name, or it needs to hide all > those details. > Rather than SplArrayStack, it might make sense to go for SplDeque, which is a structure that supports both stack and queue usage efficiently. I find that a normal PHP array serves well-enough for stacks (and is probably the most efficient way to do this in PHP), but queue and deque structures are somewhat inconvenient to implement efficiently using plain arrays as the base. Regards, Nikita --0000000000008ab5d905b8272a0c--