lib/PublicInbox/HlMod.pm | 33 +++++++++++++-------------------- lib/PublicInbox/ViewVCS.pm | 5 ----- t/hl_mod.t | 15 --------------- diff --git a/lib/PublicInbox/HlMod.pm b/lib/PublicInbox/HlMod.pm index 284e4b1896d46db23fe73613849604448f9c49ec..13f27d19cb40b85e8d5bbc33a22525c31576a551 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 @@ } (\%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 ($$) { @@ -118,18 +123,6 @@ # know that, so ensure it's marked as UTF-8 even if it isnt... my $out = $gen->generateString($$str); utf8::decode($out); \$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 acdd822dc4274099e2b362ecd0512aa43759e69a..0fb6b64e49e8b8d2e9068981c864537a63334830 100644 --- a/lib/PublicInbox/ViewVCS.pm +++ b/lib/PublicInbox/ViewVCS.pm @@ -25,11 +25,6 @@ require PublicInbox::HlMod; 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 238f8ec55b5c23a18ae7a82e9c008fe22ec7f06a..f2eb5f918765729b2d496402fbf0ef2ed1e3218a 100644 --- a/t/hl_mod.t +++ b/t/hl_mod.t @@ -40,19 +40,4 @@ is($out, $orig, 'w3m output matches'); } } -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;