unofficial mirror of meta@public-inbox.org
 help / color / mirror / Atom feed
* [PATCH 0/7] various op/allocation golfing
@ 2022-08-04  8:16 Eric Wong
  2022-08-04  8:16 ` [PATCH 1/7] http: coerce SERVER_PORT to integer Eric Wong
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: Eric Wong @ 2022-08-04  8:16 UTC (permalink / raw)
  To: meta

Nothing major, but some of these reduce Perl internal pad space,
which tends to add up across codebases with many cold paths.

Eric Wong (7):
  http: coerce SERVER_PORT to integer
  over: get_xref3: modify rows in-place
  isearch: mset_to_artnums: avoid unnecessary ops
  lei_overview: remove pointless map {} op
  imap: ensure_slices_exist: drop needless map and array
  feed: avoid unnecessary map loop in non-over path
  view: avoid intermediate array when streaming thread

 lib/PublicInbox/Feed.pm        | 13 ++++++-------
 lib/PublicInbox/HTTP.pm        |  2 +-
 lib/PublicInbox/IMAP.pm        |  7 ++-----
 lib/PublicInbox/Isearch.pm     |  5 ++---
 lib/PublicInbox/LeiOverview.pm |  2 +-
 lib/PublicInbox/Over.pm        |  8 ++++----
 lib/PublicInbox/View.pm        |  4 ++--
 7 files changed, 18 insertions(+), 23 deletions(-)

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

* [PATCH 1/7] http: coerce SERVER_PORT to integer
  2022-08-04  8:16 [PATCH 0/7] various op/allocation golfing Eric Wong
@ 2022-08-04  8:16 ` Eric Wong
  2022-08-04  8:16 ` [PATCH 2/7] over: get_xref3: modify rows in-place Eric Wong
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: Eric Wong @ 2022-08-04  8:16 UTC (permalink / raw)
  To: meta

This may save a few bytes with many connected clients.
Noticed while working on the JMAP endpoint.
---
 lib/PublicInbox/HTTP.pm | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/PublicInbox/HTTP.pm b/lib/PublicInbox/HTTP.pm
index 669211e3..3d4e3499 100644
--- a/lib/PublicInbox/HTTP.pm
+++ b/lib/PublicInbox/HTTP.pm
@@ -135,7 +135,7 @@ sub app_dispatch {
 	$env->{REMOTE_ADDR} = $self->{remote_addr};
 	$env->{REMOTE_PORT} = $self->{remote_port};
 	if (defined(my $host = $env->{HTTP_HOST})) {
-		$host =~ s/:([0-9]+)\z// and $env->{SERVER_PORT} = $1;
+		$host =~ s/:([0-9]+)\z// and $env->{SERVER_PORT} = $1 + 0;
 		$env->{SERVER_NAME} = $host;
 	}
 	if (defined $input) {

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

* [PATCH 2/7] over: get_xref3: modify rows in-place
  2022-08-04  8:16 [PATCH 0/7] various op/allocation golfing Eric Wong
  2022-08-04  8:16 ` [PATCH 1/7] http: coerce SERVER_PORT to integer Eric Wong
@ 2022-08-04  8:16 ` Eric Wong
  2022-08-04  8:16 ` [PATCH 3/7] isearch: mset_to_artnums: avoid unnecessary ops Eric Wong
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: Eric Wong @ 2022-08-04  8:16 UTC (permalink / raw)
  To: meta

There's no need to create two intermediate arrays when we can
modify the existing arrayref.
---
 lib/PublicInbox/Over.pm | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/lib/PublicInbox/Over.pm b/lib/PublicInbox/Over.pm
index 786f9d92..d6409b2a 100644
--- a/lib/PublicInbox/Over.pm
+++ b/lib/PublicInbox/Over.pm
@@ -273,13 +273,13 @@ SELECT ibx_id,xnum,oidbin FROM xref3 WHERE docid = ? ORDER BY ibx_id,xnum ASC
 	my $eidx_key_sth = $dbh->prepare_cached(<<'', undef, 1);
 SELECT eidx_key FROM inboxes WHERE ibx_id = ?
 
