Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:29375 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 55135 invoked by uid 1010); 9 May 2007 14:43:30 -0000 Delivered-To: ezmlm-scan-internals@lists.php.net Delivered-To: ezmlm-internals@lists.php.net Received: (qmail 55111 invoked from network); 9 May 2007 14:43:30 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 9 May 2007 14:43:30 -0000 Authentication-Results: pb1.pair.com smtp.mail=rquadling@googlemail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=rquadling@googlemail.com; sender-id=pass; domainkeys=bad Received-SPF: pass (pb1.pair.com: domain googlemail.com designates 64.233.184.232 as permitted sender) DomainKey-Status: bad X-DomainKeys: Ecelerity dk_validate implementing draft-delany-domainkeys-base-01 X-PHP-List-Original-Sender: rquadling@googlemail.com X-Host-Fingerprint: 64.233.184.232 wr-out-0506.google.com Linux 2.4/2.6 Received: from [64.233.184.232] ([64.233.184.232:18618] helo=wr-out-0506.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id A3/9C-60325-E0ED1464 for ; Wed, 09 May 2007 10:43:28 -0400 Received: by wr-out-0506.google.com with SMTP id i21so237917wra for ; Wed, 09 May 2007 07:43:23 -0700 (PDT) DKIM-Signature: a=rsa-sha1; c=relaxed/relaxed; d=googlemail.com; s=beta; h=domainkey-signature:received:received:message-id:date:from:reply-to:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=LxyqcEyMFUcyLTCcamE4ggZKQnL3P8lLm7VRcwNe0oEFYU7FgGezEdPy783xqXNUi/yHkCn+bUymwsJxJXsVcIQpQQCszE/g9kM3zwlyPaIE8kj/r4xgFCdEi6a3/RClMqAYtgRflwGYVxw4Z41w8TWaMHqNuSsNs9u9f6537FE= DomainKey-Signature: a=rsa-sha1; c=nofws; d=googlemail.com; s=beta; h=received:message-id:date:from:reply-to:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=m+kOcPtoR2wL3RKFcY8Fq4sCAY04OsuwoxEMWPh2gSLi1H6NmQSxzDm3p4Qq2RPBZRYFJsB6Mm6RMPhcL8HXftA7s7+tE+CzXeChkx5bJAXIpDCXkvTkc2hpePzWyFmlz8v3qtFzOmqaZJxLeV3GeK3kks2ZUhyskxcTXlsXPk4= Received: by 10.78.193.5 with SMTP id q5mr116076huf.1178721801817; Wed, 09 May 2007 07:43:21 -0700 (PDT) Received: by 10.78.48.15 with HTTP; Wed, 9 May 2007 07:43:21 -0700 (PDT) Message-ID: <10845a340705090743v6aba7896y45b9f7d60a4e77ba@mail.gmail.com> Date: Wed, 9 May 2007 15:43:21 +0100 Reply-To: RQuadling@GoogleMail.com To: "Brian Moon" Cc: internals@lists.php.net, maurice@phorum.org In-Reply-To: <4641D92C.4050208@dealnews.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline References: <4641D92C.4050208@dealnews.com> Subject: Re: [PHP-DEV] Tree sort in C From: rquadling@googlemail.com ("Richard Quadling") On 09/05/07, Brian Moon wrote: > A common issue in lots of applications is tree sorting with unlimited > depth. Phorum has used a recursive function since 1999 to solve this > problem. However, at MySQL Conference this year, we finally wrote a non > recursive function for it and acheived both memory and time savings on > very large data sets. I always knew it could be done, I had just never > stopped and worked it out. Most examples I found involved a left/right > model that our data does not use. The left/right model involves > altering rows when new rows are inserted into the tree. That is not > attractive. Phorum uses just a parent_id field to track child members. > > We would like to take this to another level and make a PHP internal > funciton to do the work. In fact, since MySQL Conference, one of team > members has written the function as a PHP extension. My questions is, > if we made a generic tree sorting function, could this be a function > that we could get considered for addition to the other array sorting > functions. I have probably ported the Phorum PHP based for tree sorting > over to 100 different other applications for sorting trees in PHP. It > would be very nice to finally have an internal function for it. > > Speed is not our main benefit, although there is a noticable speed > boost. In addition to sorting the function, each node would be assigned > a depth value (optionally named by a function parameter if needed). > This is where the biggest memory savings have been found for us. On > large data sets (2000 members) in PHP, we have found that altering the > input array would cause copy of the whole array, blowing up the memory > usage of the script to as much as 10x. Doing this in C helps to remove > that problem. > > > Basically, an array would look like: > > $array = array( > 1 => array( > "id" => 1, > "parent" => 0, > "name" => "item 1" > ), > 2 => array( > "id" => 2, > "parent" => 0, > "name" => "item 2" > ), > 3 =>array( > "id" => 3, > "parent" => 1, > "name" => "item 1" > ), > ); > > The function call would look like: > > array_treesort($array, "id", "parent"); > > The returned array would look like: > > $array = array( > 1 => array( > "id" => 1, > "parent" => 0, > "name" => "item 1", > "depth" => 0 > ), > 3 =>array( > "id" => 3, > "parent" => 1, > "name" => "item 1", > "depth" => 1 > ), > 2 => array( > "id" => 2, > "parent" => 0, > "name" => "item 2", > "depth" => 0 > ), > ); > > > Its a little funky to have a function take names of fields like this, > but this is the only way we could think of to make this work for people > without them having to change their data structure. > > You can see our current work at > http://www.phorum.org/tracfcgi/browser/phorum5/trunk/extension_src That > code has been written specifically for Phorum to replace our existing > PHP function. It does more than the PHP internal function would do. > But, the extra parts are not the things that we were trying to overcome. > They are just gravy. > > -- > > Brian Moon > Senior Developer > ------------------------------ > http://dealnews.com/ > It's good to be cheap =) > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > > Take a look at http://www.php.net/manual/en/function.array-multisort.php#68689 / http://rquadling.php1h.com/array_multisort_column.php Richard. -- ----- Richard Quadling Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731 "Standing on the shoulders of some very clever giants!"