]> git.ipfire.org Git - thirdparty/public-inbox.git/commitdiff
nntp: avoid repeated rand() calls
authorEric Wong <e@80x24.org>
Thu, 27 Mar 2025 23:20:46 +0000 (23:20 +0000)
committerEric Wong <e@80x24.org>
Sun, 30 Mar 2025 18:19:32 +0000 (18:19 +0000)
We only need to generate the secret salt once, so initialize
it early to avoid potentially expensive `rand' ops in repeated
calls to wildmat2re.  We'll also stringify it early to hopefully
improve CoW sharing and reduce fragmentation.

lib/PublicInbox/NNTP.pm

index eab6d301dc80648cd7ef7f3c79a0c6d0bd07151b..07d86d6032a3454360409cb03e5725cee979b311 100644 (file)
@@ -19,6 +19,7 @@ use PublicInbox::SHA qw(sha1_hex);
 use Time::Local qw(timegm timelocal);
 use PublicInbox::GitAsyncCat;
 use PublicInbox::Address;
+my $SALT = rand . ''; # stringify early for CoW sharing
 
 use constant {
        LINE_MAX => 512, # RFC 977 section 2.3
@@ -278,11 +279,10 @@ sub wildmat2re ($) {
        my $tmp = $_[0] // '*';
        return qr/.*/ if $tmp eq '*';
        my %keep;
-       my $salt = rand;
 
        $tmp =~ s#(?<!\\)\[(.+)(?<!\\)\]#
                my $orig = $1;
-               my $key = sha1_hex($orig . $salt);
+               my $key = sha1_hex($orig . $SALT);
                $orig =~ s/([^\w\-])+/\Q$1/g;
                $keep{$key} = $orig;
                $key