]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/View.pm
view: /$INBOX/$MSGID/t/: avoid extra hash lookup in eml case
[public-inbox.git] / lib / PublicInbox / View.pm
index 98445f0e0b0f60ad0a1ee07b28fb4b3fe1dec19b..138e0c3a217d6173de7049c577fcc0ea72769594 100644 (file)
@@ -181,7 +181,8 @@ sub fmt_ts ($) { strftime('%Y-%m-%d %k:%M', gmtime($_[0])) }
 # Displays the text of of the message for /$INBOX/$MSGID/[Tt]/ endpoint
 # this is already inside a <pre>
 sub eml_entry {
-       my ($ctx, $smsg, $eml, $more) = @_;
+       my ($ctx, $eml, $more) = @_;
+       my $smsg = delete $ctx->{smsg};
        my $subj = delete $smsg->{subject};
        my $mid_raw = $smsg->{mid};
        my $id = id_compress($mid_raw, 1);
@@ -370,12 +371,12 @@ sub pre_thread  { # walk_thread callback
 }
 
 sub thread_eml_entry {
-       my ($ctx, $level, $smsg, $eml) = @_;
-       my ($beg, $end) = thread_adj_level($ctx, $level);
-       $beg . '<pre>' . eml_entry($ctx, $smsg, $eml, 0) . '</pre>' . $end;
+       my ($ctx, $eml) = @_;
+       my ($beg, $end) = thread_adj_level($ctx, $ctx->{level});
+       $beg . '<pre>' . eml_entry($ctx, $eml, 0) . '</pre>' . $end;
 }
 
-sub next_in_queue ($;$) {
+sub next_in_queue ($$) {
        my ($q, $ghost_ok) = @_;
        while (@$q) {
                my ($level, $smsg) = splice(@$q, 0, 2);
@@ -387,29 +388,36 @@ sub next_in_queue ($;$) {
 }
 
 sub stream_thread_i { # PublicInbox::WwwStream::getline callback
-       my ($ctx) = @_;
+       my ($ctx, $eml) = @_;
+       goto &thread_eml_entry if $eml; # tail recursion
        return unless exists($ctx->{skel});
-       my $nr = $ctx->{nr}++;
-       my ($level, $smsg) = next_in_queue($ctx->{-queue}, $nr);
-
-       $smsg or return
-               join('', thread_adj_level($ctx, 0)) . ${delete $ctx->{skel}};
-
-       my $eml = $ctx->{-inbox}->smsg_eml($smsg) or return
-               ghost_index_entry($ctx, $level, $smsg);
-
-       if ($nr == 0) {
-               $ctx->{-title_html} = ascii_html($smsg->{subject});
-               $ctx->html_top . thread_eml_entry($ctx, $level, $smsg, $eml);
-       } else {
-               thread_eml_entry($ctx, $level, $smsg, $eml);
+       my $ghost_ok = $ctx->{nr}++;
+       while (1) {
+               my ($lvl, $smsg) = next_in_queue($ctx->{-queue}, $ghost_ok);
+               if ($smsg) {
+                       if (exists $smsg->{blob}) { # next message for cat-file
+                               $ctx->{level} = $lvl;
+                               if (!$ghost_ok) { # first non-ghost
+                                       $ctx->{-title_html} =
+                                               ascii_html($smsg->{subject});
+                                       $ctx->zmore($ctx->html_top);
+                               }
+                               return $smsg;
+                       }
+                       # buffer the ghost entry and loop
+                       $ctx->zmore(ghost_index_entry($ctx, $lvl, $smsg));
+               } else { # all done
+                       $ctx->zmore(join('', thread_adj_level($ctx, 0)));
+                       $ctx->zmore(${delete($ctx->{skel})});
+                       return;
+               }
        }
 }
 
 sub stream_thread ($$) {
        my ($rootset, $ctx) = @_;
        $ctx->{-queue} = [ map { (0, $_) } @$rootset ];
-       PublicInbox::WwwStream::response($ctx, 200, \&stream_thread_i);
+       PublicInbox::WwwStream::aresponse($ctx, 200, \&stream_thread_i);
 }
 
 # /$INBOX/$MESSAGE_ID/t/
@@ -449,23 +457,26 @@ sub thread_html {
        # flat display: lazy load the full message from smsg
        $ctx->{msgs} = $msgs;
        $ctx->{-html_tip} = '<pre>';
-       PublicInbox::WwwStream::response($ctx, 200, \&thread_html_i);
+       PublicInbox::WwwStream::aresponse($ctx, 200, \&thread_html_i);
 }
 
 sub thread_html_i { # PublicInbox::WwwStream::getline callback
-       my ($ctx) = @_;
-       my $msgs = $ctx->{msgs} or return;
-       while (my $smsg = shift @$msgs) {
-               my $eml = $ctx->{-inbox}->smsg_eml($smsg) or next;
+       my ($ctx, $eml) = @_;
+       if ($eml) {
+               my $smsg = $ctx->{smsg};
                if (exists $ctx->{-html_tip}) {
                        $ctx->{-title_html} = ascii_html($smsg->{subject});
-                       return $ctx->html_top .
-                               eml_entry($ctx, $smsg, $eml, scalar @$msgs);
+                       $ctx->zmore($ctx->html_top);
+               }
+               return eml_entry($ctx, $eml, scalar @{$ctx->{msgs}});
+       } else {
+               while (my $smsg = shift @{$ctx->{msgs}}) {
+                       return $smsg if exists($smsg->{blob});
                }
-               return eml_entry($ctx, $smsg, $eml, scalar @$msgs);
+               my $skel = delete($ctx->{skel}) or return; # all done
+               $ctx->zmore($$skel);
+               undef;
        }
-       my ($skel) = delete @$ctx{qw(skel msgs)};
-       $$skel;
 }
 
 sub multipart_text_as_html {