From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-4.2 required=3.0 tests=ALL_TRUSTED,BAYES_00, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, T_SCC_BODY_TEXT_LINE shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id 1C2451F623 for ; Mon, 29 Aug 2022 09:26:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=80x24.org; s=selector1; t=1661765209; bh=TwuTDlqEc54/uOwg0JrhyHPmR57SqQkPXUhXg1z3fmI=; h=From:To:Subject:Date:In-Reply-To:References:From; b=bzj6Vb7yFvQ0Igv4IN3JFBsDPIZq+/TlmInFhoInPCG+P+icHE5/B/uYj/L5x+r6q UNMZUmCeJmV+Z2QLesN8+QP4R7T0/+OcckiDoCzENgk2MJfOIrQaoc8B6hRVTkMy8P I9Pdg1fbxHLPMTUtixN7XMPdg5qnHluYtG+Zt54s= From: Eric Wong To: meta@public-inbox.org Subject: [PATCH 11/18] view: cleanups and reuse for {obuf} preparation Date: Mon, 29 Aug 2022 09:26:40 +0000 Message-Id: <20220829092647.1512215-12-e@80x24.org> In-Reply-To: <20220829092647.1512215-1-e@80x24.org> References: <20220829092647.1512215-1-e@80x24.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: {obuf} will eventually go away and we'll write directly to {zbuf}, but as an intermediate step we'll make some changes to rely less on return values. While we're in the area, reuse Linkify objects in more places where possible to save some allocations. --- lib/PublicInbox/View.pm | 49 ++++++++++++++++++++--------------------- t/plack.t | 2 +- 2 files changed, 25 insertions(+), 26 deletions(-) diff --git a/lib/PublicInbox/View.pm b/lib/PublicInbox/View.pm index 11ed2d76..354cdd93 100644 --- a/lib/PublicInbox/View.pm +++ b/lib/PublicInbox/View.pm @@ -38,7 +38,7 @@ sub msg_page_i { : $ctx->gone('over'); $ctx->{mhref} = ($ctx->{nr} || $ctx->{smsg}) ? "../${\mid_href($smsg->{mid})}/" : ''; - my $obuf = $ctx->{obuf} = _msg_page_prepare_obuf($eml, $ctx); + my $obuf = _msg_page_prepare_obuf($eml, $ctx); if (length($$obuf)) { multipart_text_as_html($eml, $ctx); $$obuf .= '
'; @@ -58,7 +58,7 @@ sub no_over_html ($) { my $eml = PublicInbox::Eml->new($bref); $ctx->{mhref} = ''; PublicInbox::WwwStream::init($ctx); - my $obuf = $ctx->{obuf} = _msg_page_prepare_obuf($eml, $ctx); + my $obuf = _msg_page_prepare_obuf($eml, $ctx); if (length($$obuf)) { multipart_text_as_html($eml, $ctx); $$obuf .= '
'; @@ -661,9 +661,9 @@ sub add_text_body { # callback for each_part sub _msg_page_prepare_obuf { my ($eml, $ctx) = @_; - my $over = $ctx->{ibx}->over; + my $have_over = !!$ctx->{ibx}->over; my $obfs_ibx = $ctx->{-obfs_ibx}; - my $rv = ''; + $ctx->{obuf} = \(my $rv = ''); my $mids = mids_for_index($eml); my $nr = $ctx->{nr}++; if ($nr) { # unlikely @@ -672,14 +672,13 @@ sub _msg_page_prepare_obuf { return \$rv; } $rv .= -"
WARNING: multiple messages have this Message-ID\n
"; - $rv .= '
';
+"
WARNING: multiple messages have this Message-ID\n
";
 	} else {
 		$ctx->{first_hdr} = $eml->header_obj;
 		$ctx->{chash} = content_hash($eml) if $ctx->{smsg}; # reused MID
 		$rv .= ""; # anchor for body start
 	}
