Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:98929 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 25675 invoked from network); 2 May 2017 09:55:36 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 2 May 2017 09:55:36 -0000 Authentication-Results: pb1.pair.com header.from=rowan.collins@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=rowan.collins@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 74.125.82.51 as permitted sender) X-PHP-List-Original-Sender: rowan.collins@gmail.com X-Host-Fingerprint: 74.125.82.51 mail-wm0-f51.google.com Received: from [74.125.82.51] ([74.125.82.51:36937] helo=mail-wm0-f51.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 42/D2-02776-79758095 for ; Tue, 02 May 2017 05:55:35 -0400 Received: by mail-wm0-f51.google.com with SMTP id m123so13097979wma.0 for ; Tue, 02 May 2017 02:55:34 -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=oEzAHqZzrvMl0uE8OBGFq6pmtU6cfTis8hJ8FsKawec=; b=gltwXJc4MaltB8e/5Bw/1scrQp2cbbyLpvX9ad1BaRXRWY/nk8dW+wOmb3ihVWA+qZ ZTc4X5dQ04reA8/LS8IwG3cDQeR3aVzvgaJiiOTHCL2KJcluZghYDxvlRqwhsJCDqx1+ NZF13bhdj8NZD7J9gZU/GdtX4PvuJprqW6hM5cdJImBIiI03RftelGgYYX3jZu+UXqPg Ayh3gTHo7mIsFVoC2JCE7PedRNiyrj9vR/0ZWMRGL2RTmMh7w0AzzJqIADX5pGybRZAK q9DBNgOKltxx31HgbR79gncWFLywic9xkh6vkY2PWFZt72PRCHWFSEpfLxRo8SzpI8vq 7PDg== 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=oEzAHqZzrvMl0uE8OBGFq6pmtU6cfTis8hJ8FsKawec=; b=drGBXCo8wo2x5EFyRNhUpK9O3xF24svmIH6k/f/lD8zabDCEcW+5VKfCFS2O4CPq4n qvSkcBLnTMq9BKK1NplqbrzGYHOXmhMJMR6Ca/uhqql0GPbZs/bH8jigC0GmzW7KL/I4 LKM5ctcHHK21SE1c1f8kXM75k/8vapwCO7OkKUmj3pZsdH9BwymJAFPSlCX1tQzv064u aEY8H4lyF965SLc7sJLMJ0e1TGcDwauucY7QwyeIwJtRBq0M6+FHSS4OmUO2UFqkJeDR UvnIFUc0hiBxWmbhEPwWRotWx/Qx51uhz9aIJv5oevlQgn+Rh0fikoXjkhuYLx3tzSeO iCDg== X-Gm-Message-State: AN3rC/4D9SawIKgW0dDoV7ufuJENkOy1MDnSXB4mjXYOBmM7l6jAbLwT 2QnRfsuxH3Meh2n8 X-Received: by 10.28.59.69 with SMTP id i66mr1593684wma.42.1493718932033; Tue, 02 May 2017 02:55:32 -0700 (PDT) Received: from ?IPv6:2a00:23c4:4bd2:6e00:6c77:1b08:f657:4323? ([2a00:23c4:4bd2:6e00:6c77:1b08:f657:4323]) by smtp.googlemail.com with ESMTPSA id t30sm16607979wrc.24.2017.05.02.02.55.30 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 02 May 2017 02:55:31 -0700 (PDT) To: internals@lists.php.net References: Message-ID: <0b4f249f-f574-2dcc-4485-1d765bc4cb18@gmail.com> Date: Tue, 2 May 2017 10:55:22 +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 02/05/2017 10:13, Jesse Schalken wrote: > Related to the optimisation made by Sara Golemon here: > https://github.com/php/php-src/commit/c74bc87c74f48bc55541b3bf2fc67d595f58a3b5 > > I often define a function like this, which checks if an array is "vector > like" i.e. has keys 0,1,2..N: > > function is_vectorlike(array $a): bool { > $i = 0; > foreach ($a as $k => $v) { > if ($k !== $i++) { > return false; > } > } > return true; > } > > The problem is that this function is O(n), but in PHP7 an array that is > vector-like is likely to be packed and without holes (HT_IS_PACKED(x) && > HT_IS_WITHOUT_HOLES(x)), in which case it is known to be vector-like > without needing to iterate over it, but PHP code can't check for this (nor > should it be able to, since it's really an implementation detail). > > Would it be a good idea to define this is_vectorlike() function in the PHP > runtime, so it can short circuit to return true on packed arrays without > holes? The above code would be a suitable polyfill. > +1, I've been thinking of making a similar suggestion. We can bikeshed the name (it should certainly start with "array_"), but I think there's a very good case for having an optimised implementation built in, given the opportunities for short-cutting based on representation details. As an example use case, serialization formats often dynamically switch between an "array"/"vector"/"list", and a "hash"/"dictionary"/"table". I came upon this example recently: https://github.com/php-amqplib/php-amqplib/blob/master/PhpAmqpLib/Wire/AMQPAbstractCollection.php#L218 Regards, -- Rowan Collins [IMSoP]