user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
* [PATCH 0/2] eml: allocation reductions
@ 2024-02-13  9:42 Eric Wong
  2024-02-13  9:42 ` [PATCH 1/2] eml: avoid anonymous __WARN__ sub for encode/decode Eric Wong
  2024-02-13  9:42 ` [PATCH 2/2] eml: reuse ->decode buffer Eric Wong
  0 siblings, 2 replies; 3+ messages in thread
From: Eric Wong @ 2024-02-13  9:42 UTC (permalink / raw)
  To: meta

1/2 is obvious, 2/2 is aspirational dream territory...
(been dreaming up a faster, alternative run-time for Perl :P)

Eric Wong (2):
  eml: avoid anonymous __WARN__ sub for encode/decode
  eml: reuse ->decode buffer

 lib/PublicInbox/Eml.pm | 22 +++++++++++++---------
 1 file changed, 13 insertions(+), 9 deletions(-)

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

* [PATCH 1/2] eml: avoid anonymous __WARN__ sub for encode/decode
  2024-02-13  9:42 [PATCH 0/2] eml: allocation reductions Eric Wong
@ 2024-02-13  9:42 ` Eric Wong
  2024-02-13  9:42 ` [PATCH 2/2] eml: reuse ->decode buffer Eric Wong
  1 sibling, 0 replies; 3+ messages in thread
From: Eric Wong @ 2024-02-13  9:42 UTC (permalink / raw)
  To: meta

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;
 }
 

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

* [PATCH 2/2] eml: reuse ->decode buffer
  2024-02-13  9:42 [PATCH 0/2] eml: allocation reductions Eric Wong
  2024-02-13  9:42 ` [PATCH 1/2] eml: avoid anonymous __WARN__ sub for encode/decode Eric Wong
@ 2024-02-13  9:42 ` Eric Wong
  1 sibling, 0 replies; 3+ messages in thread
From: Eric Wong @ 2024-02-13  9:42 UTC (permalink / raw)
  To: meta

It's not really relevant at the moment, but a sufficiently
smart implementation could eventually save some memory here.
Perl already optimizes in-place sort (@x = sort @x), so there's
precedent for a potential future where a Perl implementation
could generally optimize in-place operations for non-builtin
subroutines, too.
---
 lib/PublicInbox/Eml.pm | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/lib/PublicInbox/Eml.pm b/lib/PublicInbox/Eml.pm
index 56aec1e5..d59d7c3f 100644
--- a/lib/PublicInbox/Eml.pm
+++ b/lib/PublicInbox/Eml.pm
@@ -475,10 +475,10 @@ sub body_str {
 			join("\n\t", header_raw($self, 'Content-Type')));
 	};
 	my $enc = find_encoding($cs) or croak "unknown encoding `$cs'";
-	my $tmp = body($self);
+	my $ret = body($self);
 	local @enc_warn;
 	local $SIG{__WARN__} = $enc_warn;
-	my $ret = $enc->decode($tmp, Encode::FB_WARN);
+	$ret = $enc->decode($ret, Encode::FB_WARN);
 	croak(@enc_warn) if @enc_warn;
 	$ret;
 }

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

end of thread, other threads:[~2024-02-13  9:42 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-02-13  9:42 [PATCH 0/2] eml: allocation reductions Eric Wong
2024-02-13  9:42 ` [PATCH 1/2] eml: avoid anonymous __WARN__ sub for encode/decode Eric Wong
2024-02-13  9:42 ` [PATCH 2/2] eml: reuse ->decode buffer Eric Wong

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