Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:114791 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 2968 invoked from network); 9 Jun 2021 04:32:49 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 9 Jun 2021 04:32:49 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 7A3041804D8 for ; Tue, 8 Jun 2021 21:47:45 -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-Virus: No X-Envelope-From: Received: from mail-yb1-f179.google.com (mail-yb1-f179.google.com [209.85.219.179]) (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, 8 Jun 2021 21:47:44 -0700 (PDT) Received: by mail-yb1-f179.google.com with SMTP id n133so33644621ybf.6 for ; Tue, 08 Jun 2021 21:47:44 -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; bh=Od9wplwu9hKa6ZbjIcnjBqzgD3T5x9uBce93aZONEgI=; b=McMZrfDtV0+2N5ZtRg90kDlTxBiOKoF46M/BoCIikGseaKL6WHRKHEM8NRzzxp1p5i SR1kC6fne4ukwp95lmeWdycnV4hjpZ1ALbUDN0jOeT7D2c0Gkmu/E0KC8ehxXIF9ul9Q Qdiid6ztrq6owF3M/14scez8bOmoYVTbfqrlg= 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=Od9wplwu9hKa6ZbjIcnjBqzgD3T5x9uBce93aZONEgI=; b=HD6iQa//2K2YmiqjQX8ERwscHYB2yTShYFQDGyL7M1FDSGtvvrcNfe0sfh/gMPBPvB FEYjx6TGcFiFOY0gCfvDIyNfHm3MezDDMR67So9WFuAYYjctHLZOyXl56DBB6dGzyUii lc1TbWeMVMJutPNYBQ1YbXg+v7/oJZnHT8b5hjJ50gvPoE9FjU+H/y6fkqZ8qZ4xyJzw ZTrr+rEX1EOBBpKzOiozwuoAUbBXHyDiy2lF6Epv3/FtC12I9kFXf9NU6nhDSK3YLuRa 6uc1EPkKl1Fjz/RnS8oj4lPrCcaiKxIPvMlsARq4c89H+DOJAJt/opdeJzJh6jKAanT1 ketg== X-Gm-Message-State: AOAM533QdN+ZOYoDlmD2XLsCMArF1nhcBxZu4+dRwk+VIcyZq+9RA4zw kqW6LWl2YadVyi5iScmr53Y39qEZVsg73ZCbyYCiKQ== X-Google-Smtp-Source: ABdhPJyrTCQeaCl4LmExt3Asv/ml0GQU66JUWKKKJ6LAfAQudo9BkZiF8BqPfY6nJgtIjDdT1Vu411fSFUDaOwiiejQ= X-Received: by 2002:a25:b993:: with SMTP id r19mr37550024ybg.445.1623214062621; Tue, 08 Jun 2021 21:47:42 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Reply-To: Levi Morrison Date: Tue, 8 Jun 2021 22:47:32 -0600 Message-ID: To: tyson andre Cc: PHP internals Content-Type: text/plain; charset="UTF-8" Subject: Re: [PHP-DEV] Re: RFC: CachedIterable (rewindable, allows any key&repeating keys) From: internals@lists.php.net ("Levi Morrison via internals") On Tue, Jun 8, 2021 at 6:22 PM tyson andre wrote: > > Hi internals, > > > I've created a new RFC https://wiki.php.net/rfc/cachediterable adding CachedIterable, > > which eagerly evaluates any iterable and contains an immutable copy of the keys and values of the iterable it was constructed from > > > > This has the proposed signature: > > > > ``` > > final class CachedIterable implements IteratorAggregate, Countable, JsonSerializable > > { > > public function __construct(iterable $iterator) {} > > public function getIterator(): InternalIterator {} > > public function count(): int {} > > // [[$key1, $value1], [$key2, $value2]] > > public static function fromPairs(array $pairs): CachedIterable {} > > // [[$key1, $value1], [$key2, $value2]] > > public function toPairs(): array{} > > public function __serialize(): array {} // [$k1, $v1, $k2, $v2,...] > > public function __unserialize(array $data): void {} > > > > // useful for converting iterables back to arrays for further processing > > public function keys(): array {} // [$k1, $k2, ...] > > public function values(): array {} // [$v1, $v2, ...] > > // useful to efficiently get offsets at the middle/end of a long iterable > > public function keyAt(int $offset): mixed {} > > public function valueAt(int $offset): mixed {} > > > > // '[["key1","value1"],["key2","value2"]]' instead of '{...}' > > public function jsonSerialize(): array {} > > // dynamic properties are forbidden > > } > > ``` > > > > Currently, PHP does not provide a built-in way to store the state of an arbitrary iterable for reuse later > > (when the iterable has arbitrary keys, or when keys might be repeated). It would be useful to do so for many use cases, such as: > > > > 1. Creating a rewindable copy of a non-rewindable Traversable > > 2. Generating an IteratorAggregate from a class still implementing Iterator > > 3. In the future, providing internal or userland helpers such as iterable_flip(iterable $input), iterable_take(iterable $input, int $limit), > > iterable_chunk(iterable $input, int $chunk_size), iterable_reverse(), etc (these are not part of the RFC) > > 4. Providing memory-efficient random access to both keys and values of arbitrary key-value sequences > > > > Having this implemented as an internal class would also allow it to be much more efficient than a userland solution > > (in terms of time to create, time to iterate over the result, and total memory usage). See https://wiki.php.net/rfc/cachediterable#benchmarks > > > > After some consideration, this is being created as a standalone RFC, and going in the global namespace: > > > > - Based on early feedback on https://wiki.php.net/rfc/any_all_on_iterable#straw_poll (on the namespace preferred in previous polls) > > It seems like it's way too early for me to be proposing namespaces in any RFCs for PHP adding to modules that already exist, when there is no consensus. > > > > An earlier attempt by others on creating a policy for namespaces in general(https://wiki.php.net/rfc/php_namespace_policy#vote) also did not pass. > > > > Having even 40% of voters opposed to introducing a given namespace (in pre-existing modules) > > makes it an impractical choice when RFCs require a 2/3 majority to pass. > > - While some may argue that a different namespace might pass, > > https://wiki.php.net/rfc/any_all_on_iterable_straw_poll_namespace#vote had a sharp dropoff in feedback after the 3rd form. > > I don't know how to interpret that - e.g. are unranked namespaces preferred even less than the options that were ranked or just not seen as affecting the final result. > > A heads up - I will probably start voting on https://wiki.php.net/rfc/cachediterable this weekend after https://wiki.php.net/rfc/cachediterable_straw_poll is finished. > > Any other feedback on CachedIterable? > > Thanks, > Tyson > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: https://www.php.net/unsub.php > Based on a recent comment you made on GitHub, it seems like `CachedIterable` eagerly creates the datastore instead of doing so on-demand. Is this correct?