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 03/10] checkpatch: cache git and PATH lookups and skip git_is_single_file()
Date: Tue,  1 Dec 2026 14:53:47 -0800	[thread overview]
Message-ID: <7c299dab9cc80bb59f34b69b686accc0073bfc5c.1796165270.git.bvanassche@acm.org> (raw)
In-Reply-To: <cover.1796165270.git.bvanassche@acm.org>

Memoize git_commit_info() outcomes in %git_commit_info_cache while
preserving ok, bad, and passthrough return semantics, skip calling
git_is_single_file() when $git or $file is set or when reading from stdin
('-'), and memoize PATH lookups in which() via %which_cache.

Eliminate redundant git log -1 subprocesses for repeated commit hashes
(including Fixes: and commit references checked by two separate rules on
the same line), stop running git ls-files on commit hashes under -g or
when -f is already specified, and avoid repeating stat() scans across PATH.

Skipping git_is_single_file() is not strictly behavior-preserving. That
function runs an unquoted "git ls-files -- $filename" and matches its
output against an unescaped regular expression. Hence, with this change,
"checkpatch.pl -f 'a[b.c'" for a git-tracked file named 'a[b.c' no longer
dies with "Unmatched [ in regex", and shell syntax errors are no longer
reported for -f arguments that contain shell metacharacters. Behavior
without -f, -g and '-' is unchanged.

As a side effect of the caching, Perl warnings triggered inside which()
(e.g. with PATH unset) or git_commit_info() are reported once per unique
argument instead of once per call.

Signed-off-by: Bart Van Assche <bvanassche@acm.org>
---
 scripts/checkpatch.pl | 30 ++++++++++++++++++++++++++----
 1 file changed, 26 insertions(+), 4 deletions(-)

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 3602e79fc091..4ca3ce0f4502 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -1408,16 +1408,31 @@ sub git_is_single_file {
 	return $count eq 1 && $output =~ m{^${filename}$};
 }
 
+my %git_commit_info_cache = ();
+
 sub git_commit_info {
 	my ($commit, $id, $desc) = @_;
 
 	return ($id, $desc) if ((which("git") eq "") || !(-e "$gitroot"));
 
+	if (defined($commit) && exists($git_commit_info_cache{$commit})) {
+		my $cached = $git_commit_info_cache{$commit};
+		if ($cached->[0] eq 'ok') {
+			return ($cached->[1], $cached->[2]);
+		} elsif ($cached->[0] eq 'bad') {
+			return (undef, $desc);
+		}
+		return ($id, $desc);
+	}
+
 	my $output = `${git_command} log --no-color --format='%H %s' -1 $commit 2>&1`;
 	$output =~ s/^\s*//gm;
 	my @lines = split("\n", $output);
 
-	return ($id, $desc) if ($#lines < 0);
+	if ($#lines < 0) {
+		$git_commit_info_cache{$commit} = ['passthrough'] if defined($commit);
+		return ($id, $desc);
+	}
 
 	if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous/) {
 # Maybe one day convert this block of bash into something that returns
@@ -1429,12 +1444,15 @@ sub git_commit_info {
 #		    git log --format='%H %s' -1 $line |
 #		    echo "commit $(cut -c 1-12,41-)"
 #		done
+		$git_commit_info_cache{$commit} = ['passthrough'] if defined($commit);
 	} elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./ ||
 		 $lines[0] =~ /^fatal: bad object $commit/) {
 		$id = undef;
+		$git_commit_info_cache{$commit} = ['bad'] if defined($commit);
 	} else {
 		$id = substr($lines[0], 0, 12);
 		$desc = substr($lines[0], 41);
+		$git_commit_info_cache{$commit} = ['ok', $id, $desc] if defined($commit);
 	}
 
 	return ($id, $desc);
@@ -1483,7 +1501,7 @@ my $vname;
 $allow_c99_comments = !defined $ignore_type{"C99_COMMENT_TOLERANCE"};
 for my $filename (@ARGV) {
 	my $FILE;
-	my $is_git_file = git_is_single_file($filename);
+	my $is_git_file = (!$git && !$file && $filename ne '-') ? git_is_single_file($filename) : 0;
 	my $oldfile = $file;
 	$file = 1 if ($is_git_file);
 	if ($git) {
@@ -1678,16 +1696,20 @@ sub same_email_addresses {
 	       $comment1 eq $comment2;
 }
 
+my %which_cache;
+
 sub which {
 	my ($bin) = @_;
 
+	return $which_cache{$bin} if exists($which_cache{$bin});
+
 	foreach my $path (split(/:/, $ENV{PATH})) {
 		if (-e "$path/$bin") {
-			return "$path/$bin";
+			return $which_cache{$bin} = "$path/$bin";
 		}
 	}
 
-	return "";
+	return $which_cache{$bin} = "";
 }
 
 sub which_conf {

  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 ` Bart Van Assche [this message]
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 ` [PATCH 10/10] checkpatch: add fast paths to per-line character-scanning helpers Bart Van Assche
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=7c299dab9cc80bb59f34b69b686accc0073bfc5c.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®