diff options
author | Andreas Guldstrand <andreas.guldstrand@gmail.com> | 2017-05-30 02:11:45 +0200 |
---|---|---|
committer | Andreas Guldstrand <andreas.guldstrand@gmail.com> | 2017-05-30 02:16:20 +0200 |
commit | 00047a7a8c312f3bba73f7a8f09dd751f96fdb31 (patch) | |
tree | e21eaa552f88a037a5521404628baa385aac0f6b | |
parent | e6f5094d9eaf2df9c48c2d6ae9e0ecd2f71263ec (diff) | |
download | sbotools-00047a7a8c312f3bba73f7a8f09dd751f96fdb31.tar.xz |
sbofind: optimise searching
On my machine, before this commit, 'sbofind R' would spend
2.5 minutes searching, 6.5 minutes outputting results
After this commit, 'sbofind R' would spend
4 seconds searching, 6.5 minutes outputting results
See #48.
-rwxr-xr-x | sbofind | 47 |
1 files changed, 28 insertions, 19 deletions
@@ -97,28 +97,37 @@ sub perform_search { } my (%local, @findings); FIRST: while (my $line = <$fh>) { - next FIRST unless $line =~ /NAME:/; + if ($line =~ /NAME:\s+(.*)$/) { + my $name = $1; - # Try to match either the search string or any of the names from the TAGS.txt - foreach my $search ($search_name_re, map { quotemeta } @names) { - if (my ($name) = $line =~ /NAME:\s+($search)$/i) { + # Try to match either one of the names from TAGS.txt or the search string - # If the name matches a local override, use its location - if ($config{LOCAL_OVERRIDES} ne 'FALSE' and -d "$config{LOCAL_OVERRIDES}/$name") { - push @findings, {name => $name, location => "$config{LOCAL_OVERRIDES}/$name", local => 1 }; - $local{$name} = 1; - next FIRST; - } + my $names = @names; + # Whenever we find an element equal to $name, throw it away (and + # replace with last element rather than shifting stuff around) + for (reverse @names) { $_ = pop @names if $_ eq $name; } + + # next if $name didn't match either one of @names or $search_name_re + if ($names == @names and $name !~ /$search_name_re/i) { next FIRST; } + + # We only reach this point if $name matched one of @names, or if + # $search_name_re matched - # Otherwise the location should be in the next line - LOCATION: { - my $loc_line = <$fh>; - if (my ($location) = $loc_line =~ $loc_regex) { - push @findings, {name => $name, location => $repo_path . $location}; - next FIRST; - } else { - redo LOCATION; # But if it isn't, we try again... - } + # If the name matches a local override, use its location + if ($config{LOCAL_OVERRIDES} ne 'FALSE' and -d "$config{LOCAL_OVERRIDES}/$name") { + push @findings, {name => $name, location => "$config{LOCAL_OVERRIDES}/$name", local => 1 }; + $local{$name} = 1; + next FIRST; + } + + # Otherwise the location should be in the next line + LOCATION: { + my $loc_line = <$fh>; + if (my ($location) = $loc_line =~ $loc_regex) { + push @findings, {name => $name, location => $repo_path . $location}; + next FIRST; + } else { + redo LOCATION; # But if it isn't, we try again... } } } |