user/dev discussion of public-inbox itself
 help / color / Atom feed
* [PATCH] view: shorten life of MIME object for permalink
@ 2020-02-17 11:00 Eric Wong
  2020-02-17 23:02 ` Eric Wong
  0 siblings, 1 reply; 2+ messages in thread
From: Eric Wong @ 2020-02-17 11:00 UTC (permalink / raw)
  To: meta

We don't need to hold onto the Email::MIME object across
multiple WwwResponse->getline calls, instead we can stuff
the rendered HTML of the first (and hopefully only) message
of the buffer into ctx->{-html_tip}.
---
 lib/PublicInbox/View.pm | 58 ++++++++++++++++-------------------------
 1 file changed, 23 insertions(+), 35 deletions(-)

diff --git a/lib/PublicInbox/View.pm b/lib/PublicInbox/View.pm
index 980f265a..91443f55 100644
--- a/lib/PublicInbox/View.pm
+++ b/lib/PublicInbox/View.pm
@@ -26,22 +26,14 @@ sub th_pfx ($) { $_[0] == 0 ? '' : TCHILD };
 
 sub msg_page_i {
 	my ($nr, $ctx) = @_;
-	my $more = $ctx->{more};
-	if ($nr == 1) {
-		# $more cannot be true w/o $smsg being defined:
-		$ctx->{mhref} = $more ? '../'.mid_href($ctx->{smsg}->{mid}).'/'
-				      : '';
-		multipart_text_as_html(delete $ctx->{mime}, $ctx);
-		${delete $ctx->{obuf}} .= '</pre><hr>';
-	} elsif ($more) {
-		++$ctx->{end_nr};
-		# fake an EOF if {more} retrieval fails fails;
-		eval { msg_page_more($ctx, $nr) };
-	} elsif ($nr == $ctx->{end_nr}) {
+	if (my $more = delete $ctx->{more}) { # unlikely
+		# fake an EOF if $more retrieval fails;
+		eval { msg_page_more($ctx, $nr, @$more) };
+	} elsif (my $hdr = delete $ctx->{hdr}) {
 		# fake an EOF if generating the footer fails;
 		# we want to at least show the message if something
 		# here crashes:
-		eval { html_footer($ctx) };
+		eval { html_footer($ctx, $hdr) };
 	} else {
 		undef
 	}
@@ -53,40 +45,37 @@ sub msg_page {
 	my ($ctx) = @_;
 	my $mid = $ctx->{mid};
 	my $ibx = $ctx->{-inbox};
-	my ($first);
-	my $smsg;
+	my ($smsg, $first, $next);
 	if (my $over = $ibx->over) {
 		my ($id, $prev);
-		$smsg = $over->next_by_mid($mid, \$id, \$prev);
-		$first = $ibx->msg_by_smsg($smsg) if $smsg;
-		if ($first) {
-			my $next = $over->next_by_mid($mid, \$id, \$prev);
-			$ctx->{more} = [ $id, $prev, $next ] if $next;
-		}
-		return unless $first;
+		$smsg = $over->next_by_mid($mid, \$id, \$prev) or return;
+		$first = $ibx->msg_by_smsg($smsg) or return;
+		$next = $over->next_by_mid($mid, \$id, \$prev);
+		$ctx->{more} = [ $id, $prev, $next ] if $next;
 	} else {
 		$first = $ibx->msg_by_mid($mid) or return;
 	}
-	my $mime = $ctx->{mime} = PublicInbox::MIME->new($first);
+	my $mime = PublicInbox::MIME->new($first);
 	$ctx->{-obfs_ibx} = $ibx->{obfuscate} ? $ibx : undef;
 	my $hdr = $ctx->{hdr} = $mime->header_obj;
-	_msg_page_prepare_obuf($hdr, $ctx, 0);
-	$ctx->{end_nr} = 2;
+	$ctx->{obuf} = _msg_page_prepare_obuf($hdr, $ctx, 0);
 	$ctx->{smsg} = $smsg;
+	# $next cannot be true w/o $smsg being defined:
+	$ctx->{mhref} = $next ? '../'.mid_href($smsg->{mid}).'/' : '';
+	multipart_text_as_html($mime, $ctx);
+	$ctx->{-html_tip} = (${delete $ctx->{obuf}} .= '</pre><hr>');
 	PublicInbox::WwwStream->response($ctx, 200, \&msg_page_i);
 }
 
-sub msg_page_more {
-	my ($ctx, $nr) = @_;
-	my ($id, $prev, $smsg) = @{$ctx->{more}};
+sub msg_page_more { # cold
+	my ($ctx, $nr, $id, $prev, $smsg) = @_;
 	my $ibx = $ctx->{-inbox};
-	$smsg = $ibx->smsg_mime($smsg);
 	my $next = $ibx->over->next_by_mid($ctx->{mid}, \$id, \$prev);
-	$ctx->{more} = $next ? [ $id, $prev, $next ] : undef;
-	return '' unless $smsg;
+	$ctx->{more} = [ $id, $prev, $next ] if $next;
+	$smsg = $ibx->smsg_mime($smsg) or return '';
 	$ctx->{mhref} = '../' . mid_href($smsg->{mid}) . '/';
 	my $mime = delete $smsg->{mime};
-	_msg_page_prepare_obuf($mime->header_obj, $ctx, $nr);
+	$ctx->{obuf} = _msg_page_prepare_obuf($mime->header_obj, $ctx, $nr);
 	multipart_text_as_html($mime, $ctx);
 	${delete $ctx->{obuf}} .= '</pre><hr>';
 }
@@ -686,7 +675,7 @@ sub _msg_page_prepare_obuf {
 	}
 	$rv .= _parent_headers($hdr, $over);
 	$rv .= "\n";
-	$ctx->{obuf} = \$rv;
+	\$rv;
 }
 
 sub SKEL_EXPAND () {
@@ -769,9 +758,8 @@ sub _parent_headers {
 
 # returns a string buffer via ->getline
 sub html_footer {
-	my ($ctx) = @_;
+	my ($ctx, $hdr) = @_;
 	my $ibx = $ctx->{-inbox};
-	my $hdr = delete $ctx->{hdr};
 	my $upfx = '../';
 	my $skel = " <a\nhref=\"$upfx\">index</a>";
 	my $rv = '<pre>';

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] view: shorten life of MIME object for permalink
  2020-02-17 11:00 [PATCH] view: shorten life of MIME object for permalink Eric Wong
@ 2020-02-17 23:02 ` Eric Wong
  0 siblings, 0 replies; 2+ messages in thread
