Skip to content

Commit

Permalink
Apply pull-req from James Rouzier regarding speedup.
Browse files Browse the repository at this point in the history
  • Loading branch information
shlomif committed Jul 10, 2015
1 parent d77e8db commit 3636592
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
5 changes: 5 additions & 0 deletions config-inifiles/Changes
@@ -1,3 +1,8 @@
2.88 Unknown
* Apply speedup patch from James Rouzier.
- https://bitbucket.org/shlomif/perl-config-inifiles/pull-request/4/avoid-searching-through-all-the-sections/commits
- Avoids it being O(n^2).

2.87 2015-06-16
* Replace List::MoreUtils with List::Util-1.33-or-above.
- We only used any() and none() which exist in List::Util too now.
Expand Down
16 changes: 11 additions & 5 deletions config-inifiles/lib/Config/IniFiles.pm
Expand Up @@ -1235,6 +1235,8 @@ sub ReadConfig
$self->{mysects} = []; # A pair of hashes to remember which params are loaded
$self->{myparms} = {}; # or set using the API vs. imported - useful for
$self->{peCMT} = {}; # this will store trailing comments at the end of single-lined params
$self->{e} = {}; # If a section is already exists
$self->{mye} = {}; # If a section is already exists
# import shadowing, see below, and WriteConfig($fn, -delta=>1)

if( defined $self->{imported} ) {
Expand All @@ -1244,7 +1246,7 @@ sub ReadConfig
# This is only needed on a re-load though
$self->{imported}->ReadConfig() unless ($self->{firstload});

foreach my $field (qw(sects parms group v sCMT pCMT EOT)) {
foreach my $field (qw(sects parms group v sCMT pCMT EOT e)) {
$self->{$field} = _deepcopy($self->{imported}->{$field});
}
} # end if
Expand Down Expand Up @@ -1341,7 +1343,7 @@ sub SectionExists {

$self->_caseify(\$sect);

return ((any { $_ eq $sect } @{$self->{sects}}) ? 1 : 0);
return ((exists $self->{e}{$sect}) ? 1 : 0);
}

=head2 AddSection ( $sect_name )
Expand All @@ -1358,7 +1360,7 @@ the name that you're adding isn't in the list of sections already.
sub _AddSection_Helper
{
my ($self, $sect) = @_;

$self->{e}{$sect} = 1;
CORE::push @{$self->{sects}}, $sect;
$self->_touch_section($sect);

Expand Down Expand Up @@ -1398,9 +1400,10 @@ sub _touch_section {

$self->{mysects} ||= [];

if (none { $_ eq $sect } @{$self->{mysects}})
unless (exists $self->{mye}{$sect})
{
CORE::push @{$self->{mysects}}, $sect;
$self->{mye}{$sect} = 1;
}

return;
Expand Down Expand Up @@ -1444,6 +1447,7 @@ sub DeleteSection {
delete $self->{EOT}{$sect};
delete $self->{parms}{$sect};
delete $self->{myparms}{$sect};
delete $self->{e}{$sect};

$self->{sects} = [grep {$_ ne $sect} @{$self->{sects}}];
$self->_touch_section($sect);
Expand Down Expand Up @@ -1492,7 +1496,7 @@ sub CopySection {
$self->_AddSection_Helper($new_sect);

# This is done the fast way, change if data structure changes!!
foreach my $key (qw(v sCMT pCMT EOT parms myparms)) {
foreach my $key (qw(v sCMT pCMT EOT parms myparms e)) {
next unless exists $self->{$key}{$old_sect};
$self->{$key}{$new_sect} = Config::IniFiles::_deepcopy($self->{$key}{$old_sect});
}
Expand Down Expand Up @@ -3136,6 +3140,8 @@ data structure.
->{EOT}{$sect}{$parm} = "end of text string"
->{pCMT}{$sect}{$parm} = \@comment_lines
->{v}{$sect}{$parm} = $value OR \@values
->{e}{$sect} = 1 OR does not exist
->{mye}{$sect} = 1 OR does not exists
=head1 AUTHOR and ACKNOWLEDGEMENTS
Expand Down

0 comments on commit 3636592

Please sign in to comment.