Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:114792 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 4363 invoked from network); 9 Jun 2021 04:34:33 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 9 Jun 2021 04:34:33 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 83A461804DB for ; Tue, 8 Jun 2021 21:49:28 -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-f174.google.com (mail-yb1-f174.google.com [209.85.219.174]) (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:49:28 -0700 (PDT) Received: by mail-yb1-f174.google.com with SMTP id c14so4589592ybk.3 for ; Tue, 08 Jun 2021 21:49:28 -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=yEnjzwOoVnhNXIBALM03R/aSy8mjYogONap187Zsoz8=; b=gKUvggcNrM9IMqrFnEDDCnpM3SugQn8h8ddGe1VUJq4gnArIZJhg9iW3QaFJsct79c JDXx2ea9bBGqCHHt4BOPHh1a3mjZQbqDjlpNrd7f9IwS00pZQz5GkzER+/FOYzsep5tD PjVGG6Ayst3bQmOV7DNe/RK7B7dZdgJHin69Y= 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=yEnjzwOoVnhNXIBALM03R/aSy8mjYogONap187Zsoz8=; b=ew3EpKCJ+yNbjF9l/3ISf4Xg4Z3nJGJIQ2fxR1Jyincd8uqv0BxzftkWacET+H8qYm pDDlIidYxcKvo2IO9xV0bVxn/ziXfxhxglfckNO8oXS2pwaIfn3m524OxaA6czS0mi1J XFvHwcKH5tEKc9w/+KS233siDz3E4EyEZxaCZhQO5AgVt1/X0W8SJs2hfJSoi7+VnrNg +a6wZG/vGfCvckeUO4pRRKfg9D8HGsbCjGtinKeFCUiYmpDU8OyqneGe2WPDB1EmLj1e 0it3c8/WaKF+4edZp5x8SYbZ7FSvBZIWTKd6tbWPlYN/8n8u/YUzUSa99BZQ18aWsg4D B7yg== X-Gm-Message-State: AOAM530j0CTNNVRBiH4DHVD8M81Q73ALKtGh+olGrdRmfxeJ6D81doBh pUhhNNKwfUdV6ey+6gE8fDVFh3EX3bOTCK1w6mrIBQ== X-Google-Smtp-Source: ABdhPJwtzd+mXqAajJi9BiJsaW0JGXlO6BPjsgCE9X6awvqtICI4SWaQfLSohqXTmRBIpGTQtUzV+2XNU69xV5upDmg= X-Received: by 2002:a05:6902:50b:: with SMTP id x11mr35355452ybs.133.1623214167543; Tue, 08 Jun 2021 21:49:27 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Reply-To: Levi Morrison Date: Tue, 8 Jun 2021 22:49:17 -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 10:47 PM Levi Morrison wrote: > > 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? Sorry, yes, that's correct and pointed out in the RFC. I think that's a significant implementation flaw. I don't see why we'd balloon memory usage unnecessarily by being eager -- if an operation needs to fetch more data then it can go ahead and do so.