Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:119875 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 73669 invoked from network); 10 Apr 2023 19:50:48 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 10 Apr 2023 19:50:48 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id CA08518037E for ; Mon, 10 Apr 2023 12:50:47 -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,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-f45.google.com (mail-wr1-f45.google.com [209.85.221.45]) (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 12:50:47 -0700 (PDT) Received: by mail-wr1-f45.google.com with SMTP id e2so5358695wrc.10 for ; Mon, 10 Apr 2023 12:50:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1681156246; h=content-transfer-encoding:in-reply-to:from:content-language :references:to:subject:user-agent:mime-version:date:message-id:from :to:cc:subject:date:message-id:reply-to; bh=uaOlqE64WCqjwqAqDF5CTC3P9kccMyK0pHKyylhtDA4=; b=GUZhOtNrdyUCgBrdA5a3lHNbz9QzWy3iMmSj+/FC/TRGSd/d5qXt89Rq+hSxeNB0d2 oL7nHFG39UljvCRxm5CkS/K/4VORikIDfSdqR60r1c0JpQKfnNLRHqx7j5iZVPwS4F6g YJMQo9ySf99SJS9xeiu0L6vjIYots2fqDllCzDRSVe9+u6Eht9gXdPQFD6tmHLTZEHuf ohZcVgyplnHjVRMqIW/RdGamxC3I7/KIWT8PpHrEZyGdGlphNsyATlHAUWoULPVfG1jv tjSXSMqFl0sNbyqPUjFQyJaTB9fbivABMRpDWsuIsf2F60EFvIRiknm03do4X984r4L6 f7lA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1681156246; h=content-transfer-encoding:in-reply-to:from:content-language :references:to:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=uaOlqE64WCqjwqAqDF5CTC3P9kccMyK0pHKyylhtDA4=; b=gjMpUC/ynJjN93czHovxZ6HoFLWqEmc6KlFky4857nNc6JG4paiCkUPvqUPitNHBE9 Zxn+XQysXOTVy2/526yS/G7IvD4n5C9LYqkewJ/GT4kSIG4UkHfFoa3JBTkDtv5lRRAC 64pyu7QBzC5B/HsvMX8wHSMELUma1r1vfM++5zOpCCU0JWexZpuRRVb6C60xzdlbuwsi oXZg5P4j/R6wJpqBN0TQCgzwitMDkh/iLm3K6Tv57VL491oQZ5Ge+pk8ExD/hnJ/9UFP atbKal4QilRW2pktIkLEcjrv0710vEHr1/qgdAkSGLPTN6+AtWkoEkTksVcfuesr7aag IClg== X-Gm-Message-State: AAQBX9fH89bpMvakB117PV11iJxgDpFDR0RCytVBpHTOJo3BP7Puy0UA 5sME1e6qvXrWTBsMyC9xLT4= X-Google-Smtp-Source: AKy350aBpE0D/uryOLhiA5z7JBAn21hUSmqVMrz19E/q5EoKHAufacSmyQvCJCaJSBARSNEnBpx5RA== X-Received: by 2002:a5d:4d47:0:b0:2cc:1935:1ae8 with SMTP id a7-20020a5d4d47000000b002cc19351ae8mr187454wru.47.1681156246009; Mon, 10 Apr 2023 12:50:46 -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 m8-20020adffe48000000b002c55521903bsm12584602wrs.51.2023.04.10.12.50.45 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 10 Apr 2023 12:50:45 -0700 (PDT) Message-ID: Date: Mon, 10 Apr 2023 21:50:44 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.9.1 To: =?UTF-8?Q?Tim_D=c3=bcsterhus?= , "Vorisek, Michael" , "internals@lists.php.net" References: <8c615ee5-a1a1-d760-ca92-65f0fd4c5b79@bastelstu.be> Content-Language: en-US In-Reply-To: <8c615ee5-a1a1-d760-ca92-65f0fd4c5b79@bastelstu.be> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Subject: Re: [PHP-DEV] Array spread append From: dossche.niels@gmail.com (Niels Dossche) Hey Tim On 10/04/2023 14:45, Tim Düsterhus wrote: > Hi > > On 4/8/23 22:17, Niels Dossche wrote: >> I think this could be made more generic, and be cleaned up. >> But I don't know if something like this is desired in PHP. > > Yes, please. I believe that “performance” should not influence API design if it can be avoided. Instead the heavy lifting of optimization should be done by the engine (see also: https://externals.io/message/118896#118921). > I agree. > 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. 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. > Best regards > Tim Düsterhus Kind regards Niels