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

From: Date: Sat, 18 Sep 2021 23:26:31 +0000
Subject: Re: RFC: Add `final class Vector` to PHP
References: 1 2  Groups: php.internals 
Request: Send a blank email to internals+get-116093@lists.php.net to get a copy of this message
Hi Mike Schinkel, > Given there seems to be a lot of concern about the approach the RFC proposes would it not > address the concerns about memory usage and performance if several methods were added to > SplFixedArray instead (as well as functions like indexOf(), contains(), map(), filter(), > JSONSerialize(), etc., or similar): > > =============== > > setCapacity(int) — Sets the Capacity, i.e. the maximum Size before resize > getCapacity():int — Gets the current Capacity. > > setGrowthFactor(float) — Sets the Growth Factor for push(). Defaults to 2 > getGrowthFactor():float — Gets the current Growth Factor > > pop([shrink]):mixed — Returns [Size] then subtracts 1 from Size. If (bool)shrink passed then > call shrink(). > push(mixed) — Sets [Size]=mixed, then Size++, unless Size=Capacity then setSize(n) where > n=round(Size*GrowthFactor,0) before Size++. > > grow([new_capacity]) — Increases memory allocated. Sets Capacity to Size*GrowthFactor or > new_capacity. > shrink([new_capacity]) — Reduces memory allocated. Sets Capacity to current Size or > new_capacity. > > =============== > > If you had these methods then I think you would get the memory and performance improvements you > want, and if you really want a final Vector class for your own uses you could roll your own using > inheritance or containment. I asked 8 months ago about push/pop in SplFixedArray. The few responses were unanimously opposed to SplFixedArray being repurposed like a vector, the setSize functionality was treated more like an escape hatch and it was conceptually for fixed-size data. I also believe adding a configurable growth factor would be excessive for a high level language. Thanks, Tyson

« previous php.internals (#116093) next »