Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:98520 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 74943 invoked from network); 14 Mar 2017 16:48:33 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 14 Mar 2017 16:48:33 -0000 Authentication-Results: pb1.pair.com header.from=php@golemon.com; sender-id=softfail Authentication-Results: pb1.pair.com smtp.mail=php@golemon.com; spf=softfail; sender-id=softfail Received-SPF: softfail (pb1.pair.com: domain golemon.com does not designate 74.125.82.51 as permitted sender) X-PHP-List-Original-Sender: php@golemon.com X-Host-Fingerprint: 74.125.82.51 mail-wm0-f51.google.com Received: from [74.125.82.51] ([74.125.82.51:37716] helo=mail-wm0-f51.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id AB/36-38004-DDE18C85 for ; Tue, 14 Mar 2017 11:48:30 -0500 Received: by mail-wm0-f51.google.com with SMTP id n11so3868862wma.0 for ; Tue, 14 Mar 2017 09:48:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=golemon-com.20150623.gappssmtp.com; s=20150623; h=mime-version:sender:in-reply-to:references:from:date:message-id :subject:to:cc; bh=PgZGCmRvOrdl/sK6jsi4sATHUB+lLigbKhVvG39SoJY=; b=yUDDvZc2qkYEfAZeMDvF4v27UWtW6VEPMKZFwhoid+uI1Blrcnnoukk4JYCDBE6yvz 0FwbM8rnSFdXtRgvYyYDsQsy6w50cvonb07XW/prC75pF+k5Xt7EDOT3GsLfISWKQ4pj g6QuqFvDrI0WTmxvfoUvOiKY5Yvbj5sGc+r9w0Udvg6WeW5PAdWxJy7+1Qjjn5A1BNEa c/TIvOBNeufeNVpw6VM9O0jdA+n75u5x6dlHwP4wKZ/4aM7Hwsq1hqujDZc+RdRwcUDv 6AmOtb472/kN7RoJshbWKC6TumRzVMY8VDR+9PkZWPyHDJdtdOd/l7vRpKlCmtlzLYi5 OS3A== 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=PgZGCmRvOrdl/sK6jsi4sATHUB+lLigbKhVvG39SoJY=; b=k65jcl7pb+UP7h/oWT7OgM7IviRDxdCkU0ygNcUnhYwVK1mAF7CoyR8+OzN1Krl1GT qx6l/PG81mZwbKxc3IbL23D/KSpSwiKbZHEFLCzSH/J+2fcoPU5KFNbfgycp7CsmhRAP auAx9/nqMKCqrUrZZ1lJK33tUTK/l7K38almwHDJIBHYezuz7PR6pCgcVnC2SuWdHN8P dboRZFgSafOT3A7PEIXkq7PpG+9OdL2UYO0aKjZEC41t6JwAeOCW8A/o4XdmThAu3zDk 5cpWZ8wb9AeomuGYKL5ojssaSFnndsdl/lLjl4xtry5AfTc76LWBk+fQQBfn61/eJ3Bm V4EA== X-Gm-Message-State: AFeK/H3WEoORdIcMgpKkmZt90cnhSZGC/wESWbeLGe3CS//kv+tme575c++4SMcTUjodi6a5gVHKmU7z7j4nXg== X-Received: by 10.28.215.18 with SMTP id o18mr16374251wmg.98.1489510107221; Tue, 14 Mar 2017 09:48:27 -0700 (PDT) MIME-Version: 1.0 Sender: php@golemon.com Received: by 10.223.152.213 with HTTP; Tue, 14 Mar 2017 09:48:26 -0700 (PDT) X-Originating-IP: [73.9.224.155] In-Reply-To: References: Date: Tue, 14 Mar 2017 11:48:26 -0500 X-Google-Sender-Auth: xVPgbqLe6WWf1yMqPdHYdMju1nY Message-ID: To: Julien Pauli Cc: Jesse Schalken , PHP internals Content-Type: text/plain; charset=UTF-8 Subject: Re: [PHP-DEV] array_values should be a no-op for packed layout arrays From: pollita@php.net (Sara Golemon) On Tue, Mar 14, 2017 at 11:21 AM, Julien Pauli 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. > Minor nit: [12=>'foo', 42=>'bar'] is not a packed array. [0=>'foo', 2=>'bar'] is however, so your primary point stands. However it should be simple enough to detect when a packed array is also vector-like (indexed from 0 to n-1) and make this minor optimization. -Sara