[PATCH 10/10] checkpatch: add fast paths to per-line character-scanning helpers
From: Bart Van Assche
Date: Tue Dec 01 2026 - 17:54:29 EST
Speed up three per-line character-scanning helpers:
- sanitise_line(): return $line immediately when length($line) <= 1 or
when $sanitise_quote is empty and substr($line, 1) contains none of
'/', "'", '"', or '#'.
- expand_tabs(): return $str immediately when index($str, "\t") < 0 and
copy contiguous non-tab slices using index() and substr() instead of
splitting into a per-character array.
- get_raw_comment(): return '' immediately when index($line, "$;") < 0 and
copy contiguous $;+ spans instead of iterating per character.
Avoid per-character Perl loops on the common lines that lack quotes,
comments, preprocessor directives, or tabs.
Signed-off-by: Bart Van Assche <bvanassche@xxxxxxx>
---
scripts/checkpatch.pl | 34 +++++++++++++++++-----------------
1 file changed, 17 insertions(+), 17 deletions(-)
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index b3854bc3ad6d..31723f178629 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -1789,20 +1789,17 @@ sub which_conf {
sub expand_tabs {
my ($str) = @_;
+ return $str if (index($str, "\t") < 0);
+
my $res = '';
- my $n = 0;
- for my $c (split(//, $str)) {
- if ($c eq "\t") {
- $res .= ' ';
- $n++;
- for (; ($n % $tabsize) != 0; $n++) {
- $res .= ' ';
- }
- next;
- }
- $res .= $c;
- $n++;
+ my $pos = 0;
+ my $idx;
+ while (($idx = index($str, "\t", $pos)) >= 0) {
+ $res .= substr($str, $pos, $idx - $pos);
+ $res .= ' ' x ($tabsize - (length($res) % $tabsize));
+ $pos = $idx + 1;
}
+ $res .= substr($str, $pos);
return $res;
}
@@ -1838,6 +1835,9 @@ sub sanitise_line_reset {
sub sanitise_line {
my ($line) = @_;
+ return $line if (length($line) <= 1 ||
+ ($sanitise_quote eq '' && substr($line, 1) !~ m{[/'"#]}));
+
my $res = '';
my $l = '';
@@ -2928,12 +2928,12 @@ sub pos_last_openparen {
sub get_raw_comment {
my ($line, $rawline) = @_;
- my $comment = '';
- for my $i (0 .. (length($line) - 1)) {
- if (substr($line, $i, 1) eq "$;") {
- $comment .= substr($rawline, $i, 1);
- }
+ return '' if (index($line, "$;") < 0);
+
+ my $comment = '';
+ while ($line =~ /$;+/g) {
+ $comment .= substr($rawline, $-[0], $+[0] - $-[0]);
}
return $comment;