Skip to content

Commit 515a235

Browse files
JoePerchestorvalds
authored andcommitted
checkpatch: improve octal permissions test speed
The current octal permissions test is very slow. When patch ("checkpatch: add checks for constant non-octal permissions") was added, processing time approximately tripled. Regain almost all of the performance by not looping through all the possible functions unless the line contains one of the functions. Signed-off-by: Joe Perches <joe@perches.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent daa8b05 commit 515a235

1 file changed

Lines changed: 31 additions & 20 deletions

File tree

‎scripts/checkpatch.pl‎

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,13 @@ sub hash_show_words {
388388
["(?:CLASS|DEVICE|SENSOR)_ATTR", 2],
389389
);
390390

391+
#Create a search pattern for all these functions to speed up a loop below
392+
our $mode_perms_search = "";
393+
foreach my $entry (@mode_permission_funcs) {
394+
$mode_perms_search .= '|' if ($mode_perms_search ne "");
395+
$mode_perms_search .= $entry->[0];
396+
}
397+
391398
our $allowed_asm_includes = qr{(?x:
392399
irq|
393400
memory
@@ -4524,26 +4531,30 @@ sub process {
45244531
"Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
45254532
}
45264533

4527-
foreach my $entry (@mode_permission_funcs) {
4528-
my $func = $entry->[0];
4529-
my $arg_pos = $entry->[1];
4530-
4531-
my $skip_args = "";
4532-
if ($arg_pos > 1) {
4533-
$arg_pos--;
4534-
$skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
4535-
}
4536-
my $test = "\\b$func\\s*\\(${skip_args}([\\d]+)\\s*[,\\)]";
4537-
if ($^V && $^V ge 5.10.0 &&
4538-
$line =~ /$test/) {
4539-
my $val = $1;
4540-
$val = $6 if ($skip_args ne "");
4541-
4542-
if ($val !~ /^0$/ &&
4543-
(($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
4544-
length($val) ne 4)) {
4545-
ERROR("NON_OCTAL_PERMISSIONS",
4546-
"Use 4 digit octal (0777) not decimal permissions\n" . $herecurr);
4534+
# Mode permission misuses where it seems decimal should be octal
4535+
# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
4536+
if ($^V && $^V ge 5.10.0 &&
4537+
$line =~ /$mode_perms_search/) {
4538+
foreach my $entry (@mode_permission_funcs) {
4539+
my $func = $entry->[0];
4540+
my $arg_pos = $entry->[1];
4541+
4542+
my $skip_args = "";
4543+
if ($arg_pos > 1) {
4544+
$arg_pos--;
4545+
$skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
4546+
}
4547+
my $test = "\\b$func\\s*\\(${skip_args}([\\d]+)\\s*[,\\)]";
4548+
if ($line =~ /$test/) {
4549+
my $val = $1;
4550+
$val = $6 if ($skip_args ne "");
4551+
4552+
if ($val !~ /^0$/ &&
4553+
(($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
4554+
length($val) ne 4)) {
4555+
ERROR("NON_OCTAL_PERMISSIONS",
4556+
"Use 4 digit octal (0777) not decimal permissions\n" . $herecurr);
4557+
}
45474558
}
45484559
}
45494560
}

0 commit comments

Comments
 (0)