Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:98519 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 72782 invoked from network); 14 Mar 2017 16:22:43 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 14 Mar 2017 16:22:43 -0000 Authentication-Results: pb1.pair.com smtp.mail=julienpauli@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=julienpauli@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.213.43 as permitted sender) X-PHP-List-Original-Sender: julienpauli@gmail.com X-Host-Fingerprint: 209.85.213.43 mail-vk0-f43.google.com Received: from [209.85.213.43] ([209.85.213.43:34663] helo=mail-vk0-f43.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id B3/E5-38004-1D818C85 for ; Tue, 14 Mar 2017 11:22:41 -0500 Received: by mail-vk0-f43.google.com with SMTP id r136so54740109vke.1 for ; Tue, 14 Mar 2017 09:22:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:sender:in-reply-to:references:from:date:message-id :subject:to:cc; bh=9xOKGG+19xZcYBdQTd6SAH6+JCjvcVvhL8V7rZrC8AU=; b=jIdlwn3k6znDzXEbn4AwNRBrvVT/3MnUhMfWNCZ+MMfOBN8sYwQu8snJC4wBkTOKGt Ov74KW5YrfmlcbqpO4c/H3NxQbQr27mmPa3wivvhFUUYk7Z1CjCM9k236s7FePW3k/qc ighLNAskHw5DLzKzbYl/DSmxXYkeoXqPxFYzPwRCosb1nhxR4y/lfOPHRKRI5KsEmgwB SCaDD539uGmrLhf4onWHBFzkNyQ3ZT9JHc3YbcAsUgb5x3mAAp+W7pnxFDie4nN4n34h TDQ29HPrgG75KuCpcr3ubH3pxY39/oSFdzYX+/QP6YcMISSbeQLmIAaGJlQFnVWc/UFc MBOA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:sender:in-reply-to:references:from :date:message-id:subject:to:cc; bh=9xOKGG+19xZcYBdQTd6SAH6+JCjvcVvhL8V7rZrC8AU=; b=IBrhUAb/EWK4m4xG5dkdaTpnJsm8bpDz/gAwvtWWjZpQJryvihA1r//ysum14PWg9H k0grn3ahNqj97hU42qxBu7vsBKfQpQiYNmvfULVe76iPLOQDPxtUQX9dwymqSWAHHROd GN/EJGyGHQnT1I+9BXEktOWD+BD//5QOVxR2ea+OtzezpAL2wX4F2JBIB/+32q+Y80+k LRkj3aW4uMwyCzL16mhGhFPCCtwKW7Bdfbjd25RVTnHIC7RBahRi3Sdt4MAcGpU6XwH5 QsQo52fIvigPBNMdkL6RyDBWcF9DoS2gSLuFa7twAxva2hohOsX3V5UWfSA825HEZ9N9 Odnw== X-Gm-Message-State: AMke39n/bNtPenHVOi9SSGPKd/liza62jhj2m7Y11orzXiaxRv6h8ij6jODVQq/qYniwEiCFyrS5Ik24OqdJQw== X-Received: by 10.31.197.196 with SMTP id v187mr17240307vkf.84.1489508558153; Tue, 14 Mar 2017 09:22:38 -0700 (PDT) MIME-Version: 1.0 Sender: julienpauli@gmail.com Received: by 10.176.83.113 with HTTP; Tue, 14 Mar 2017 09:21:57 -0700 (PDT) In-Reply-To: References: Date: Tue, 14 Mar 2017 17:21:57 +0100 X-Google-Sender-Auth: Kx5uN7JW6-x9ZjampuXtnJV0-kI Message-ID: To: Jesse Schalken Cc: PHP internals Content-Type: multipart/alternative; boundary=001a114e758a7faf3d054ab33c01 Subject: Re: [PHP-DEV] array_values should be a no-op for packed layout arrays From: jpauli@php.net (Julien Pauli) --001a114e758a7faf3d054ab33c01 Content-Type: text/plain; charset=UTF-8 On Mon, Mar 13, 2017 at 6:34 AM, Jesse Schalken wrote: > I noticed this commit > 9e853282fac968d9cd454> > recently in HHVM, which makes array_values() return the same array back if > it's packed instead of building a copy. > > My understanding is PHP 7 also has packed arrays like HHVM, and my reading > of PHP's array_values() implementation (here > 29cfb16a55196dc43/ext/standard/array.c#L4000>) > is that it always creates a copy of the array, even if it's packed. > > Would this be a worthwhile optimisation to make in PHP as well? > That would be nice if array_values() wouldn't reindex the array. But it does. $a = [12=> 'foo', 42 => 'bar']; // packed array $b = array_values($a); $b // [0 => 'foo', 42 => 'bar']; // keys have been changed and are not kept from source So, array_values() must allocate a new array, and fill it in with the values of source, because array_values() reindexes the source array from key 0 ; whatever was the original array. Note that the allocation is very light, in PHP 7, we allocate a full buffer at once , and not one buffer for each slot. The performance inpact of such an allocation is really really tiny. Julien.Pauli --001a114e758a7faf3d054ab33c01--