Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:100970 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 66452 invoked from network); 27 Oct 2017 16:34:21 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 27 Oct 2017 16:34:21 -0000 Authentication-Results: pb1.pair.com smtp.mail=php@golemon.com; spf=softfail; sender-id=softfail Authentication-Results: pb1.pair.com header.from=php@golemon.com; sender-id=softfail Received-SPF: softfail (pb1.pair.com: domain golemon.com does not designate 209.85.216.176 as permitted sender) X-PHP-List-Original-Sender: php@golemon.com X-Host-Fingerprint: 209.85.216.176 mail-qt0-f176.google.com Received: from [209.85.216.176] ([209.85.216.176:45913] helo=mail-qt0-f176.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 19/30-28573-A0063F95 for ; Fri, 27 Oct 2017 12:34:20 -0400 Received: by mail-qt0-f176.google.com with SMTP id p1so9129838qtg.2 for ; Fri, 27 Oct 2017 09:34:18 -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=JPpXxDWEUgZs+aBw7YFOiWdnJnf/e2K8v3/mjvEfwQE=; b=myKMWhCJPCpWPidDcdFpZT1xK2w68prlD+GiUM9RXNFojqwHRbuP4mbnpnp1+eI0+N sSF3f+epHJyUEl49fZAvQMk7Y1ZAS1mTpBGQUA3jWMgZ9HewKIo/fnvdlZlSoPfkua8y 1SYdO3D5BoDMUSBRN5teMMmIeE/N2zEv1MfBfoFysT3uTXkxqkm5kz/U/ehtUmeMhIZ+ JCVxvJqpCUMi/ujoOHXupNAVORupLUKNHsNVJxHM9cPEMARR+U8r/tH+F6aEBE3Ugx9a MbWo5Z8OUJOzJ81U7yL555hxsqTuaqithZfEinbYHCy/iznDVeuMCEO/KB1hnRHpIWvS PKDA== 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=JPpXxDWEUgZs+aBw7YFOiWdnJnf/e2K8v3/mjvEfwQE=; b=g1+5pw2TQLoG6Ks9yBNPxDG5D3bC35IC2tN5jLFeDij2XFLZWM8vwrVQd5iV3TQTQ0 wMJnd9+gLCO6JrujINxVI7Fc4n1wKb1/7yWq6gZ2UpXAZlGhOsqLyKe50ewb5hQMXNnK m182jUzgNzdiCf1aM6vnr/35+iyM1EpUMVPpznB2Ha3VPzNvkBCtoyz+AroXoeIcN7Ki o2fwlL2j3Z79J3kEUunP0hdx/s/fYs+ugp4qnMeeE2bb7/lzIjmiPNQBJML7IOVCJ2H8 3tm2Ycs94Uq+t7424oCHLk1bDmDrlKJzUab/Sc38R3H/n+ZVyTXe9cLFaXlqtiOSpYtP hsJg== X-Gm-Message-State: AMCzsaWRjoaQ9V5HaFmCp2O9hwkVNsFofsNaVVrKCyvhdTzydDNKCoFo zPUofr9ZNsxChmsDm7muEwn31Alkeq543vvA63tpog== X-Google-Smtp-Source: ABhQp+Sv5EOjp+fdLXrBCuuevIOxXaHC+Cp8jBMY37Sv1HabWEfgQqZ/mAqC4YfexBWWqUpJDNMNf/YrF60+bTzq/aU= X-Received: by 10.237.35.58 with SMTP id h55mr1764943qtc.108.1509122056196; Fri, 27 Oct 2017 09:34:16 -0700 (PDT) MIME-Version: 1.0 Sender: php@golemon.com Received: by 10.12.156.1 with HTTP; Fri, 27 Oct 2017 09:34:15 -0700 (PDT) X-Originating-IP: [206.252.215.26] In-Reply-To: References: <20171027121225.2AAC95FB05@mx.zeyos.com> Date: Fri, 27 Oct 2017 12:34:15 -0400 X-Google-Sender-Auth: gvVhOu7MJ-THH9kiVxlNYwFvxt8 Message-ID: To: Nikita Popov Cc: Benjamin Coutu , PHP Internals , Dmitry Stogov Content-Type: text/plain; charset="UTF-8" Subject: Re: [PHP-DEV] Apply substr() optimization to array_slice() From: pollita@php.net (Sara Golemon) On Fri, Oct 27, 2017 at 12:10 PM, Nikita Popov wrote: > 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 form, > because arrays have a bunch of additional hidden state. See > https://bugs.php.net/bug.php?id=75433 for a (not yet fixed) issue that > resulted from similar optimizations in 7.2. We'll have to review all the > places where we apply optimizations like these and make sure that we're not > introducing incorrect behavior wrt the next free element or internal array > 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 Basically, that next insert index for the output of array_values() should be reset, and with the optimization it's not. For array_values() the quick and dirty fix is adding "&& nextIndex == 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. I'll clean up these uses now, and would suggest something like: 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(). -Sara