From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS12876 163.172.208.0/20 X-Spam-Status: No, score=-2.0 required=3.0 tests=AWL,BAYES_00, RCVD_IN_MSPIKE_BL,RCVD_IN_MSPIKE_ZBI,RCVD_IN_XBL,SPF_FAIL,SPF_HELO_FAIL, TO_EQ_FM_DOM_SPF_FAIL shortcircuit=no autolearn=no autolearn_force=no version=3.4.0 Received: from 80x24.org (baconisawesome.rathhansen.com [163.172.214.76]) by dcvr.yhbt.net (Postfix) with ESMTP id 8D8CA1F855 for ; Fri, 5 Aug 2016 00:08:21 +0000 (UTC) From: Eric Wong To: spew@80x24.org Subject: [PATCH] avoid recursion warnings in recurse_down Date: Fri, 5 Aug 2016 00:08:14 +0000 Message-Id: <20160805000814.2266-1-e@80x24.org> List-Id: There is no need to actually perform recursion when traversing messages to perform callbacks on them. This quiets down recursion warnings in large threads (and may avoid crashes). --- Thread.pm | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/Thread.pm b/Thread.pm index a0c8903..01e710c 100644 --- a/Thread.pm +++ b/Thread.pm @@ -440,22 +440,28 @@ sub order_children { } sub recurse_down { + my ($self, $callback) = @_; my %seen; - my $do_it_all; - $do_it_all = sub { - my $self = shift; - my $callback = shift; - $seen{$self}++; - $callback->($self); - - if ($self->next && $seen{$self->next}) { $self->next(undef) } - $do_it_all->($self->next, $callback) if $self->next; - if ($self->child && $seen{$self->child}) { $self->child(undef) } - $do_it_all->($self->child, $callback) if $self->child; - - }; - $do_it_all->(@_); - undef $do_it_all; + my @q = ($self); + while (my $cont = shift @q) { + $seen{$cont} = 1; + $callback->($cont); + + if (my $next = $cont->next) { + if ($seen{$next}) { + $cont->next(undef); + } else { + push @q, $next; + } + } + if (my $child = $cont->child) { + if ($seen{$child}) { + $cont->child(undef); + } else { + push @q, $child; + } + } + } } sub iterate_down { -- EW