Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:115583 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 64627 invoked from network); 25 Jul 2021 21:08:25 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 25 Jul 2021 21:08:25 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id DD4021804B0 for ; Sun, 25 Jul 2021 14:34:58 -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.2 required=5.0 tests=BAYES_40,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS 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-yb1-f179.google.com (mail-yb1-f179.google.com [209.85.219.179]) (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 ; Sun, 25 Jul 2021 14:34:58 -0700 (PDT) Received: by mail-yb1-f179.google.com with SMTP id a201so11577030ybg.12 for ; Sun, 25 Jul 2021 14:34:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:from:date:message-id:subject:to; bh=cgeoXhkc5dZz4J5X2KR+qRFdDuoLMoYsHDSqtuDcf4Q=; b=HxlohtkRq+17m5E/VKfL5dsJdk8H/il1ldhJYHqUvHhyNBVRjgu6xtIjm7+g9RFE9/ +l399U0rq/OdhjIqpjY3U0sb303j0DADhrso3cTGQHI/vH0JTalicxlnxX6dVoXBMjZa 4or3kKGXfNl/zqMMIwXJWecySK9Se8FkVoND+rRyr41jqM6px05W8DSZPFzIJtdnXWN6 1KUJRtvIYl1BE9wRvLSHrHa8zotKTVKl+40EV30Ux6ebDf7CwRVjYJfuRHHY7U0q7jiP s/OVymS+FcaVkXm5PySolMuL4SOd2FfzDNO4vGVGKPojn94FHIL7FcPQQHIZ3tWLXSOq +P9Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=cgeoXhkc5dZz4J5X2KR+qRFdDuoLMoYsHDSqtuDcf4Q=; b=Pe8UQNp7+gtH9S1yyEK6GU69ef+dzBzoyscSgKSxB/eUxfpZWpj+derMOXZoncgb2n kXHtpMoMfuLjISs85+sjAqSE0CDZWD2P3TBAc/IHd7mtnkY+vSY/pVRiPVuUrqGLkVxS buTfWoSUMBLNRLCy2XR1K18trtv4BTh27fRgnFUdce/j6kPhViTMdpRW6uxL0W7NO+35 hIbudqlcFH7r72ic8u2XU9n1eCAy9f/1kLizLKwsQwATYw+E70yNSNAKhbVschXmsRHu z9T+A6sVb4vVDuS2upkGtqoaZdm/bgsf7DsXD+z9lYQD51+oM99hIDYTbIm66KAB8FMl HGsg== X-Gm-Message-State: AOAM533RGRzH1k5aOP1GA9bOwr9udLPrj38xkzkjNZcDXHHlIrCNiqwj f5PSAkadMVK1mNWD4/rWyPyszr53sNo9bM+1jjY6goeGf1HPhA== X-Google-Smtp-Source: ABdhPJyDjM2XAkVxFY4avpdR2AgNWalhbouSHclr6cqDqyL8bdxuq/zp48lHRuQr0CDUJjE/fGrHyLjAUOXysWzxj7Q= X-Received: by 2002:a25:b10f:: with SMTP id g15mr10052444ybj.516.1627248896833; Sun, 25 Jul 2021 14:34:56 -0700 (PDT) MIME-Version: 1.0 Date: Sun, 25 Jul 2021 18:34:45 -0300 Message-ID: To: PHP Internals Content-Type: multipart/alternative; boundary="00000000000074fde805c7f96837" Subject: array_merge() inside looping optimization From: david.proweb@gmail.com (David Rodrigues) --00000000000074fde805c7f96837 Content-Type: text/plain; charset="UTF-8" Hi! Using array_merge() inside a loop can greatly impact performance. From the tests I did, doing it in the middle costs 250% more than doing it later: - Inside: for(...) $x = array_merge($x, $y) - After: for(...) $x[] = $y; array_merge(... $x) Even using array_push() doesn't seem like a good alternative as it costs 140%. https://pastebin.com/s7f4Ttm3 https://3v4l.org/Vqt7o (+83% for array_push(), +760% for array_merge() inside) As far as I know, the array_merge() function makes a copy of the array, rather than updating the input array. It makes sense, but in some situations, not so rare, it might be possible to optimize when the destination variable is part of the argument. $arr = array_merge($arr, [ 1, 2, 3 ]); // or $arr = array_merge([ 1, 2, 3 ], $arr); This could reduce processing cost and memory consumption, as it would no longer be necessary to copy the array to generate another one. Anyway, I don't know if this is actually possible, or if the cost-benefit would be better than what there is today. But I believe it is a path to be thought of. Atenciosamente, David Rodrigues --00000000000074fde805c7f96837--