From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-4.0 required=3.0 tests=ALL_TRUSTED,BAYES_00 shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id 19CFF1F4C4; Tue, 15 Oct 2019 20:36:34 +0000 (UTC) From: Eric Wong To: meta@public-inbox.org Cc: "Eric W. Biederman" Subject: [PATCH 3/7] config: simplify lookup* methods Date: Tue, 15 Oct 2019 20:36:29 +0000 Message-Id: <20191015203633.17665-4-e@80x24.org> In-Reply-To: <20191015203633.17665-1-e@80x24.org> References: <20191015203633.17665-1-e@80x24.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: This ensures we always process inboxes in section order and reduces the amount of code we have to maintain for each lookup. Avoiding the cost of inboxes object creation is not worth the code overhead; and we can implement a config cache via Storable easily for large configs and -mda users. --- lib/PublicInbox/Config.pm | 75 ++++++++------------------------------- 1 file changed, 14 insertions(+), 61 deletions(-) diff --git a/lib/PublicInbox/Config.pm b/lib/PublicInbox/Config.pm index b7e03af3..2b99346a 100644 --- a/lib/PublicInbox/Config.pm +++ b/lib/PublicInbox/Config.pm @@ -63,58 +63,24 @@ sub new { $self; } +sub _fill_all ($) { each_inbox($_[0], sub {}) } + +sub _lookup_fill ($$$) { + my ($self, $cache, $key) = @_; + $self->{$cache}->{$key} // do { + _fill_all($self); + $self->{$cache}->{$key}; + } +} + sub lookup { my ($self, $recipient) = @_; - my $addr = lc($recipient); - my $ibx = $self->{-by_addr}->{$addr}; - return $ibx if $ibx; - - my $pfx; - - foreach my $k (keys %$self) { - $k =~ m!\A(publicinbox\.[^/]+)\.address\z! or next; - my $v = $self->{$k}; - if (ref($v) eq "ARRAY") { - foreach my $alias (@$v) { - (lc($alias) eq $addr) or next; - $pfx = $1; - last; - } - } else { - (lc($v) eq $addr) or next; - $pfx = $1; - last; - } - } - defined $pfx or return; - _fill($self, $pfx); + _lookup_fill($self, '-by_addr', lc($recipient)); } sub lookup_list_id { my ($self, $list_id) = @_; - $list_id = lc($list_id); - my $ibx = $self->{-by_list_id}->{$list_id}; - return $ibx if $ibx; - - my $pfx; - - foreach my $k (keys %$self) { - $k =~ /\A(publicinbox\.[\w-]+)\.listid\z/ or next; - my $v = $self->{$k}; - if (ref($v) eq "ARRAY") { - foreach my $alias (@$v) { - (lc($alias) eq $list_id) or next; - $pfx = $1; - last; - } - } else { - (lc($v) eq $list_id) or next; - $pfx = $1; - last; - } - } - defined $pfx or return; - _fill($self, $pfx); + _lookup_fill($self, '-by_list_id', lc($list_id)); } sub lookup_name ($$) { @@ -135,20 +101,7 @@ sub each_inbox { sub lookup_newsgroup { my ($self, $ng) = @_; - $ng = lc($ng); - my $ibx = $self->{-by_newsgroup}->{$ng}; - return $ibx if $ibx; - - foreach my $k (keys %$self) { - $k =~ m!\A(publicinbox\.[^/]+)\.newsgroup\z! or next; - my $v = $self->{$k}; - my $pfx = $1; - if ($v eq $ng) { - $ibx = _fill($self, $pfx); - return $ibx; - } - } - undef; + _lookup_fill($self, '-by_newsgroup', lc($ng)); } sub limiter { @@ -461,7 +414,7 @@ sub _fill { if ($ibx->{obfuscate}) { $ibx->{-no_obfuscate} = $self->{-no_obfuscate}; $ibx->{-no_obfuscate_re} = $self->{-no_obfuscate_re}; - each_inbox($self, sub {}); # noop to populate -no_obfuscate + _fill_all($self); # noop to populate -no_obfuscate } if (my $ibx_code_repos = $ibx->{coderepo}) {