unofficial mirror of meta@public-inbox.org
 help / color / mirror / Atom feed
From: Eric Wong <e@80x24.org>
To: meta@public-inbox.org
Subject: [PATCH] daemon: dedupe PublicInbox::Config objects by pathname
Date: Fri, 5 Aug 2022 08:29:54 +0000	[thread overview]
Message-ID: <20220805082954.GA14626@dcvr> (raw)

This means all Inbox, Git, Over, Msgmap, Search objects also get
deduplicated if they belong to the same config file, reducing
memory and FD usage.  This helps save memory and improve cache
hit rates in -netd setups where NNTP, IMAP, HTTP, and POP3
servers run in the same process.

InboxIdle was the only bit which needed adjustment, but there
may be other bugs lurking despite all tests passing.
---
 Pored over the read-only server code a bit to see if there's
 any problems lurking, but I haven't noticed any...

 lib/PublicInbox/Config.pm    | 9 ++++++++-
 lib/PublicInbox/Daemon.pm    | 3 +++
 lib/PublicInbox/InboxIdle.pm | 4 ++--
 3 files changed, 13 insertions(+), 3 deletions(-)

diff --git a/lib/PublicInbox/Config.pm b/lib/PublicInbox/Config.pm
index a31b5b74..ad8b8e9d 100644
--- a/lib/PublicInbox/Config.pm
+++ b/lib/PublicInbox/Config.pm
@@ -13,6 +13,7 @@ use v5.10.1;
 use PublicInbox::Inbox;
 use PublicInbox::Spawn qw(popen_rd);
 our $LD_PRELOAD = $ENV{LD_PRELOAD}; # only valid at startup
+our $DEDUPE; # set to {} to dedupe or clear cache
 
 sub _array ($) { ref($_[0]) eq 'ARRAY' ? $_[0] : [ $_[0] ] }
 
@@ -22,11 +23,17 @@ sub new {
 	my ($class, $file, $errfh) = @_;
 	$file //= default_file();
 	my $self;
+	my $set_dedupe;
 	if (ref($file) eq 'SCALAR') { # used by some tests
 		open my $fh, '<', $file or die;  # PerlIO::scalar
 		$self = config_fh_parse($fh, "\n", '=');
 		bless $self, $class;
 	} else {
+		if (-f $file && $DEDUPE) {
+			$file = rel2abs_collapsed($file);
+			$self = $DEDUPE->{$file} and return $self;
+			$set_dedupe = 1;
+		}
 		$self = git_config_dump($class, $file, $errfh);
 		$self->{'-f'} = $file;
 	}
@@ -62,7 +69,7 @@ sub new {
 	if (my $css = delete $self->{'publicinbox.css'}) {
 		$self->{css} = _array($css);
 	}
-
+	$DEDUPE->{$file} = $self if $set_dedupe;
 	$self;
 }
 
diff --git a/lib/PublicInbox/Daemon.pm b/lib/PublicInbox/Daemon.pm
index 86234771..b6f4f9ed 100644
--- a/lib/PublicInbox/Daemon.pm
+++ b/lib/PublicInbox/Daemon.pm
@@ -22,6 +22,7 @@ use PublicInbox::Sigfd;
 use PublicInbox::Git;
 use PublicInbox::GitAsyncCat;
 use PublicInbox::Eml;
+use PublicInbox::Config;
 our $SO_ACCEPTFILTER = 0x1000;
 my @CMD;
 my ($set_user, $oldset);
@@ -653,8 +654,10 @@ sub defer_accept ($$) {
 
 sub daemon_loop ($) {
 	my ($xnetd) = @_;
+	local $PublicInbox::Config::DEDUPE = {}; # enable dedupe cache
 	my $refresh = sub {
 		my ($sig) = @_;
+		%$PublicInbox::Config::DEDUPE = (); # clear cache
 		for my $xn (values %$xnetd) {
 			delete $xn->{tlsd}->{ssl_ctx}; # PublicInbox::TLS::start
 			eval { $xn->{refresh}->($sig) };
diff --git a/lib/PublicInbox/InboxIdle.pm b/lib/PublicInbox/InboxIdle.pm
index 2781b3e1..ffbbfea7 100644
--- a/lib/PublicInbox/InboxIdle.pm
+++ b/lib/PublicInbox/InboxIdle.pm
@@ -30,9 +30,9 @@ sub in2_arm ($$) { # PublicInbox::Config::each_inbox callback
 	my $old_ibx = $cur->[0];
 	$cur->[0] = $ibx;
 	if ($old_ibx) {
-		$ibx->{unlock_subs} and
-			die "BUG: $dir->{unlock_subs} should not exist";
+		my $u = $ibx->{unlock_subs};
 		$ibx->{unlock_subs} = $old_ibx->{unlock_subs};
+		%{$ibx->{unlock_subs}} = (%$u, %{$ibx->{unlock_subs}}) if $u;
 
 		# Linux::Inotify2::Watch::name matches if watches are the
 		# same, no point in replacing a watch of the same name

                 reply	other threads:[~2022-08-05  8:29 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20220805082954.GA14626@dcvr \
    --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.
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).