-	[ map {
-		my $r = $_;
+	for my $r (@$rows) {
 		$eidx_key_sth->execute($r->[0]);
 		my $eidx_key = $eidx_key_sth->fetchrow_array;
 		$eidx_key //= "missing://ibx_id=$r->[0]";
-		"$eidx_key:$r->[1]:".unpack('H*', $r->[2]);
-	} @$rows ];
+		$r = "$eidx_key:$r->[1]:".unpack('H*', $r->[2]);
+	}
+	$rows;
 }
 
 sub next_by_mid {

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

* [PATCH 3/7] isearch: mset_to_artnums: avoid unnecessary ops
  2022-08-04  8:16 [PATCH 0/7] various op/allocation golfing Eric Wong
  2022-08-04  8:16 ` [PATCH 1/7] http: coerce SERVER_PORT to integer Eric Wong
  2022-08-04  8:16 ` [PATCH 2/7] over: get_xref3: modify rows in-place Eric Wong
@ 2022-08-04  8:16 ` Eric Wong
  2022-08-04  8:17 ` [PATCH 4/7] lei_overview: remove pointless map {} op Eric Wong
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: Eric Wong @ 2022-08-04  8:16 UTC (permalink / raw)
  To: meta

We can use DBI's selectcol_arrayref directly (as we do in other
places) to avoid unnecessary arrays and ops on our end.
---
 lib/PublicInbox/Isearch.pm | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/lib/PublicInbox/Isearch.pm b/lib/PublicInbox/Isearch.pm
index df940e76..2b45e08e 100644
--- a/lib/PublicInbox/Isearch.pm
+++ b/lib/PublicInbox/Isearch.pm
@@ -69,12 +69,11 @@ sub mset_to_artnums {
 			$range = 'AND xnum >= ? AND xnum <= ?';
 			@r = @$r;
 		}
-		my $rows = $self->{es}->over->dbh->
-			selectall_arrayref(<<"", undef, $ibx_id, @$docids, @r);
+		return $self->{es}->over->dbh->
+			selectcol_arrayref(<<"", undef, $ibx_id, @$docids, @r);
 SELECT xnum FROM xref3 WHERE ibx_id = ? AND docid IN ($qmarks) $range
 ORDER BY xnum ASC
 
-		return [ map { $_->[0] } @$rows ];
 	}
 
 	my $rows = $self->{es}->over->dbh->

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

* [PATCH 4/7] lei_overview: remove pointless map {} op
  2022-08-04  8:16 [PATCH 0/7] various op/allocation golfing Eric Wong
                   ` (2 preceding siblings ...)
  2022-08-04  8:16 ` [PATCH 3/7] isearch: mset_to_artnums: avoid unnecessary ops Eric Wong
@ 2022-08-04  8:17 ` Eric Wong
  2022-08-04  8:17 ` [PATCH 5/7] imap: ensure_slices_exist: drop needless map and array Eric Wong
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: Eric Wong @ 2022-08-04  8:17 UTC (permalink / raw)
  To: meta

We can rely on //g and autovivification, here.
---
 lib/PublicInbox/LeiOverview.pm | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/PublicInbox/LeiOverview.pm b/lib/PublicInbox/LeiOverview.pm