-	$ctx->{-upfx} = '../' if $over;
+	$ctx->{-upfx} = '../' if $have_over;
 	my @title; # (Subject[0], From[0])
 	for my $v ($eml->header('From')) {
 		my @n = PublicInbox::Address::names($v);
@@ -704,7 +703,7 @@ sub _msg_page_prepare_obuf {
 		my $v = ascii_html(shift @subj);
 		obfuscate_addrs($obfs_ibx, $v) if $obfs_ibx;
 		$rv .= 'Subject: ';
-		$rv .= $over ? qq($v\n) : "$v\n";
+		$rv .= $have_over ? qq($v\n) : "$v\n";
 		$title[0] = $v;
 		for $v (@subj) { # multi-Subject message :<
 			$v = ascii_html($v);
@@ -712,7 +711,7 @@ sub _msg_page_prepare_obuf {
 			$rv .= "Subject: $v\n";
 		}
 	} else { # dummy anchor for thread skeleton at bottom of page
-		$rv .= qq() if $over;
+		$rv .= qq() if $have_over;
 		$title[0] = '(no subject)';
 	}
 	for my $v ($eml->header('Date')) {
@@ -724,22 +723,22 @@ sub _msg_page_prepare_obuf {
 		$ctx->{-title_html} = join(' - ', @title);
 		$rv = $ctx->html_top . $rv;
 	}
+
+	$ctx->{-linkify} //= PublicInbox::Linkify->new;
 	if (scalar(@$mids) == 1) { # common case
 		my $mhtml = ascii_html($mids->[0]);
-		$rv .= "Message-ID: <$mhtml> ";
-		$rv .= "(raw)\n";
+		$rv .= qq[Message-ID: <$mhtml> (raw)\n];
 	} else {
 		# X-Alt-Message-ID can happen if a message is injected from
 		# public-inbox-nntpd because of multiple Message-ID headers.
-		my $lnk = PublicInbox::Linkify->new;
 		my $s = '';
 		for my $h (qw(Message-ID X-Alt-Message-ID)) {
 			$s .= "$h: $_\n" for ($eml->header_raw($h));
 		}
-		$lnk->linkify_mids('..', \$s, 1);
+		$ctx->{-linkify}->linkify_mids('..', \$s, 1);
 		$rv .= $s;
 	}
-	$rv .= _parent_headers($eml, $over);
+	_parent_headers($ctx, $eml);
 	$rv .= "\n";
 	\$rv;
 }
@@ -792,35 +791,35 @@ sub thread_skel ($$$) {
 }
 
 sub _parent_headers {
-	my ($hdr, $over) = @_;
-	my $rv = '';
+	my ($ctx, $hdr) = @_;
 	my @irt = $hdr->header_raw('In-Reply-To');
 	my $refs;
 	if (@irt) {
-		my $lnk = PublicInbox::Linkify->new;
-		$rv .= "In-Reply-To: $_\n" for @irt;
-		$lnk->linkify_mids('..', \$rv);
+		my $s = '';
+		$s .= "In-Reply-To: $_\n" for @irt;
+		$ctx->{-linkify}->linkify_mids('..', \$s);
+		${$ctx->{obuf}} .= $s;
 	} else {
 		$refs = references($hdr);
 		my $irt = pop @$refs;
 		if (defined $irt) {
 			my $html = ascii_html($irt);
 			my $href = mid_href($irt);
-			$rv .= "In-Reply-To: <";
-			$rv .= "$html>\n";
+			${$ctx->{obuf}} .= <$html>
+EOM
 		}
 	}
 
 	# do not display References: if search is present,
 	# we show the thread skeleton at the bottom, instead.
-	return $rv if $over;
+	return if $ctx->{ibx}->over;
 
 	$refs //= references($hdr);
 	if (@$refs) {
-		@$refs = map { linkify_ref_no_over($_) } @$refs;
-		$rv .= 'References: '. join("\n\t", @$refs) . "\n";
+		$_ = linkify_ref_no_over($_) for @$refs;
+		${$ctx->{obuf}} .= 'References: '. join("\n\t", @$refs) . "\n";
 	}
-	$rv;
 }
 
 # returns a string buffer
diff --git a/t/plack.t b/t/plack.t
index 20f5d8d5..32209c7d 100644
--- a/t/plack.t
+++ b/t/plack.t
@@ -155,7 +155,7 @@ my $c1 = sub {
 	is(200, $res->code, "success for $path");
 	my $html = $res->content;
 	like($html, qr!hihi - Me!, 'HTML returned');
-	like($html, qr!