Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:119876 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 75846 invoked from network); 10 Apr 2023 20:11:20 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 10 Apr 2023 20:11:20 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 6B91C18037E for ; Mon, 10 Apr 2023 13:11:18 -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,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS24940 176.9.0.0/16 X-Spam-Virus: No X-Envelope-From: Received: from chrono.xqk7.com (chrono.xqk7.com [176.9.45.72]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Mon, 10 Apr 2023 13:11:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bastelstu.be; s=mail20171119; t=1681157476; bh=kvhxy/XhBPp9isIu1iAhd211qGtWc8N6Fw2q6cKreR0=; h=Message-ID:Date:MIME-Version:Subject:To:References:From: In-Reply-To:Content-Type:from:to:cc:subject:message-id; b=PmEkzdN9vLSi803D2uQaS0qJGf7Py4ZcBNPtf8+nm6mMFy1OVcJTJjeE/6v59uzN5 YXhePjQz9c78H738+gvjUhATaS2ZXC/a9uHMk9xJWxSyc4PRgi00ip6Bnh7/tR3Nj2 e9l1KZ2FrCJCcLV/zBDrIejAgofY9npuahYjcV1zx1YGv+v3Z/sfdgHDpNfiSZOJIv tbROXY3WG6g2RnJayNeJhv97QtigmRBN0IsSLjTEcxZaayL5u0ys2hFW5EtFG3DWwJ ussFfLlG6+jQLGi1RKqdZohEMQepBZhgbB0paIj58BXrmoqw17JIwQ9CmAKnr2EmUr rdbyCqDohHNsg== Message-ID: Date: Mon, 10 Apr 2023 22:11:15 +0200 MIME-Version: 1.0 Content-Language: en-US To: Niels Dossche , "internals@lists.php.net" References: <8c615ee5-a1a1-d760-ca92-65f0fd4c5b79@bastelstu.be> In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Subject: Allowing $a = foo($a) to operate in-place (was Re: [PHP-DEV] Array spread append) From: tim@bastelstu.be (=?UTF-8?Q?Tim_D=c3=bcsterhus?=) Hi On 4/10/23 21:50, Niels Dossche wrote: >> The suggested optimization of "the input is overwritten with the output" would then also allow to avoid introducing reference parameters just for optimization purposes. The sort() family comes to my mind and also the shuffle() function. Randomizer::shuffleArray() already returns a copy and thus would benefit from the proposed optimization for $a = $r->shuffleArray($a). >> > > I did extend my optimization since the first time I posted it here. > It can handle two cases: > - $x = array_something($x, ...) like I previously showed with array_merge > - $x = array_something(temporary_array_with_refcount_1,...) which is new > > There is one caveat with the first optimization: it is only safe if we know for sure no exception can happen during array modification. > Because, if an exception happens during modification, then the changed array is already visible to the exception handler, but this isn't allowed because the assignment didn't happen yet. > This "exception problem" does not happen for the second optimization though. I see. That makes stuff certainly more complicated, because an exception can also arise in an error handler (i.e. for any warnings and notices). It's also not just about visibility to the exception handler, but also "incomplete modifications" in cases like these: try { $foo = something($foo); } catch (\Exception) {} // $foo might or might not be fully modified. > So I looked if it was possible to do the optimization for shuffleArray. > It is only possible for the second case, because I see some EG(exception) checks inside php_array_data_shuffle(). > Unless we can determine upfront which random algorithms have an exception-free range function. For the internal engines this is easy (when ignoring extensions): - Mt19937 - PcgOneseq128XslRr64 - Xoshiro256StarStar ... are all infallible. - Secure ... is fallible (it doesn't fail in practice on modern OSes, though [1]) Any userland engine (engine_user.c) can do anything it wants, of course. Unfortunately with Secure being fallible, this optimization is of little use in practice. The same would likely be true for a non-reference sorting function, because of incomparable values and userland comparison handlers that can all kinds of unsafe stuff. Best regards Tim Düsterhus [1] For a future major we *might* be able to make a working CSPRNG a hard requirement.