Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:119882 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 89229 invoked from network); 10 Apr 2023 20:57:24 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 10 Apr 2023 20:57:24 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 907151804A9 for ; Mon, 10 Apr 2023 13:57:23 -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.6 required=5.0 tests=BAYES_00,BODY_8BITS, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, NICE_REPLY_A,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE 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-wr1-f48.google.com (mail-wr1-f48.google.com [209.85.221.48]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Mon, 10 Apr 2023 13:57:23 -0700 (PDT) Received: by mail-wr1-f48.google.com with SMTP id s2so2277060wra.7 for ; Mon, 10 Apr 2023 13:57:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1681160241; h=content-transfer-encoding:in-reply-to:from:references:to :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=Nn8UhLePCCjbm4nCGNmTKLx1YXWub1tKqqBbSryXoDY=; b=BYHnDCS8AHHjCNrIAGG06A7H/UEu4fGjRecOxp+JHE7UZ/tlU3dTuhRSJC3L4SIJra Pl68e40ft3rdB1oOsS7xCpZPcvkc/ZaNDscvFECpMgerrlelLvH5c+2YM91ycsJPTw6v NiZZFlmemSOGI+fYk+DHXYHB5YqvRtO9sPPRWHlwqKyRL0YUHleDr/TUR2pkkb4E7ouN 6XTMNc0z77W6QZ/B2P2ecShX8ATJ+RrKlGvuCxWC/7u+PMO0Y6NxdP9/5LEI9diwZimY LEAFE8vlNFlsncYokrMG/hpg/ydqx4GL18rRWHty3IYzDm/DRdCooyoc7oeHNNDIb7xn 0xog== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1681160241; h=content-transfer-encoding:in-reply-to:from:references:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=Nn8UhLePCCjbm4nCGNmTKLx1YXWub1tKqqBbSryXoDY=; b=J/dxcLAJjcOudo2uMH/K5qjJAzBPl4OWXRv2rvpQUHXX2lIBMiw/Jjqj59BIBVzSdU AegblQAa7Y16/evnetXGf6yFBibBy9whaThatUkKsTqw4MR/qtDO0Vmx4TyW3kd7Rvft 2Ya68pgMjgjIq/1m2wurO4pQPSH14lyjTnFoH9Eq46C3/M+2IpYCDX2BWSggRVeczkGb qIpEqdV8axzXfFq6Hi+S7/f4wOZ0nLf8fKxsYaL34+ube/VI/GNIdQcDvq2fLN9BH8TO F74badF61r1u8G18eQoSDqolKH84a6SHQwP1G7oCRVIpbE8QmU+8JZWxUnajdzcEN1vZ tCzw== X-Gm-Message-State: AAQBX9fVq8HdglWf9WgbvRupoyO62VljYcKSBiDtVA3sBy+V/lMiofri hbv+5OMIRcFpVGJMNRPlTDsWS5i2df0= X-Google-Smtp-Source: AKy350bVjzpV4Sb0ncixtKvmXfgqA8+SAM6G0CEA2AcVqutw0T4PCVizd0WynZHATdyJd+oHaVP3JQ== X-Received: by 2002:a5d:55ca:0:b0:2ef:19f4:823e with SMTP id i10-20020a5d55ca000000b002ef19f4823emr365189wrw.49.1681160241446; Mon, 10 Apr 2023 13:57:21 -0700 (PDT) Received: from ?IPV6:2a02:1811:cc83:eef0:7bf1:a0f8:a9aa:ac98? (ptr-dtfv0pmq82wc9dcpm6w.18120a2.ip6.access.telenet.be. [2a02:1811:cc83:eef0:7bf1:a0f8:a9aa:ac98]) by smtp.gmail.com with ESMTPSA id z3-20020a5d4c83000000b002e55cc69169sm12594534wrs.38.2023.04.10.13.57.20 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 10 Apr 2023 13:57:21 -0700 (PDT) Message-ID: <23effc93-de97-9cfc-f00d-b672868350e3@gmail.com> Date: Mon, 10 Apr 2023 22:57:20 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.9.1 Content-Language: en-US To: =?UTF-8?Q?Tim_D=c3=bcsterhus?= , "internals@lists.php.net" References: <8c615ee5-a1a1-d760-ca92-65f0fd4c5b79@bastelstu.be> In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Subject: Re: Allowing $a = foo($a) to operate in-place (was Re: [PHP-DEV] Array spread append) From: dossche.niels@gmail.com (Niels Dossche) Hi On 10/04/2023 22:11, Tim Düsterhus wrote: > 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). > Right. Thanks for pointing this out! I completely forgot about this. That's unfortunate... So far only the array_unique optimization will have trouble with this. I added a check now that checks if a user handler is installed. In any case, if anyone is interested, I created a PR against my fork (https://github.com/nielsdos/php-src/pull/5) where I'm tinkering with the optimization idea. > 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. > That's unfortunate. Then it does indeed seem like the optimization will not work well for your use case :/ > Best regards > Tim Düsterhus > > [1] For a future major we *might* be able to make a working CSPRNG a hard requirement. Kind regards Niels