Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:119835 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 91860 invoked from network); 8 Apr 2023 20:17:18 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 8 Apr 2023 20:17:18 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 082C31804D7 for ; Sat, 8 Apr 2023 13:17: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,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-ed1-f43.google.com (mail-ed1-f43.google.com [209.85.208.43]) (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 ; Sat, 8 Apr 2023 13:17:17 -0700 (PDT) Received: by mail-ed1-f43.google.com with SMTP id 4fb4d7f45d1cf-50479981ed6so1011464a12.0 for ; Sat, 08 Apr 2023 13:17:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1680985036; 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=VTOoqC/BlI0xg9T2moefjwbls4daH20y2KiOCvoj2sA=; b=BdDQdvWbPJA5Prs+LrdwNPp03P0xyuon6kkZuy11sTtLxMx0dimoyfB5GrV7j+/x9n kJwD3nG3MNr+L+zsDBUUcEDtgFLu72HrHP1f5i4NCVXZn+2oI46gWS3WTrEmujzEY/u6 OiRSTXIsbUy2Kwjy2R+GOsILYvHc9r1SE2hk42VdGRfMQyNwKANoxvgxTm0WnHHugQI2 9NnnDpetL4SwXm2bLkV1r3McZipF772op81Cg6xGr7uOz5lmqUcGzuqG8WYlDzTWv+TX mfrOdsCH81k7dz4MMYNojLCJFW1B8kdfLO0mXAga0WwaPYiHEkyEdcIIcj6gpQyYXp8h txPQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680985036; 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=VTOoqC/BlI0xg9T2moefjwbls4daH20y2KiOCvoj2sA=; b=fOk3CQ1PG2H6/bs1TG4+JTXEvhpuPpR77zJjBuKHcKYbdytnBcRRGwxSRHi74DJDLg nviBf8RvIzcJ3ilbO17Y65zWWg2Nlb506DBZbIdtb9uNNGWp1i/QKradIJQx31V6fvMy CxPnRdNaSoQJYegtKL/NXgx6mBeMUUrEByARaVCg+k6FzzMjMAZnQZ+jz2FaqCU/K4Qj hCnwjisojh59G6f5gG0Caz1SJtisTWzNlJGxhC+GirXRiS+lNUL3GV/IFsDK3sN27BCk k0IuudIIWkXvj7EvtLm6ZfDyxLUQjQZj0d9fg3l1KZu6gCr1shSlUQoofN4IvsK5p3he /Mjg== X-Gm-Message-State: AAQBX9dQ+5dCO2MaT5jQYzMYSXF78UgdkIHUt6a1LnnP52ej/6paM+vv jrcwNuS7ely+Nq9tKnsIf6x/gwbjOU4= X-Google-Smtp-Source: AKy350aU6LVhBm4LS8BNZURXcj4gn46KNE/oFif8QG0PQuGJM25NIanCWWHCdKdVvFpwHdgoxu2l9g== X-Received: by 2002:a50:fa97:0:b0:502:9aef:493e with SMTP id w23-20020a50fa97000000b005029aef493emr4517047edr.33.1680985035877; Sat, 08 Apr 2023 13:17:15 -0700 (PDT) Received: from [192.168.0.59] (178-117-137-225.access.telenet.be. [178.117.137.225]) by smtp.gmail.com with ESMTPSA id s13-20020a50ab0d000000b004aeeb476c5bsm3196395edc.24.2023.04.08.13.17.15 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 08 Apr 2023 13:17:15 -0700 (PDT) Message-ID: Date: Sat, 8 Apr 2023 22:17:14 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.9.0 Content-Language: en-US To: "Vorisek, Michael" , "internals@lists.php.net" References: In-Reply-To: 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) Hi On 4/6/23 00:12, Vorisek, Michael wrote: > Hello, > > I would like to open a discussion for https://github.com/php/php-src/issues/10791 . > [https://opengraph.githubassets.com/a23cb565cc8acac6a33ecab5d9ee68a46f046a1ffe215501673156e506695430/php/php-src/issues/10791] > Array spread append · Issue #10791 · php/php-src > Description Currently spread operator can be used for almost anything. But not for array append. I propose the following to be supported: github.com > Appending N elements to an array is quite common language usage pattern and I belive it should be supported natively for shorter syntax, language consistency and performance. > > I am unable to implement it, but I am ready to help with RFC. > > Michael Vorisek > As far as I understand, this is somewhat of a continuation of https://github.com/php/php-src/issues/9881, in which the concern about the array_merge performance was posted. This in turn resulted in this feature proposal if I understand correctly. I think $x = array_merge($x, ...); is probably a common pattern. Creating a new function or a new syntax will make it more difficult for users to know when to use the array_merge approach or when to use the new append approach. I saw the original comments on the array_merge GitHub thread, and read about the optimization difficulty. The reason there's a performance penalty is due the first array always being copied because: - it may be modified (i.e. keys made sequential) - it may be shared in more places However, I think it might be worth adding an optimization for the most common $x = array_merge($x, ...) case. To do this: - we need to check if the array is sequential. For packed array this can be done with HT_IS_WITHOUT_HOLES(). - we need to detect that coding pattern. This can be done by checking if there's a ZEND_ASSIGN after the call that assigns the result to the first passed array variable. - array must not be immutable - array must have refcount 2: once as variable and once as argument I implemented this optimization in the following commit on my fork: https://github.com/nielsdos/php-src/commit/269d547043c9d3c59d7c9d773eb1ef174dcc5c44 For now it's limited to packed arrays, and it's only a proof-of-concept. I'm also not sure if it's 100% correct, I'd need to do some more debugging / testing to know for sure, but I'm short on time now. The performance improvement for the array_merge case in https://github.com/php/php-src/issues/10993#issuecomment-1493166391 is big: 1.32s without my patch, vs 0.008s with my patch 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. Kinds regards Niels