From: Brandon L Black Date: Sat, 21 Apr 2007 16:27:33 +0000 (+0000) Subject: split stacks is faster X-Git-Tag: 0.07~2 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=fa36ae3690db0d971f4fef42808f3e1ec100738b;p=gitmo%2FAlgorithm-C3.git split stacks is faster --- diff --git a/lib/Algorithm/C3.pm b/lib/Algorithm/C3.pm index 66c32ff..73ede8e 100644 --- a/lib/Algorithm/C3.pm +++ b/lib/Algorithm/C3.pm @@ -12,7 +12,12 @@ sub merge { my ($root, $parent_fetcher, $cache) = @_; $cache ||= {}; - my @STACK; # stack for simulating recursion + + # stacks for simulating recursion + my @RSTACK; + my @PSTACK; + my @MSTACK; + my @ISTACK; my $pfetcher_is_coderef = ref($parent_fetcher) eq 'CODE'; @@ -33,7 +38,7 @@ sub merge { if($seen{$new_root}) { my @isastack = ( - (map { $_->[0] } @STACK), + @RSTACK, $current_root, $new_root ); @@ -47,12 +52,10 @@ sub merge { confess "Could not find method $parent_fetcher in $new_root"; } - push(@STACK, [ - $current_root, - $current_parents, - $recurse_mergeout, - $i, - ]); + push(@RSTACK, $current_root); + push(@PSTACK, $current_parents); + push(@MSTACK, $recurse_mergeout); + push(@ISTACK, $i); $current_root = $new_root; $current_parents = $cache->{pfetch}->{$current_root} ||= [ $current_root->$parent_fetcher ]; @@ -117,10 +120,12 @@ sub merge { \@res; }; - return @$mergeout if !@STACK; + return @$mergeout if !@ISTACK; - ($current_root, $current_parents, $recurse_mergeout, $i) - = @{pop @STACK}; + $current_root = pop(@RSTACK); + $current_parents = pop(@PSTACK); + $recurse_mergeout = pop(@MSTACK); + $i = pop(@ISTACK); push(@$recurse_mergeout, $mergeout) if @$mergeout; }