]> Sergey Matveev's repositories - public-inbox.git/commitdiff
daemon: dedupe PublicInbox::Config objects by pathname
authorEric Wong <e@80x24.org>
Fri, 5 Aug 2022 08:29:54 +0000 (08:29 +0000)
committerEric Wong <e@80x24.org>
Sat, 6 Aug 2022 06:34:45 +0000 (06:34 +0000)
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.

lib/PublicInbox/Config.pm
lib/PublicInbox/Daemon.pm
lib/PublicInbox/InboxIdle.pm

index a31b5b74c7c7282ba1e3bb83e7fcc6603b7427bb..ad8b8e9dc18cdecb87e44a6aa1fe81ed20692f03 100644 (file)
@@ -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;
 }
 
index 862347715e277f7a5cdafe01fe2ecf25d8d0b9b9..b6f4f9ed06123b5555a7516e4591e8eaeae2e20d 100644 (file)
@@ -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) };
index 2781b3e15bb96c290be28169009705a941662d08..ffbbfea7c51c2c554e7ed951fd8e545f0c7ca190 100644 (file)
@@ -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