about summary refs log tree commit homepage
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2019-02-02 07:35:06 +0000
committerEric Wong <e@80x24.org>2019-02-05 10:58:35 +0000
commit8eb2c4f7c0aed11810280c6dfa63f75fd961e3cd (patch)
tree8544eeb37994389de4bde873bb9d023d2cb465ba
parent58db2bbcbc9836fe36c91eff761e31edfd53f866 (diff)
downloadpublic-inbox-8eb2c4f7c0aed11810280c6dfa63f75fd961e3cd.tar.gz
It turns out there's no point in having multiple instances of
this or having to worry about destruction or destruction
ordering.

This will make it easier to reuse the one instance we have
across different modules.
-rw-r--r--lib/PublicInbox/HlMod.pm33
-rw-r--r--lib/PublicInbox/ViewVCS.pm5
-rw-r--r--t/hl_mod.t15
3 files changed, 13 insertions, 40 deletions
diff --git a/lib/PublicInbox/HlMod.pm b/lib/PublicInbox/HlMod.pm
index 284e4b18..13f27d19 100644
--- a/lib/PublicInbox/HlMod.pm
+++ b/lib/PublicInbox/HlMod.pm
@@ -16,6 +16,7 @@ package PublicInbox::HlMod;
 use strict;
 use warnings;
 use highlight; # SWIG-generated stuff
+my $hl;
 
 sub _parse_filetypes ($) {
         my $ft_conf = $_[0]->searchFile('filetypes.conf') or
@@ -52,16 +53,20 @@ sub _parse_filetypes ($) {
         (\%ext2lang, \@shebang);
 }
 
+# We only need one instance, so we don't need to do
+# highlight::CodeGenerator::deleteInstance
 sub new {
         my ($class) = @_;
-        my $dir = highlight::DataDir->new;
-        $dir->initSearchDirectories('');
-        my ($ext2lang, $shebang) = _parse_filetypes($dir);
-        bless {
-                -dir => $dir,
-                -ext2lang => $ext2lang,
-                -shebang => $shebang,
-        }, $class;
+        $hl ||= do {
+                my $dir = highlight::DataDir->new;
+                $dir->initSearchDirectories('');
+                my ($ext2lang, $shebang) = _parse_filetypes($dir);
+                bless {
+                        -dir => $dir,
+                        -ext2lang => $ext2lang,
+                        -shebang => $shebang,
+                }, $class;
+        };
 }
 
 sub _shebang2lang ($$) {
@@ -120,16 +125,4 @@ sub do_hl_lang {
         \$out;
 }
 
-# SWIG instances aren't reference-counted, but $self is;
-# so we need to delete all the CodeGenerator instances manually
-# at our own destruction
-sub DESTROY {
-        my ($self) = @_;
-        foreach my $gen (values %$self) {
-                if (ref($gen) eq 'highlight::CodeGenerator') {
-                        highlight::CodeGenerator::deleteInstance($gen);
-                }
-        }
-}
-
 1;
diff --git a/lib/PublicInbox/ViewVCS.pm b/lib/PublicInbox/ViewVCS.pm
index acdd822d..0fb6b64e 100644
--- a/lib/PublicInbox/ViewVCS.pm
+++ b/lib/PublicInbox/ViewVCS.pm
@@ -25,11 +25,6 @@ my $hl = eval {
         PublicInbox::HlMod->new;
 };
 
-# we need to trigger highlight::CodeGenerator::deleteInstance
-# in HlMod::DESTROY before the rest of Perl shuts down to avoid
-# a segfault at shutdown
-END { $hl = undef };
-
 my %QP_MAP = ( A => 'oid_a', B => 'oid_b', a => 'path_a', b => 'path_b' );
 my $max_size = 1024 * 1024; # TODO: configurable
 my $BIN_DETECT = 8000; # same as git
diff --git a/t/hl_mod.t b/t/hl_mod.t
index 238f8ec5..f2eb5f91 100644
--- a/t/hl_mod.t
+++ b/t/hl_mod.t
@@ -40,19 +40,4 @@ my $orig = $str;
         }
 }
 
-my $nr = $ENV{TEST_MEMLEAK};
-if ($nr && -r "/proc/$$/status") {
-        my $fh;
-        open $fh, '<', "/proc/$$/status";
-        diag "starting at memtest at ".join('', grep(/VmRSS:/, <$fh>));
-        PublicInbox::HlMod->new->do_hl(\$orig) for (1..$nr);
-        open $fh, '<', "/proc/$$/status";
-        diag "creating $nr instances: ".join('', grep(/VmRSS:/, <$fh>));
-        my $hls = PublicInbox::HlMod->new;
-        $hls->do_hl(\$orig) for (1..$nr);
-        $hls = undef;
-        open $fh, '<', "/proc/$$/status";
-        diag "reused instance $nr times: ".join('', grep(/VmRSS:/, <$fh>));
-}
-
 done_testing;