mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Bart Van Assche <bvanassche@acm.org>
To: Joe Perches <joe@perches.com>, Andy Whitcroft <apw@canonical.com>
Cc: Andrew Morton <akpm@linux-foundation.org>,
	linux-kernel@vger.kernel.org,
	Bart Van Assche <bvanassche@acm.org>
Subject: [PATCH 10/10] checkpatch: add fast paths to per-line character-scanning helpers
Date: Tue,  1 Dec 2026 14:53:54 -0800	[thread overview]
Message-ID: <eac8396b0da614aa436ed3152968d90719747125.1796165270.git.bvanassche@acm.org> (raw)
In-Reply-To: <cover.1796165270.git.bvanassche@acm.org>

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@acm.org>
---
 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;

  parent reply	other threads:[~2026-12-01 22:54 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01 22:53 [PATCH 00/10] Make checkpatch faster Bart Van Assche
2026-12-01 22:53 ` [PATCH 01/10] checkpatch: pre-filter obsolete MAINTAINERS checks and gate on OBSOLETE Bart Van Assche
2026-12-01 22:53 ` [PATCH 02/10] checkpatch: cache SPDX license checks and gate on SPDX_LICENSE_TAG Bart Van Assche
2026-12-01 22:53 ` [PATCH 03/10] checkpatch: cache git and PATH lookups and skip git_is_single_file() Bart Van Assche
2026-12-01 22:53 ` [PATCH 04/10] checkpatch: skip redundant build_types() rebuilds Bart Van Assche
2026-12-01 22:53 ` [PATCH 05/10] checkpatch: avoid dynamic regex recompilation in hot checks Bart Van Assche
2026-12-01 22:53 ` [PATCH 06/10] checkpatch: guard expensive per-line regexes with cheap pre-checks Bart Van Assche
2026-12-01 22:53 ` [PATCH 07/10] checkpatch: use hash lookups for single-word spelling checks Bart Van Assche
2026-12-01 22:53 ` [PATCH 08/10] checkpatch: scan ctx_statement_block() in place without slicing Bart Van Assche
2026-12-01 22:53 ` [PATCH 09/10] checkpatch: scan annotate_values() in place with leading-char dispatch Bart Van Assche
2026-12-01 22:53 ` Bart Van Assche [this message]
2026-12-01 23:26 ` [PATCH 00/10] Make checkpatch faster Joe Perches
2026-12-01 23:53   ` Bart Van Assche

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=eac8396b0da614aa436ed3152968d90719747125.1796165270.git.bvanassche@acm.org \
    --to=bvanassche@acm.org \
    --cc=akpm@linux-foundation.org \
    --cc=apw@canonical.com \
    --cc=joe@perches.com \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®