]> git.donarmstrong.com Git - bin.git/commitdiff
stop using IO::Uncompress::Gunzip; it's slow
authorDon Armstrong <don@donarmstrong.com>
Sat, 30 Mar 2019 23:47:59 +0000 (16:47 -0700)
committerDon Armstrong <don@donarmstrong.com>
Sat, 30 Mar 2019 23:47:59 +0000 (16:47 -0700)
postfix_grep

index da659bcc21b3f6217eb631a4fe349fa2e7512391..b10e5be4675ac6844cd228920a4738a1dae7a225 100755 (executable)
@@ -72,8 +72,6 @@ GetOptions(\%options,
 pod2usage() if $options{help};
 pod2usage({verbose=>2}) if $options{man};
 
-use IO::Uncompress::Gunzip;
-
 $DEBUG = $options{debug};
 
 if (not exists $options{regex}) {
@@ -102,12 +100,36 @@ if (not @ARGV) {
     push @ARGV,undef;
 }
 
+sub open_compressed_file {
+    my ($file,$encoding) = @_;
+    $encoding //= ':encoding(UTF-8)';
+    my $fh;
+    if (not defined $file) {
+        $fh = \*STDIN;
+        binmode($fh,':encoding(UTF-8)');
+        return $fh;
+    }
+    my $mode = "<$encoding";
+    my @opts;
+    if ($file =~ /\.gz$/) {
+        $mode = "-|$encoding";
+        push @opts,'gzip','-dc';
+    }
+    if ($file =~ /\.xz$/) {
+        $mode = "-|$encoding";
+        push @opts,'xz','-dc';
+    }
+    if ($file =~ /\.bz2$/) {
+        $mode = "-|$encoding";
+        push @opts,'bzip2','-dc';
+    }
+    open($fh,$mode,@opts,$file);
+    return $fh;
+}
+
 my %postfix_ids;
 for my $file (@ARGV) {
-    my $fh = IO::Uncompress::Gunzip->new(defined $file ? $file:\*STDIN,
-                                         MultiStream => 1,
-                                         Transparent => 1,)
-        or die "IO::Uncompress::Gunzip failed: $IO::Uncompress::Gunzip::GunzipError";
+    my $fh = open_compressed_file($file);
     while (<$fh>) {
         chomp;
         my $line = $_;