dumping ground for random patches and texts
 help / color / mirror / Atom feed
* [PATCH] avoid recursion warnings in recurse_down
@ 2016-08-05  0:08 Eric Wong
  0 siblings, 0 replies; only message in thread
From: Eric Wong @ 2016-08-05  0:08 UTC (permalink / raw)
  To: spew

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


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2016-08-05  0:08 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-08-05  0:08 [PATCH] avoid recursion warnings in recurse_down Eric Wong

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).