user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
From: Eric Wong <e@80x24.org>
To: meta@public-inbox.org
Subject: [PATCH 1/2] eml: avoid anonymous __WARN__ sub for encode/decode
Date: Tue, 13 Feb 2024 09:42:41 +0000	[thread overview]
Message-ID: <20240213094242.3496966-2-e@80x24.org> (raw)
In-Reply-To: <20240213094242.3496966-1-e@80x24.org>

Repeatedly allocating an anonymous sub is an expensive operation
and a potential source of leaks in older Perl.  Instead,
`local'-ize a global and use a permanent sub to workaround the
old Encode 2.87..3.12 leak.
---
 lib/PublicInbox/Eml.pm | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)

diff --git a/lib/PublicInbox/Eml.pm b/lib/PublicInbox/Eml.pm
index 24060ec8..56aec1e5 100644
--- a/lib/PublicInbox/Eml.pm
+++ b/lib/PublicInbox/Eml.pm
@@ -334,6 +334,11 @@ sub body_set {
 	undef;
 }
 
+# workaround https://rt.cpan.org/Public/Bug/Display.html?id=139622
+# Encode 2.87..3.12 leaks on croak, so we defer and croak ourselves
+our @enc_warn;
+my $enc_warn = sub { push @enc_warn, @_ };
+
 sub body_str_set {
 	my ($self, $str) = @_;
 	my $cs = ct($self)->{attributes}->{charset} //
@@ -341,10 +346,10 @@ sub body_str_set {
 	my $enc = find_encoding($cs) // croak "unknown encoding `$cs'";
 	my $tmp;
 	{
-		my @w;
-		local $SIG{__WARN__} = sub { push @w, @_ };
+		local @enc_warn;
+		local $SIG{__WARN__} = $enc_warn;
 		$tmp = $enc->encode($str, Encode::FB_WARN);
-		croak(@w) if @w;
+		croak(@enc_warn) if @enc_warn;
 	};
 	body_set($self, \$tmp);
 }
@@ -471,11 +476,10 @@ sub body_str {
 	};
 	my $enc = find_encoding($cs) or croak "unknown encoding `$cs'";
 	my $tmp = body($self);
-	# workaround https://rt.cpan.org/Public/Bug/Display.html?id=139622
-	my @w;
-	local $SIG{__WARN__} = sub { push @w, @_ };
+	local @enc_warn;
+	local $SIG{__WARN__} = $enc_warn;
 	my $ret = $enc->decode($tmp, Encode::FB_WARN);
-	croak(@w) if @w;
+	croak(@enc_warn) if @enc_warn;
 	$ret;
 }
 

  reply	other threads:[~2024-02-13  9:42 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-13  9:42 [PATCH 0/2] eml: allocation reductions Eric Wong
2024-02-13  9:42 ` Eric Wong [this message]
2024-02-13  9:42 ` [PATCH 2/2] eml: reuse ->decode buffer Eric Wong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://public-inbox.org/README

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240213094242.3496966-2-e@80x24.org \
    --to=e@80x24.org \
    --cc=meta@public-inbox.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/public-inbox.git

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).