Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:98928 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 22723 invoked from network); 2 May 2017 09:13:55 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 2 May 2017 09:13:55 -0000 Authentication-Results: pb1.pair.com smtp.mail=jesseschalken@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=jesseschalken@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.214.54 as permitted sender) X-PHP-List-Original-Sender: jesseschalken@gmail.com X-Host-Fingerprint: 209.85.214.54 mail-it0-f54.google.com Received: from [209.85.214.54] ([209.85.214.54:37548] helo=mail-it0-f54.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 4C/62-02776-0DD48095 for ; Tue, 02 May 2017 05:13:53 -0400 Received: by mail-it0-f54.google.com with SMTP id x188so5983245itb.0 for ; Tue, 02 May 2017 02:13:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:sender:from:date:message-id:subject:to; bh=vW1Gnh79Xm3TrKx2lzgoSla/OyxIYK9zSY3/F914cN4=; b=Vz/+jqOTYE7Ep1FxFyNx5cwsLiXB6jqtwtn1NzQYAyOrpa7cSZJsphhyYtU72BFkcl OifZk3L9O7bSPXkaoKB2/YTsIgcJfGsZqFY8Kc2qWJ+vzKKYpcVCuMGIj1puFIZBFfCR OFP83KdMdlFjDuqiFmKg+9GVBlcbrfdONWYh1GBMOFA/R0cLQmHRQ4bqgvKHpYf7zhzi iB0fzAVp6/lOlC1SdiWo4Cx/9z4Zi+eXhjHx4AE8ZEOuY4vRXwlhS/r2tj5XCOgkQITi kvSnKD50V8P7bltFaaUIaxvjPTi7InejeXweV8DOwiRHqkfK1rf6U6ZaGsf8BU+wFSfJ eh8w== 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:from:date:message-id:subject :to; bh=vW1Gnh79Xm3TrKx2lzgoSla/OyxIYK9zSY3/F914cN4=; b=po6/gpmxlVQNYWYgCBkGrXvYXzl6yO3WM67nSl01T4RL2zy4ZBbD8W7nv0PHzFcqbC 9AfqxqCVjKB5WFm1VFN6iBeZ1fYsSHcBfDIKTvHp2EI4Keuihc8VzxqSiedzQMTkryjy WVfSo92qpwHBFEkO89PgoS4dox26HraA6m7u8fzJfNzsRwHj95K1kAXiuQfQF76nowSS NSFqlKdEVVBNZRpyB4nrbcWcvwChchXV7OZmndPy8KJfwNek8eWjhXTeEFPSiYpZY2dz GC0FYLRC/NNAA/C3rUxqVOHqFaqtG/pPVU9uHMJh9f/xeLKr4QnrF3IOoP9TukwmwApR 3gAg== X-Gm-Message-State: AN3rC/5rh1xiJMRD2gvrOQnYzE93aiRlgKlYUy4kFK0W6NXoqg0I014Q 7/3IkjjXedY6BRKUnHnU9CblYb9v2veEA8U= X-Received: by 10.202.5.1 with SMTP id 1mr10642017oif.0.1493716429411; Tue, 02 May 2017 02:13:49 -0700 (PDT) MIME-Version: 1.0 Sender: jesseschalken@gmail.com Received: by 10.74.134.135 with HTTP; Tue, 2 May 2017 02:13:49 -0700 (PDT) Date: Tue, 2 May 2017 19:13:49 +1000 X-Google-Sender-Auth: Z8spsM25xUGB59SCeCpZ5IQ7K9I Message-ID: To: PHP internals Content-Type: multipart/alternative; boundary=94eb2c18d9862b9a23054e86f56c Subject: Add is_vectorlike($array) function From: me@jesseschalken.com (Jesse Schalken) --94eb2c18d9862b9a23054e86f56c Content-Type: text/plain; charset=UTF-8 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. --94eb2c18d9862b9a23054e86f56c--