Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:116085 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 53704 invoked from network); 18 Sep 2021 04:52:52 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 18 Sep 2021 04:52:52 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 6BE431804B2 for ; Fri, 17 Sep 2021 22:33:00 -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=-0.2 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE, YOU_INHERIT 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-oi1-f180.google.com (mail-oi1-f180.google.com [209.85.167.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 ; Fri, 17 Sep 2021 22:32:59 -0700 (PDT) Received: by mail-oi1-f180.google.com with SMTP id bi4so16965624oib.9 for ; Fri, 17 Sep 2021 22:32:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=newclarity-net.20210112.gappssmtp.com; s=20210112; h=mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=ySnKy5fiVopgKq5P1kDnHcPQ68iKJey18m9L+dyI9/4=; b=4dcXCfTRpfbZ8M45OmqiteH+qfYR/tWEBsst+OBsNh14Pbi2n4kZSopRZfYK+CVRs6 XW9g/60ZXyWyBPApevF5GTREyKpJNkXYWhK+2F4nkV2OuX5eXQxlfo5Jb0QpikqBJeBW VgSjiceREkh/djbcu0ZwuUxcMLTHd3lf0WGEcVCsW/NS0iWuMncgOIzmBHIOjv4AUuQ5 XY3Y9vkvaT6pz6AJ+Ph7fjD1SrlJTb9V/FqlapJKdssuF0BbMiAZ4K/8Vvdp8ffv3mte kuVav44oBE16WkyT2JIXCE+1zxsvQfchmf83PKSaA8lMjvi/gket0mF8SrPqi+7g3cbi QtVA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=ySnKy5fiVopgKq5P1kDnHcPQ68iKJey18m9L+dyI9/4=; b=WiRiBIjG6myIVSHB6YtkuXOrulcwb4jVoDGOaGvEZkQTRgWlhqo/cHNjrfkd599033 Jipal8I2K3VSkLMvAvryT6L0SvnPCwrb2n/WJIga6NW9qnQHOb2SKFLrkL9n9mZvhOGY JmsoO0DPBSn4Wb9BBZQXdaKs/253gCKEe4p0f/pJQs5M5Zu6pqHMd60IfyptYfomAydr pU+Y1NxelOruxnil9QAJ6IcUujhHRCgK1Yp+ni4wG7v2MH8IIJYCJIwYWBFYjHWhe48/ HHuiJLrVt+L3UDnscC6yoa53+wKseIh1r9ttkb8akjlkWuPRKq/OUKVV3TZSnWX1pxbr zkhQ== X-Gm-Message-State: AOAM530WQtCiujHSg+C399pEJEJ+1ARmkjkuowIOzKwtNQuNpxS05MQi y9zxCa4bryzV5Gdfbx/HEoHNgA== X-Google-Smtp-Source: ABdhPJz7YYa/xGtCsCTRK8nXtgB1JpkAKKyYXerYzAEkkfO1e068Q9+77CwDpTWBACqGi9l+fBplww== X-Received: by 2002:aca:6008:: with SMTP id u8mr6750817oib.127.1631943178220; Fri, 17 Sep 2021 22:32:58 -0700 (PDT) Received: from [192.168.1.10] (c-24-98-254-8.hsd1.ga.comcast.net. [24.98.254.8]) by smtp.gmail.com with ESMTPSA id w10sm2000755oiv.14.2021.09.17.22.32.57 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 17 Sep 2021 22:32:57 -0700 (PDT) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 13.4 \(3608.120.23.2.7\)) In-Reply-To: Date: Sat, 18 Sep 2021 01:32:56 -0400 Cc: "internals@lists.php.net" Content-Transfer-Encoding: quoted-printable Message-ID: References: To: tyson andre X-Mailer: Apple Mail (2.3608.120.23.2.7) Subject: Re: [PHP-DEV] RFC: Add `final class Vector` to PHP From: mike@newclarity.net (Mike Schinkel) > On Sep 16, 2021, at 10:09 PM, tyson andre = wrote: >=20 > Hi internals, >=20 > I've created a new RFC https://wiki.php.net/rfc/vector proposing to = add `final class Vector` to PHP. >=20 > 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. >=20 > It would be useful to have an efficient variable-length container in = the standard library for the following reasons:=20 >=20 > 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=20 > (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=20 > 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. >=20 > Thoughts on Vector? 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): =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D setCapacity(int) =E2=80=94 Sets the Capacity, i.e. the maximum Size = before resize getCapacity():int =E2=80=94 Gets the current Capacity. setGrowthFactor(float) =E2=80=94 Sets the Growth Factor for push(). = Defaults to 2 getGrowthFactor():float =E2=80=94 Gets the current Growth Factor pop([shrink]):mixed =E2=80=94 Returns [Size] then subtracts 1 from Size. = If (bool)shrink passed then call shrink(). push(mixed) =E2=80=94 Sets [Size]=3Dmixed, then Size++, unless = Size=3DCapacity then setSize(n) where n=3Dround(Size*GrowthFactor,0) = before Size++. grow([new_capacity]) =E2=80=94 Increases memory allocated. Sets Capacity = to Size*GrowthFactor or new_capacity. shrink([new_capacity]) =E2=80=94 Reduces memory allocated. Sets Capacity = to current Size or new_capacity. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D 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. Would this not work? -Mike P.S. I also think asking for new methods on SplFixedArray has a much = greater chance of successful than an RFC for Vector. #jmtcw