Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:98968 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 32827 invoked from network); 4 May 2017 22:18:15 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 4 May 2017 22:18:15 -0000 Authentication-Results: pb1.pair.com smtp.mail=rowan.collins@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=rowan.collins@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.128.174 as permitted sender) X-PHP-List-Original-Sender: rowan.collins@gmail.com X-Host-Fingerprint: 209.85.128.174 mail-wr0-f174.google.com Received: from [209.85.128.174] ([209.85.128.174:32812] helo=mail-wr0-f174.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 07/E7-02776-6A8AB095 for ; Thu, 04 May 2017 18:18:15 -0400 Received: by mail-wr0-f174.google.com with SMTP id w50so14773046wrc.0 for ; Thu, 04 May 2017 15:18:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-transfer-encoding; bh=drwO9/oR/FCIAfxyjZYw4aakXxERBQOj5fPrnngaXOk=; b=rX5jeXJyO/UEbTd6k9bFnP3KRuTUhI/pewMiuEJE/gbtpISbnNhwbbbPaCfpRGlr1t UPO9a0FS5urVcfjU6rC9ANXPKzwftCC+M4HXeXQ07aoHZxyUQcFbyg6iUiRbtyKVk/n5 i0cPPoKmbAonAy7djYhKvqUdL7W3InV6zdFQkDWgV3ZyB3elmsE8hNyMFj/UoxHiRn9z bhdzQ116UdpH6GNQ7sqIMlC2CFF9FD6a0T9Hqjd7LoRXpxPb9h+HOJS3CGxKWnl3hrsR Twr8R5sDzji8Y7HTbddKuuYSn7345F/cmG5CDWFN1CQj7Bm80ZhSDBtRDyggJhrocwcb cjrQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding; bh=drwO9/oR/FCIAfxyjZYw4aakXxERBQOj5fPrnngaXOk=; b=jTMbYiaiUcCzNQLUFOMQIN9K21EgJ0CC2OSI6rmuwloJLpiiCwJ4m4MF3Z8MEaBCsV VLI50cIgnAgori4T5AsRU6zUEOKWTrEPopdojKUw6o4y7dE9VKtgLcFJeor4lxwI64LT z5jqi74SWT6cBp1LSeaPcIWJ5qr4evph0hsMIsRZ0kWbMDS3Jw0HR0seS3qrUEUsJrFF 1DL42oOEHLI37WKt1N59K2Odw0zTkkk8DpJYz8LEoCQG02j28SijPr4QaH4hPXRiMgFG 83+3YUtVP4ZEnpCbyucFMwKNZseRnGBVghlfaqXzMQxPSjLBO+CeimQVcuTOGzW4d4si 4qsg== X-Gm-Message-State: AN3rC/5h/jGJNY3ZhWF+MCeVqUketIKi/6I1T4wdgiZdmxplahnqE4v7 CVilxkGKVcCvtR9l X-Received: by 10.223.160.70 with SMTP id l6mr29612694wrl.0.1493936291908; Thu, 04 May 2017 15:18:11 -0700 (PDT) Received: from ?IPv6:2a00:23c4:4bd2:6e00:ddaf:efd9:92d2:d495? ([2a00:23c4:4bd2:6e00:ddaf:efd9:92d2:d495]) by smtp.googlemail.com with ESMTPSA id o97sm2912368wrc.48.2017.05.04.15.18.10 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 04 May 2017 15:18:11 -0700 (PDT) To: internals@lists.php.net References: Message-ID: Date: Thu, 4 May 2017 23:18:08 +0100 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.8.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] Add is_vectorlike($array) function From: rowan.collins@gmail.com (Rowan Collins) On 04/05/2017 16:52, Sara Golemon wrote: > Just want to underline the "is likely to be" part of this sentence. > The implementation can't assume an array is not vector-like just > because those checks fail. It's possible to manipulate an array into > being vector-like, but not packed, or having holes. > > Not really commenting on the proposal yet, which I'm a big > shoulder-shruggy about. Just highlighting the caveat for any eventual > implementation. We can short-circuit the O(n) loop if it *is* > packed/no-holes, but if not then we need to walk it to be certain. > This is probably fine since this check is likely used in places where > the array is expected to be vector like and non-vector should be a > slow path anyway. Maybe I'm misunderstanding what "with holes" means in this case, but I would have expected any array with holes to trivially return false, here, because we're looking for arrays with a complete set of consecutive keys. That aside, there are 3 cases here: A) arrays that are not vector / list like B) arrays that have been constructed and manipulated in various ways, such that they are *now* vector / list like, but aren't packed in memory C) arrays that have been consistently constructed and handled as a "list" / "vector", and will be packed and hole-less in memory - A can be determined (in userland or internally) by walking the array, and short-cutting to false when you find the first non-integer or non-sequential key. - B can only be determined by walking the whole array, right to the end. - In userland, C is indistinguishable from B, so you have to walk right to the end. This is where the big win comes in for an internal function, because we can go from examining *all* the keys to examining *none* of them. I don't know the exact caveats of when the engine packs arrays, but I would expect C to be a common case, because as you say the function will often be called when *expecting* vector-like arrays, and those vector-like arrays will often have been constructed using only basic operations like ['a', 'b', 'c'] or $foo[] = 'd' So the worst case behaviour of an internal function is exactly the same as an optimised userland implementation; but a fairly common best case goes from a guaranteed O(n) to constant. Regards, -- Rowan Collins [IMSoP]