From: Eric Wong <e@80x24.org>
To: meta@public-inbox.org
Subject: [PATCH 09/10] config: config_fh_parse: micro-optimize
Date: Wed, 23 Dec 2020 08:38:52 +0000 [thread overview]
Message-ID: <20201223083853.30721-10-e@80x24.org> (raw)
In-Reply-To: <20201223083853.30721-1-e@80x24.org>
We can avoid a slow regexp capture and instead and rely on
rindex + substr to extract the section from the config file.
Then we use the defined-or-assignment (//=) operator combined
with the documented return value of `push' to ensure @section_order
is unique without repeating a hash lookup.
Finally, we avoid short-lived variables inside the loop and
declare them subroutine-wide to knock a teeny bit of allocation
time.
Combined, these optimizations bring the ~1.22s
PublicInbox::Config->new time down to ~0.98s with 50K inboxes.
---
lib/PublicInbox/Config.pm | 17 ++++++-----------
1 file changed, 6 insertions(+), 11 deletions(-)
diff --git a/lib/PublicInbox/Config.pm b/lib/PublicInbox/Config.pm
index 4d143c6e..60107d45 100644
--- a/lib/PublicInbox/Config.pm
+++ b/lib/PublicInbox/Config.pm
@@ -132,20 +132,15 @@ sub default_file {
sub config_fh_parse ($$$) {
my ($fh, $rs, $fs) = @_;
- my %rv;
- my (%section_seen, @section_order);
+ my (%rv, %section_seen, @section_order, $line, $k, $v, $section, $cur);
local $/ = $rs;
- while (defined(my $line = <$fh>)) {
+ while (defined($line = <$fh>)) { # performance critical with giant configs
chomp $line;
- my ($k, $v) = split($fs, $line, 2);
- my ($section) = ($k =~ /\A(\S+)\.[^\.]+\z/);
- unless (defined $section_seen{$section}) {
- $section_seen{$section} = 1;
- push @section_order, $section;
- }
+ ($k, $v) = split($fs, $line, 2);
+ $section = substr($k, 0, rindex($k, '.'));
+ $section_seen{$section} //= push(@section_order, $section);
- my $cur = $rv{$k};
- if (defined $cur) {
+ if (defined($cur = $rv{$k})) {
if (ref($cur) eq "ARRAY") {
push @$cur, $v;
} else {
next prev parent reply other threads:[~2020-12-23 8:38 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-12-23 8:38 [PATCH 00/10] start optimizing startup w/ ALL->misc Eric Wong
2020-12-23 8:38 ` [PATCH 01/10] miscsearch: load Xapian at initialization Eric Wong
2020-12-23 8:38 ` [PATCH 02/10] xt: add create-many-inboxes helper test Eric Wong
2020-12-23 8:38 ` [PATCH 03/10] inbox: git_epoch: correct false comment Eric Wong
2020-12-23 8:38 ` [PATCH 04/10] inboxwritable: _init_v1: set created_at ASAP Eric Wong
2020-12-23 8:38 ` [PATCH 05/10] miscsearch: index UIDVALIDITY, use as startup cache Eric Wong
2020-12-23 8:38 ` [PATCH 06/10] extsearchidx: close SQLite handles after attaching Eric Wong
2020-12-23 8:38 ` [PATCH 07/10] config: _fill: inbox name extraction optimization Eric Wong
2020-12-23 8:38 ` [PATCH 08/10] config: git_config_dump: pre-compile RE for split Eric Wong
2020-12-23 8:38 ` Eric Wong [this message]
2020-12-23 8:38 ` [PATCH 10/10] config: config_fh_parse: micro-optimize harder Eric Wong
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=20201223083853.30721-10-e@80x24.org \
--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.
Code repositories for project(s) associated with this public inbox
https://80x24.org/public-inbox.git
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).