Skip to content

Commit b3617f1

Browse files
committed
Speed up -l, -L, and -c when not using -v
Since these invocations only need to know whether or not a match exists, or how many matches a file has, we don't need to scan the file line by line. This way, we can stay in C land for longer.
1 parent 96dd2fb commit b3617f1

File tree

1 file changed

+28
-10
lines changed

1 file changed

+28
-10
lines changed

ack

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -818,14 +818,24 @@ sub resource_has_match {
818818
}
819819
}
820820
else {
821-
my $opt_v = $opt->{v};
822-
my $re = $opt->{regex};
823-
while ( <$fh> ) {
824-
if (/$re/o xor $opt_v) {
825-
$has_match = 1;
826-
last;
821+
my $re = $opt->{regex};
822+
if ( $opt->{v} ) {
823+
while ( <$fh> ) {
824+
if (!/$re/o) {
825+
$has_match = 1;
826+
last;
827+
}
827828
}
828829
}
830+
else {
831+
# XXX read in chunks
832+
# XXX only do this for certain file sizes?
833+
my $content = do {
834+
local $/;
835+
<$fh>;
836+
};
837+
$has_match = $content =~ /$re/og;
838+
}
829839
close $fh;
830840
}
831841

@@ -843,10 +853,18 @@ sub count_matches_in_resource {
843853
}
844854
}
845855
else {
846-
my $opt_v = $opt->{v};
847-
my $re = $opt->{regex};
848-
while ( <$fh> ) {
849-
++$nmatches if (/$re/o xor $opt_v);
856+
my $re = $opt->{regex};
857+
if ( $opt->{v} ) {
858+
while ( <$fh> ) {
859+
++$nmatches if (!/$re/o);
860+
}
861+
}
862+
else {
863+
my $content = do {
864+
local $/;
865+
<$fh>;
866+
};
867+
$nmatches =()= ($content =~ /$re/og);
850868
}
851869
close $fh;
852870
}

0 commit comments

Comments
 (0)