index 2d3db9f4..066c40bd 100644
--- a/lib/PublicInbox/LeiOverview.pm
+++ b/lib/PublicInbox/LeiOverview.pm
@@ -143,7 +143,7 @@ sub _unbless_smsg {
 	$smsg->{dt} = iso8601(delete $smsg->{ds}); # JMAP UTCDate
 	$smsg->{pct} = get_pct($mitem) if $mitem;
 	if (my $r = delete $smsg->{references}) {
-		$smsg->{refs} = [ map { $_ } ($r =~ m/$MID_EXTRACT/go) ];
+		@{$smsg->{refs}} = ($r =~ m/$MID_EXTRACT/go);
 	}
 	if (my $m = delete($smsg->{mid})) {
 		$smsg->{'m'} = $m;

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

* [PATCH 5/7] imap: ensure_slices_exist: drop needless map and array
  2022-08-04  8:16 [PATCH 0/7] various op/allocation golfing Eric Wong
                   ` (3 preceding siblings ...)
  2022-08-04  8:17 ` [PATCH 4/7] lei_overview: remove pointless map {} op Eric Wong
@ 2022-08-04  8:17 ` Eric Wong
  2022-08-04  8:17 ` [PATCH 6/7] feed: avoid unnecessary map loop in non-over path Eric Wong
  2022-08-04  8:17 ` [PATCH 7/7] view: avoid intermediate array when streaming thread Eric Wong
  6 siblings, 0 replies; 8+ messages in thread
From: Eric Wong @ 2022-08-04  8:17 UTC (permalink / raw)
  To: meta

We can reduce ops and temporary objects here by folding the
stringification into the `for' loop and push directly into the
{mailboxlist} array; relying on autovivification to turn it into
a noop for the initial population.
---
 lib/PublicInbox/IMAP.pm | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)

diff --git a/lib/PublicInbox/IMAP.pm b/lib/PublicInbox/IMAP.pm
index 9955984b..bed633e5 100644
--- a/lib/PublicInbox/IMAP.pm
+++ b/lib/PublicInbox/IMAP.pm
@@ -354,17 +354,14 @@ sub ensure_slices_exist ($$) {
 	my ($imapd, $ibx) = @_;
 	my $mb_top = $ibx->{newsgroup} // return;
 	my $mailboxes = $imapd->{mailboxes};
-	my @created;
+	my $list = $imapd->{mailboxlist}; # may be undef, just autoviv + noop
 	for (my $i = int($ibx->art_max/UID_SLICE); $i >= 0; --$i) {
 		my $sub_mailbox = "$mb_top.$i";
 		last if exists $mailboxes->{$sub_mailbox};
 		$mailboxes->{$sub_mailbox} = $ibx;
 		$sub_mailbox =~ s/\Ainbox\./INBOX./i; # more familiar to users
-		push @created, $sub_mailbox;
+		push @$list, qq[* LIST (\\HasNoChildren) "." $sub_mailbox\r\n]
 	}
-	return unless @created;
-	my $l = $imapd->{mailboxlist} or return;
-	push @$l, map { qq[* LIST (\\HasNoChildren) "." $_\r\n] } @created;
 }
 
 sub inbox_lookup ($$;$) {

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

* [PATCH 6/7] feed: avoid unnecessary map loop in non-over path
  2022-08-04  8:16 [PATCH 0/7] various op/allocation golfing Eric Wong
                   ` (4 preceding siblings ...)
  2022-08-04  8:17 ` [PATCH 5/7] imap: ensure_slices_exist: drop needless map and array Eric Wong
@ 2022-08-04  8:17 ` Eric Wong
  2022-08-04  8:17 ` [PATCH 7/7] view: avoid intermediate array when streaming thread Eric Wong
  6 siblings, 0 replies; 8+ messages in thread
From: Eric Wong @ 2022-08-04  8:17 UTC (permalink / raw)
  To: meta

We can bless objects while doing the initial insertion to avoid
extra the extra map iteration and temporary array(s).  Fewer ops
means memory savings for the likely case of ->over users, too.
---
 lib/PublicInbox/Feed.pm | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)

diff --git a/lib/PublicInbox/Feed.pm b/lib/PublicInbox/Feed.pm
index b2219dad..ee579f6d 100644
--- a/lib/PublicInbox/Feed.pm
+++ b/lib/PublicInbox/Feed.pm
@@ -1,10 +1,10 @@
-# Copyright (C) 2013-2021 all contributors <meta@public-inbox.org>
+# Copyright (C) all contributors <meta@public-inbox.org>
 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
 #
 # Used for generating Atom feeds for web-accessible mailing list archives.
 package PublicInbox::Feed;
 use strict;
-use warnings;
+use v5.10.1;
 use PublicInbox::View;
 use PublicInbox::WwwAtomStream;
 use PublicInbox::Smsg; # this loads w/o Search::Xapian
@@ -108,13 +108,13 @@ sub recent_msgs {
 	my $last;
 	my $last_commit;
 	local $/ = "\n";
-	my @oids;
+	my @ret;
 	while (defined(my $line = <$log>)) {
 		if ($line =~ /$addmsg/o) {
 			my $add = $1;
 			next if $deleted{$add}; # optimization-only
-			push @oids, $add;
-			if (scalar(@oids) >= $max) {
+			push(@ret, bless { blob => $add }, 'PublicInbox::Smsg');
+			if (scalar(@ret) >= $max) {
 				$last = 1;
 				last;
 			}
@@ -136,8 +136,7 @@ sub recent_msgs {
 	$last_commit and
 		$ctx->{next_page} = qq[<a\nhref="?r=$last_commit"\nrel=next>] .
 					'next (older)</a>';
-
-	[ map { bless {blob => $_ }, 'PublicInbox::Smsg' } @oids ];
+	\@ret;
 }
 
 1;

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

* [PATCH 7/7] view: avoid intermediate array when streaming thread
  2022-08-04  8:16 [PATCH 0/7] various op/allocation golfing Eric Wong
                   ` (5 preceding siblings ...)
  2022-08-04  8:17 ` [PATCH 6/7] feed: avoid unnecessary map loop in non-over path Eric Wong
@ 2022-08-04  8:17 ` Eric Wong
  6 siblings, 0 replies; 8+ messages in thread
From: Eric Wong @ 2022-08-04  8:17 UTC (permalink / raw)
  To: meta

We can rely on auto-vivification to avoid an intermediate
array for the map result.
---
 lib/PublicInbox/View.pm | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/lib/PublicInbox/View.pm b/lib/PublicInbox/View.pm
index fa96cca3..26094082 100644
--- a/lib/PublicInbox/View.pm
+++ b/lib/PublicInbox/View.pm
@@ -1,4 +1,4 @@
-# Copyright (C) 2014-2021 all contributors <meta@public-inbox.org>
+# Copyright (C) all contributors <meta@public-inbox.org>
 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
 #
 # Used for displaying the HTML web interface.
@@ -429,7 +429,7 @@ sub stream_thread_i { # PublicInbox::WwwStream::getline callback
 
 sub stream_thread ($$) {
 	my ($rootset, $ctx) = @_;
-	$ctx->{-queue} = [ map { (0, $_) } @$rootset ];
+	@{$ctx->{-queue}} = map { (0, $_) } @$rootset;
 	PublicInbox::WwwStream::aresponse($ctx, 200, \&stream_thread_i);
 }
 

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

end of thread, other threads:[~2022-08-04  8:17 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-04  8:16 [PATCH 0/7] various op/allocation golfing Eric Wong
2022-08-04  8:16 ` [PATCH 1/7] http: coerce SERVER_PORT to integer Eric Wong
2022-08-04  8:16 ` [PATCH 2/7] over: get_xref3: modify rows in-place Eric Wong
2022-08-04  8:16 ` [PATCH 3/7] isearch: mset_to_artnums: avoid unnecessary ops Eric Wong
2022-08-04  8:17 ` [PATCH 4/7] lei_overview: remove pointless map {} op Eric Wong
2022-08-04  8:17 ` [PATCH 5/7] imap: ensure_slices_exist: drop needless map and array Eric Wong
2022-08-04  8:17 ` [PATCH 6/7] feed: avoid unnecessary map loop in non-over path Eric Wong
2022-08-04  8:17 ` [PATCH 7/7] view: avoid intermediate array when streaming thread Eric Wong

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