From: Eric Wong @ 2020-02-17 23:02 UTC (permalink / raw)
  To: meta

Pushed as commit 9703d80efd848f582e5b265db1958e0f143d8712

I expect this to be significant in high-concurrency situations.
There's more changes on the horizon to further reduce memory
usage of the WWW interface :>

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, back to index

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-02-17 11:00 [PATCH] view: shorten life of MIME object for permalink Eric Wong
2020-02-17 23:02 ` Eric Wong

user/dev discussion of public-inbox itself

Archives are clonable:
	git clone --mirror https://public-inbox.org/meta
	git clone --mirror http://czquwvybam4bgbro.onion/meta
	git clone --mirror http://hjrcffqmbrq6wope.onion/meta
	git clone --mirror http://ou63pmih66umazou.onion/meta

Example config snippet for mirrors

Newsgroups are available over NNTP:
	nntp://news.public-inbox.org/inbox.comp.mail.public-inbox.meta
	nntp://ou63pmih66umazou.onion/inbox.comp.mail.public-inbox.meta
	nntp://czquwvybam4bgbro.onion/inbox.comp.mail.public-inbox.meta
	nntp://hjrcffqmbrq6wope.onion/inbox.comp.mail.public-inbox.meta
	nntp://news.gmane.io/gmane.mail.public-inbox.general

 note: .onion URLs require Tor: https://www.torproject.org/

AGPL code for this site: git clone https://public-inbox.org/public-inbox.git