]> git.ipfire.org Git - thirdparty/public-inbox.git/commitdiff
send_cmd: throttle `sleeping on sendmsg' messages
authorEric Wong <e@80x24.org>
Fri, 29 Nov 2024 23:54:00 +0000 (23:54 +0000)
committerEric Wong <e@80x24.org>
Sat, 30 Nov 2024 22:43:35 +0000 (22:43 +0000)
Emitting a message every 100ms was too much for busy machines.
Throttle it to 1.6s to avoid flooding user terminals by emitting
every 16 sleeps.  A power-of-two (16) was chosen for its
optimization potential via bitwise AND.  Since perl(1) doesn't
do this optimization, we open code the bitwise AND.  I don't
assume an optimizing compiler for Inline::C, either, since I
find working in C far more enjoyable with optimizations off.

lib/PublicInbox/CmdIPC4.pm
lib/PublicInbox/Spawn.pm

index bdf17a3f58d7f3f2fa7196bb61802a711bdad3c2..af49f36d35710d3a385a1a346a90ef0312ab1b33 100644 (file)
@@ -12,7 +12,8 @@ sub sendmsg_retry ($) {
        return 1 if $!{EINTR};
        return unless ($!{ENOMEM} || $!{ENOBUFS} || $!{ETOOMANYREFS});
        return if $_[0]-- == 0;
-       warn "# sleeping on sendmsg: $! ($_[0] tries left)\n";
+       # n.b. `N & (power-of-two - 1)' is a faster `N % power-of-two'
+       warn "# sleeping on sendmsg: $! ($_[0] tries left)\n" if !($_[0] & 15);
        select(undef, undef, undef, 0.1);
        1;
 }
index 1c6cc5becb826feb2fdcd0a9cb9574f6b50d1117..d818af0f1675b6ac770850bf0a4c2740804fbf0c 100644 (file)
@@ -184,8 +184,10 @@ static int sendmsg_retry(long *tries)
        case EINTR: PERL_ASYNC_CHECK(); return 1;
        case ENOBUFS: case ENOMEM: case ETOOMANYREFS:
                if (*tries-- == 0) return 0;
-               fprintf(stderr, "# sleeping on sendmsg: %s (%ld tries left)\n",
-                       strerror(err), *tries);
+               if (!(*tries & 15))
+                       fprintf(stderr,
+                               "# sleeping on sendmsg: %s (%ld tries left)\n",
+                               strerror(err), *tries);
                nanosleep(&req, NULL);
                PERL_ASYNC_CHECK();
                return 1;