Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:112751 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 10634 invoked from network); 4 Jan 2021 16:22:19 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 4 Jan 2021 16:22:19 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 411FB1804E4 for ; Mon, 4 Jan 2021 07:58:18 -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,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-il1-f175.google.com (mail-il1-f175.google.com [209.85.166.175]) (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 ; Mon, 4 Jan 2021 07:58:17 -0800 (PST) Received: by mail-il1-f175.google.com with SMTP id r17so25661565ilo.11 for ; Mon, 04 Jan 2021 07:58:17 -0800 (PST) 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; bh=AO/9pQHXDs2nNtJh4xmgGc4TjwViY4zTw8+0vqBtR3A=; b=D7rydwPImxQg/DDstaHBJb5VWGRGhw5r2/JyIGZlRoQgoubOA+gdNU6Gsu1H+wnXEh IO+/HZHW2P1JNs4Dx6g3xQZpSh95mqezQtVvEk1aCc1HrTmKWZDKLpo1qHqcRRJx6+8j dFk1ONdza/O3eYmOAODuJ1BkVfuM4zOqgLncE= 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=AO/9pQHXDs2nNtJh4xmgGc4TjwViY4zTw8+0vqBtR3A=; b=qZ7CDaRU8QYHgfx8NvTjr8xPj6atWgzgAsQkL0O3RWNimGs8mGKv085sn98DHRrMEr Ulk8pyd/HoTinqD1lA+ENiV1wHTH7LVOuzraV9mwSTJ4atvcx8s25i2Nxe8FwHE0qR2Q hLX6CRMjY+37nuGxtnrU3ZZ3LzjXtxT0goR1bY4j1y4r/P8ps9gHgwIM43AJecog+wuS tKsCHbh70vmaRKTRhAviA7odz/CZRsqgEtS2Pe8PN/Ah4gJHrCmtemN+0uave+Zz8OFG qWot13YRAV9l37PEbHg0c6FseThhtbE4Nt91qm6wLPVL9uHyX/RNO2AeAz5UjCaX800E c1dA== X-Gm-Message-State: AOAM532JJctwzL/3aJh7x2GVeDD8xvuqa5WfxWlS7v+oYIyXfzuLPtCI PMEFU+3kjnSkU7yw87xRZNzKy82bbZqEuNIlqT75OA== X-Google-Smtp-Source: ABdhPJwtd476L0at6m2wX2ZlBHBlHPjFGYYlKyDpcxeOq3Ua4+I/CbPoI+u7hqVwsjv4WA2zY0FJ21DylsJvT3MW6hQ= X-Received: by 2002:a05:6e02:1311:: with SMTP id g17mr67503892ilr.223.1609775895225; Mon, 04 Jan 2021 07:58:15 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: Reply-To: Levi Morrison Date: Mon, 4 Jan 2021 08:58:04 -0700 Message-ID: To: tyson andre Cc: "internals@lists.php.net" Content-Type: text/plain; charset="UTF-8" Subject: Re: [PHP-DEV] Proposal: Adding SplFixedArray->push() and SplFixedArray->pop() From: internals@lists.php.net ("Levi Morrison via internals") 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.