Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:100973 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 80130 invoked from network); 27 Oct 2017 21:11:27 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 27 Oct 2017 21:11:27 -0000 Authentication-Results: pb1.pair.com smtp.mail=ben.coutu@zeyos.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=ben.coutu@zeyos.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain zeyos.com designates 88.99.153.70 as permitted sender) X-PHP-List-Original-Sender: ben.coutu@zeyos.com X-Host-Fingerprint: 88.99.153.70 mx.zeyos.com Received: from [88.99.153.70] ([88.99.153.70:52978] helo=mx.zeyos.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 91/81-28573-CF0A3F95 for ; Fri, 27 Oct 2017 17:11:26 -0400 Received: from mx.zeyos.com (localhost [127.0.0.1]) by mx.zeyos.com (Postfix) with ESMTP id 06BF55FAFE for ; Fri, 27 Oct 2017 23:11:22 +0200 (CEST) Authentication-Results: mx.zeyos.com (amavisd-new); dkim=pass reason="pass (just generated, assumed good)" header.d=zeyos.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=zeyos.com; h= content-transfer-encoding:content-type:content-type:mime-version :to:subject:subject:from:from:date:date; s=dkim; t=1509138681; x=1510002682; bh=Q9IhE4I3wlR4Elf1dm/JwZi9KVFng0sL3BUmbELFX+w=; b= RYhNC8hVFAK+/Cdz5RameErrw0RB1rSRtPTpo4nJPKBjsf+bTDjhaPfwegl8/mYt 7EWtdRyNfIJDAWDQu1Xw3mkC/ar8657yUlVqPL8uty1q2vlwI5RjOdyIeAiBtbX/ cXh/rF5jiVZxajFmglEUCdi7Ldy7OCl2wAze2Vfa69U= X-Virus-Scanned: Debian amavisd-new at mx.zeyos.com Received: from mx.zeyos.com ([127.0.0.1]) by mx.zeyos.com (mx.zeyos.com [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id Ylccox9wQTdg for ; Fri, 27 Oct 2017 23:11:21 +0200 (CEST) Received: from [81.171.8.203] (unknown [81.171.8.203]) by mx.zeyos.com (Postfix) with ESMTPSA id B163C5FA78; Fri, 27 Oct 2017 23:11:20 +0200 (CEST) Date: Fri, 27 Oct 2017 23:11:20 +0200 To: Sara Golemon , Nikita Popov Cc: PHP Internals , Dmitry Stogov MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Message-ID: <20171027211122.06BF55FAFE@mx.zeyos.com> Subject: Re: [PHP-DEV] Apply substr() optimization to array_slice() From: ben.coutu@zeyos.com (Benjamin Coutu) Yes, abstracting such additional checks through something like zend_hash_du= plicate() would make sense, but IMHO it should be named differently, e.g. z= end_hash_copy_lazy. So to be analogous to zend_string_copy(), but not to be= confused with the existing zend_hash_copy().=0A=0ABy the way: array_pad() = in L4320, array_unique() in L4476 and array_diff() in L5415 also return the= array without those kind of checks (analougous to what I have proposed for= the array_slice() case). These existing bugs would have to be fixed as wel= l.=0A=0A- Ben -=0A=0A=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D Original =3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=0AFrom: Sara Golemon =0ATo: Nikita Popo= v =0ADate: Fri, 27 Oct 2017 18:34:15 +0200=0ASubject:= Re: [PHP-DEV] Apply substr() optimization to array_slice()=0A=0A>=20 >=20 > On Fri, Oct 27, 2017 at 12:10 PM, Nikita Popov wro= te: > > On Fri, Oct 27, 2017 at 4:16 PM, Sara Golemon wrote: > >> > >> On Fri, Oct 27, 2017 at 8:12 AM, Benjamin Coutu > >> wrote: > >> > Now, array_slice() could be optimized similarly, but currently > >> > (unless the resulting array is expected to be empty) we always > >> > create a new array no matter if we actually have to. > >> > > >> Pushed, with an additional escape hatch for vector-like arrays (which > >> are implicitly like preserve_keys). In the future though, please use > >> the PR process. Thanks. > > > > > > Unfortunately these optimizations are subtly incorrect in the current f= orm, > > because arrays have a bunch of additional hidden state. See > > https://bugs.php.net/bug.php?id=3D75433 for a (not yet fixed) issue tha= t > > resulted from similar optimizations in 7.2. We'll have to review all th= e > > places where we apply optimizations like these and make sure that we're= not > > introducing incorrect behavior wrt the next free element or internal ar= ray > > pointer. > > > It took awhile to unwind the repro case given in the bug report, but > this seems to be a simpler example: > https://3v4l.org/rqphD >=20 > Basically, that next insert index for the output of array_values() > should be reset, and with the optimization it's not. >=20 > For array_values() the quick and dirty fix is adding "&& nextIndex =3D=3D > num_elements" psuedocode. In the case of array_slice, the same will > be true, so I agree we should be careful about applying such > optimizations. >=20 > I'll clean up these uses now, and would suggest something like: >=20 > zend_array* zend_hash_duplicate(zend_array* input, zend_bool > preserve_keys); type API which can be a certral place for making that > kind of short-circuit versus slow-copy decision when called from > places like array_values() and array_slice(). >=20 > -Sara