Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:64819 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 91100 invoked from network); 10 Jan 2013 14:14:42 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 10 Jan 2013 14:14:42 -0000 Authentication-Results: pb1.pair.com header.from=glopes@nebm.ist.utl.pt; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=glopes@nebm.ist.utl.pt; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain nebm.ist.utl.pt from 193.136.128.21 cause and error) X-PHP-List-Original-Sender: glopes@nebm.ist.utl.pt X-Host-Fingerprint: 193.136.128.21 smtp1.ist.utl.pt Linux 2.6 Received: from [193.136.128.21] ([193.136.128.21:59242] helo=smtp1.ist.utl.pt) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id AF/F8-02684-FCCCEE05 for ; Thu, 10 Jan 2013 09:14:40 -0500 Received: from localhost (localhost.localdomain [127.0.0.1]) by smtp1.ist.utl.pt (Postfix) with ESMTP id 0097A70003E0 for ; Thu, 10 Jan 2013 14:14:37 +0000 (WET) X-Virus-Scanned: by amavisd-new-2.6.4 (20090625) (Debian) at ist.utl.pt Received: from smtp1.ist.utl.pt ([127.0.0.1]) by localhost (smtp1.ist.utl.pt [127.0.0.1]) (amavisd-new, port 10025) with LMTP id eYt7VDklSIVZ for ; Thu, 10 Jan 2013 14:14:36 +0000 (WET) Received: from nebm.ist.utl.pt (unknown [IPv6:2001:690:2100:4::58:1]) by smtp1.ist.utl.pt (Postfix) with ESMTP id A7D4F70003D6 for ; Thu, 10 Jan 2013 14:14:36 +0000 (WET) Received: from localhost ([127.0.0.1] helo=nebm.ist.utl.pt) by nebm.ist.utl.pt with esmtp (Exim 4.72) (envelope-from ) id 1TtItt-00026O-Iy for internals@lists.php.net; Thu, 10 Jan 2013 14:14:33 +0000 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Date: Thu, 10 Jan 2013 15:14:33 +0100 To: internals PHP Organization: =?UTF-8?Q?N=C3=BAcleo_de_Engenharia_Biom=C3=A9dica_do_Insti?= =?UTF-8?Q?tuto_Superior_T=C3=A9cnico?= Message-ID: X-Sender: glopes@nebm.ist.utl.pt User-Agent: RoundCube Webmail/0.8-rc Subject: Add a =?UTF-8?Q?zend=5Fqsort=5Fr/zend=5Fqsort=20implementation?= From: glopes@nebm.ist.utl.pt (Gustavo Lopes) I need to order an native array. The comparison function needs some context to do the comparison. For test purposes, I defined _GNU_SOURCE and used qsort_r. But since this is obviously not acceptable I looked for alternatives. PHP includes at least two sorting functions: zend_qsort (in Zend/zend_qsort.c) php_mergesort (in main/mergesort.c), which is not used anywhere, including PECL: http://lxr.php.net/search?q=&defs=&refs=php_mergesort&path=&hist=&project=PECL&project=PHP_TRUNK Both take comparison functions with the type int (*)(void *, void * TSRMLS_DC) So I *could* pass the context through a global variable, but I'd rather not do it. I think it'd be good to have a zend_qsort_r function with a different comparison function type: int (*)(void *, void * TSRMLS_DC, void *) With this we could implement zend_qsort in terms of zend_qsort_r very simply, though with technically undefined behavior: https://github.com/cataphract/php-src/commit/c6aa2faa83dc8787d7cefcb27faa7f07f4aae6f0 We could export zend_qsort_r starting in 5.5; I need it ext/standard so it need not be exported, which would be problematic on a stable branch. It's undefined behavior because the comparison function is being called with one extra pointer argument. This is the same technique glibc uses though, so it should be safe. I could not find any measurable performance penalty. *** A different topic: the current zend_qsort function can be further optimized. I got these results (test script below, best of 5 runs) with the current implementation: random: 0.6453 sorted:0.6091 reverse sorted: 0.6203 And bionic's quick sort implementation: random: 0.5361 sorted:0.5769 reverse sorted: 0.5920 I also tested with musl's smooth sort implementation: random: 1.3554 sorted:0.2584 reverse sorted: 0.6439 Bionic's implementation also has the advantage that it appears to be stable: