Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:98970 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 46628 invoked from network); 5 May 2017 03:44:13 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 5 May 2017 03:44:13 -0000 Authentication-Results: pb1.pair.com header.from=jesseschalken@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=jesseschalken@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.223.182 as permitted sender) X-PHP-List-Original-Sender: jesseschalken@gmail.com X-Host-Fingerprint: 209.85.223.182 mail-io0-f182.google.com Received: from [209.85.223.182] ([209.85.223.182:36695] helo=mail-io0-f182.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 0F/C8-02776-C05FB095 for ; Thu, 04 May 2017 23:44:13 -0400 Received: by mail-io0-f182.google.com with SMTP id p80so47326825iop.3 for ; Thu, 04 May 2017 20:44:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:sender:in-reply-to:references:from:date:message-id :subject:to:cc; bh=oAS6OoEDhjjGsmA6XZSUFSOkWZyEaLbFaSjbXqyWknI=; b=hmdrZtiOaSM0OXQWWVz2ZElqhrlX0ifgykO/G1z1wh9+DL7gs8S65hUiKxZF+ZBKTl guIffC2c2FohKSHhP//pxh11JQ7ngUm7ujsET6DCoqOULInmVez4PLklAS8AKLpxKpYY 1Di4SlNT2Xj2YTUZ5lwoGPXoL7TCUy9w9W+pcKSX8ZAbjJUMlKjcDRmhIjXL5+125HYg iIQLOwkZZryeNkdxsp27jIwCfemgMtRMN1dnyPPMQRCeRDhRtqHViV2To5aMgYEQqkvu qXs1G6QdbBkI6+nCiJ/KPO1xn9+5RSqcJY94wuvva4WyEJHZy3VnoPi7vInEDvT0Laap hU9g== 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=oAS6OoEDhjjGsmA6XZSUFSOkWZyEaLbFaSjbXqyWknI=; b=qOanOmQ1bm1f1ySvhKegqNN52JGeHWSJWcgvHP8es9tGWMzeSimPKzoQsuC5H2SG/T t2UXHPVlLuD8JTTbbsQQIJHl5elGHHMv/78pD/SPm5MDhFNmSQLoq5DNceM306bi6Qhw YyO8Nl9lr0x0eKbaQjmzLXu7OwDJI8j04xiqdDlsjEfC/vZMPwh0z9qzkd8n7S3zRPSo 0jp6t+OnRnfCWIA00Aevpgw2umKp1DclhMTw+oxbFnBSC8vhTX0fUJsJupuYRNJf5Qw5 YLLh6K2guYYepSupgJ/cXAn1sXNqtzTAp0xBt6IqRM/T0zA2xp0Vp6k7nSrwb0fw27k1 9Anw== X-Gm-Message-State: AN3rC/76L2uuuXs5fPMKWV4zR+03y4LzDBiTJKOhbJYUheSleVqyOTUz YddVAg9l9Y2rHlMwnHLnnIGJuJ+2cw== X-Received: by 10.202.190.84 with SMTP id o81mr3683745oif.116.1493955850254; Thu, 04 May 2017 20:44:10 -0700 (PDT) MIME-Version: 1.0 Sender: jesseschalken@gmail.com Received: by 10.74.134.135 with HTTP; Thu, 4 May 2017 20:44:09 -0700 (PDT) In-Reply-To: References: Date: Fri, 5 May 2017 13:44:09 +1000 X-Google-Sender-Auth: mopYfCqFmpKogBrqswR8T_DTtw4 Message-ID: To: Rowan Collins Cc: PHP internals Content-Type: multipart/alternative; boundary=001a113ddda4c3c32a054ebeb3ff Subject: Re: [PHP-DEV] Add is_vectorlike($array) function From: me@jesseschalken.com (Jesse Schalken) --001a113ddda4c3c32a054ebeb3ff Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable On Fri, May 5, 2017 at 8:18 AM, Rowan Collins wrote: > 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, suc= h > 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 t= o > 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 th= em. > > 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-lik= e > arrays will often have been constructed using only basic operations like > ['a', 'b', 'c'] or $foo[] =3D 'd' > > So the worst case behaviour of an internal function is exactly the same a= s > an optimised userland implementation; but a fairly common best case goes > from a guaranteed O(n) to constant. > > Exactly, and I would add that in the case of a truly associative array (A) it will return false on the first key that doesn't match 0,1,2=E2=80=A6N, w= hich is likely to be the first one, so even that is likely to be fast. Putting it in a table: keys checked keys checked keys (PHP version) (C version) ______________________________________________ 0,1,2=E2=80=A6N (packed) N 0 0,1,2=E2=80=A6N N N =E2=8B=AE =E2=8B=AE =E2=8B=AE 0,1,?=E2=80=A6 3 3 0,?,?=E2=80=A6 2 2 ?,?,?=E2=80=A6 1 1 The top and bottom rows are the common cases. --001a113ddda4c3c32a054ebeb3ff--