Skip to content

Commit 9ea42d7

Browse files
committed
lcov: Improve JSON module selection
Instead of using a fixed default module when config option lcov_json_module is set to 'auto', try to load the fastest available module from the following list of alternatives: - JSON::XS - Cpanel::JSON::XS - JSON::PP - JSON Also add a man page section describing config option lcov_json_module. Signed-off-by: Peter Oberparleiter <[email protected]>
1 parent ba69439 commit 9ea42d7

File tree

3 files changed

+70
-12
lines changed

3 files changed

+70
-12
lines changed

bin/geninfo

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ use Getopt::Long;
6060
use Digest::MD5 qw(md5_base64);
6161
use Cwd qw/abs_path/;
6262
use IO::Uncompress::Gunzip qw(gunzip $GunzipError);
63+
use Module::Load;
64+
use Module::Load::Conditional qw(check_install);
6365

6466
if( $^O eq "msys" )
6567
{
@@ -223,6 +225,7 @@ sub compat_name($);
223225
sub parse_compat_modes($);
224226
sub is_compat($);
225227
sub is_compat_auto($);
228+
sub load_json_module($);
226229

227230

228231
# Global variables
@@ -494,18 +497,6 @@ if ($rc_intermediate eq "0") {
494497
"'$rc_intermediate'\n");
495498
}
496499

497-
# Determine JSON module
498-
my $current_json_module = "JSON::PP";
499-
if ($rc_json_module ne "auto") {
500-
$current_json_module = $rc_json_module
501-
}
502-
503-
use Module::Load;
504-
eval "load $current_json_module, 'decode_json'";
505-
if ($@) {
506-
die("Module is not installed: ". "'$current_json_module'\n");
507-
};
508-
509500
if ($intermediate) {
510501
info("Using intermediate gcov format\n");
511502
if ($opt_derive_func_data) {
@@ -517,6 +508,9 @@ if ($intermediate) {
517508
die("ERROR: excluding exception branches is not compatible with ".
518509
"text intermediate format\n");
519510
}
511+
if ($gcov_caps->{'json-format'}) {
512+
load_json_module($rc_json_module);
513+
}
520514
}
521515

522516
if ($no_exception_br && ($gcov_version < $GCOV_VERSION_3_3_0)) {
@@ -4722,3 +4716,45 @@ sub is_compat_auto($)
47224716
return 1 if ($compat_value{$mode} == $COMPAT_VALUE_AUTO);
47234717
return 0;
47244718
}
4719+
4720+
#
4721+
# load_json_module(rc)
4722+
#
4723+
# If RC is "auto", load best available JSON module from a list of alternatives,
4724+
# otherwise load the module specified by RC.
4725+
#
4726+
sub load_json_module($)
4727+
{
4728+
my ($rc) = @_;
4729+
# List of alternative JSON modules to try
4730+
my @alternatives = (
4731+
"JSON::XS", # Fast, but not always installed
4732+
"Cpanel::JSON::XS", # Fast, a more recent fork
4733+
"JSON::PP", # Slow, part of core-modules
4734+
"JSON", # Not available in all distributions
4735+
);
4736+
my $mod;
4737+
4738+
# Determine JSON module
4739+
if (lc($rc) eq "auto") {
4740+
for my $m (@alternatives) {
4741+
if (check_install(module => $m)) {
4742+
$mod = $m;
4743+
last;
4744+
}
4745+
}
4746+
4747+
if (!defined($mod)) {
4748+
die("No JSON module found (tried ".
4749+
join(" ", @alternatives).")\n");
4750+
}
4751+
} else {
4752+
$mod = $rc;
4753+
}
4754+
4755+
eval "load '$mod', 'decode_json'";
4756+
if ($@) {
4757+
die("Module is not installed: ". "'$mod'\n");
4758+
}
4759+
info("Using JSON module $mod\n");
4760+
}

lcovrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,3 +187,6 @@ lcov_branch_coverage = 0
187187
# Ask LCOV to return non-zero exit code if line coverage is below threshold
188188
# Default is 0.0 - i.e., do not check threshold.
189189
#lcov_fail_under_lines = 97.5
190+
191+
# Specify JSON module to use, or choose best available if set to auto
192+
lcov_json_module = auto

man/lcovrc.5

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,13 @@ lcov_branch_coverage = 0
296296
lcov_fail_under_lines = 97.5
297297
.br
298298

299+
# Specify JSON module to use, or choose best available if
300+
.br
301+
# set to auto
302+
.br
303+
lcov_json_module = auto
304+
.br
305+
299306
.PP
300307

301308
.SH OPTIONS
@@ -1011,6 +1018,18 @@ This option is equivalent to the \-\-fail\-under\-lines lcov command line option
10111018
The default is 0 (no threshold).
10121019
.PP
10131020

1021+
.BR lcov_json_module " ="
1022+
.IR module | auto
1023+
.IP
1024+
Specify the JSON module to use, or choose best available from a set of
1025+
alternatives if set to 'auto'. Note that some JSON modules are slower than
1026+
others (notably JSON::PP can be very slow compared to JSON::XS).
1027+
.br
1028+
1029+
Default is 'auto'.
1030+
.PP
1031+
1032+
10141033
.SH FILES
10151034

10161035
.TP

0 commit comments

Comments
 (0)