Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:98971 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 48439 invoked from network); 5 May 2017 03:58:51 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 5 May 2017 03:58:51 -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.52 as permitted sender) X-PHP-List-Original-Sender: php@golemon.com X-Host-Fingerprint: 74.125.82.52 mail-wm0-f52.google.com Received: from [74.125.82.52] ([74.125.82.52:36393] helo=mail-wm0-f52.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 84/29-02776-978FB095 for ; Thu, 04 May 2017 23:58:50 -0400 Received: by mail-wm0-f52.google.com with SMTP id u65so13146263wmu.1 for ; Thu, 04 May 2017 20:58:49 -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=ieeSVTX04I8VnaExBpI1K7SHetKKA6BzZyMHMk2utyU=; b=1JTNbPjKdHD7YvCPmt4zbU7ks3hQTOm1aKSptifBiJkpX5KGXodIM/7tCaU3U80zqg kYRrUNpeLpy7fNPUU6x6H//vHsOQQTA7qZYyJPiN2EZRAL4D4NgwO103kD5XNbppQ77w ASrVDzEpEy4FAj2wL/zSW/veeqxtbpq24z6uwjfmtakJMUemZaIT1Lu9W0ofGhJJUHSy fjVhjtm+gXScF8Oq3A/3PQ4sspD+/u5uqcKIlRxAnYNjRiIoPBqufvr8TrLyntC83MjP uePgudUgKXFI6HMIqzhdANnQKBByww8FqWRdwTgl8t3oD8TpR1/iRIeIKMwZe0Da8Xnv il6g== 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=ieeSVTX04I8VnaExBpI1K7SHetKKA6BzZyMHMk2utyU=; b=QC0Wfn9PfHefuqhOR/gzDYY0eNuA7ZYOmWBPcYkq+Qd03JZIOgKlUDI1gZB/DjHnx1 CwFsYj3Ky3otICmohjwMRRVLP1ACL2FGtsyydUG4efQqi+efGEXdQjnli3tqkvt9wIdi Gdmn/HxiajHyfZnJqRSiU0njfvBIzAeRBbrdZZeFlbrw3V/XyKt/VB9pWOaSeEpP5b7J Rqry8mIaqqv4SNxIkZKb7zD3ouCLl7c+du+NVDnSpWDXn6JwF3gSNJgm4JtH3suNqsK0 G/tmLfXkr6NGvr6D5IcG7uBIpOINS+OEVzq1oCPT4vLybSA4Sv0SFZaSZ9TwCHl/loWs /OYg== X-Gm-Message-State: AODbwcBJsNf9/rNx1nDrJEB4LzybTJtM/5oW7kPzPzVJYIBP0X9YKfrg JChYisQDb1IouB6t9BBF1phrd7DduA== X-Received: by 10.28.139.136 with SMTP id n130mr3896757wmd.4.1493956726474; Thu, 04 May 2017 20:58:46 -0700 (PDT) MIME-Version: 1.0 Sender: php@golemon.com Received: by 10.223.157.38 with HTTP; Thu, 4 May 2017 20:58:45 -0700 (PDT) X-Originating-IP: [73.9.224.155] In-Reply-To: References: Date: Thu, 4 May 2017 22:58:45 -0500 X-Google-Sender-Auth: YndhA7yepAYN9Z4M2tXa3NEFOLI Message-ID: To: Rowan Collins Cc: PHP internals Content-Type: text/plain; charset=UTF-8 Subject: Re: [PHP-DEV] Add is_vectorlike($array) function From: pollita@php.net (Sara Golemon) On Thu, May 4, 2017 at 5:18 PM, Rowan Collins wrote: > 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. > Correct. The "B" class is the primary case I was referencing. I was thinking there might be another class: Array manipulated such that it is now vector like even without holes, but that the flag hadn't been reset. Looking into zend_hash, I can now see that my assumption about that was wrong, so nevermind. :) > 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' > Absolutely. Were calls to this written in C, they would be wrapped in an EXPECTED() macro. I'm not super worried about the perf (which is certainly no worse than doing it in userland), I just wanted to point out that not *all* invocations of is_vectorlike() would be O(1). That's really all. > 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. > Yes, which is why at worst, this is just a tiny no-harm micro-optimization API. And thus my unemotional shrug later in the message. :) -Sara