about summary refs log tree commit homepage
path: root/script
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2020-12-31 13:51:51 +0000
committerEric Wong <e@80x24.org>2021-01-01 05:00:40 +0000
commitc3bdb8f03474d35ec8904f1758c4302159adfa57 (patch)
tree51bca9494e132dbb6bcf4c9c5fcef16929632f42 /script
parent52df89ced31a5685d37e17c62ccccd3db5aa3e62 (diff)
downloadpublic-inbox-c3bdb8f03474d35ec8904f1758c4302159adfa57.tar.gz
Spawn was designed to speed up process spawning inside
long-lived daemons with largish memory usage.  It does not help
for short-lived scripts which only exist to start and connect to
a daemon.

This change actually speeds up initial lei startup from
~190ms to ~140ms(!).  Normal usage once the daemon is running
is unaffected, at <20ms for help text.

While we're in the area, simplify Cwd error message generation,
too.
Diffstat (limited to 'script')
-rwxr-xr-xscript/lei17
1 files changed, 6 insertions, 11 deletions
diff --git a/script/lei b/script/lei
index ceaf1e00..0457adfd 100755
--- a/script/lei
+++ b/script/lei
@@ -21,18 +21,13 @@ if (my ($sock, $pwd) = eval {
         my $addr = pack_sockaddr_un($path);
         socket(my $sock, AF_UNIX, SOCK_STREAM, 0) or die "socket: $!";
         unless (connect($sock, $addr)) { # start the daemon if not started
-                my $cmd = [ $^X, qw[-MPublicInbox::LEI
+                local $ENV{PERL5LIB} = join(':', @INC);
+                open(my $daemon, '-|', $^X, qw[-MPublicInbox::LEI
                         -E PublicInbox::LEI::lazy_start(@ARGV)],
-                        $path, $! + 0 ];
-                my $env = { PERL5LIB => join(':', @INC) };
-                pipe(my ($daemon, $w)) or die "pipe: $!";
-                my $opt = { 1 => $w, 2 => $w };
-                require PublicInbox::Spawn;
-                my $pid = PublicInbox::Spawn::spawn($cmd, $env, $opt);
-                $opt = $w = undef;
+                        $path, $! + 0) or die "popen: $!";
                 while (<$daemon>) { warn $_ } # EOF when STDERR is redirected
-                waitpid($pid, 0) or warn <<"";
-lei-daemon could not start, PID:$pid exited with \$?=$?
+                close($daemon) or warn <<"";
+lei-daemon could not start, exited with \$?=$?
 
                 # try connecting again anyways, unlink+bind may be racy
                 unless (connect($sock, $addr)) {
@@ -43,8 +38,8 @@ Falling back to (slow) one-shot mode
                 }
         }
         require Cwd;
-        my $cwd = Cwd::fastcwd() // die "fastcwd(PWD=".($ENV{PWD}//'').": $!";
         my $pwd = $ENV{PWD} // '';
+        my $cwd = Cwd::fastcwd() // die "fastcwd(PWD=$pwd): $!";
         if ($pwd ne $cwd) { # prefer ENV{PWD} if it's a symlink to real cwd
                 my @st_cwd = stat($cwd) or die "stat(cwd=$cwd): $!";
                 my @st_pwd = stat($pwd); # PWD invalid, use cwd