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 07/10] checkpatch: use hash lookups for single-word spelling checks
Date: Tue,  1 Dec 2026 14:53:51 -0800	[thread overview]
Message-ID: <ef7fe3e5ff5cc79dd4e419b2e7ce72d7cfe3e50f.1796165270.git.bvanassche@acm.org> (raw)
In-Reply-To: <cover.1796165270.git.bvanassche@acm.org>

Split %spelling_fix entries at startup into %spelling_fix_word for single
tokens matching /^[\w\-'`]+$/ and lazily compiled multi-word regexes
($misspellings_multi_re and $misspellings_multi_has_re) for the remaining
phrases instead of joining all entries into one alternation regex.

Gate the TYPO_SPELLING check on (!$file || $check) and
show_type("TYPO_SPELLING"), skip decode("utf8", $rawline) for pure-ASCII
lines unless a multi-word entry contains a raw 0xDF byte (which only
case-folds to "ss" in UTF-8 flagged strings), and preserve left-to-right
report ordering and consumed trailing-delimiter semantics via
$min_start = $end + 2.

Avoid running a ~1,800 branch alternation regex (or tens of thousands of
branches with --codespell) on every added and commit-log line, and skip
spelling scans under -f when CHECK messages are disabled.

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

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 64c965c84d7d..3eb2ac2c7a9b 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -965,9 +965,15 @@ our $allowed_asm_includes = qr{(?x:
 )};
 # memory.h: ARM has a custom one
 
-# Load common spelling mistakes and build regular expression list.
+# Load common spelling mistakes and build lookup table / multi-word regex.
 my $misspellings;
 my %spelling_fix;
+my %spelling_fix_word;
+my $misspellings_multi_joined;
+my $misspellings_multi_has_df = 0;
+my $misspellings_multi_re;
+my $misspellings_multi_has_re;
+my $misspellings_full_re;
 
 if (open(my $spelling, '<', $spelling_file)) {
 	while (<$spelling>) {
@@ -1012,7 +1018,23 @@ if ($codespell) {
 	}
 }
 
-$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
+if (keys %spelling_fix) {
+	$misspellings = 1;
+	my @multi = ();
+	foreach my $suspect (sort keys %spelling_fix) {
+		if ($suspect =~ /^[\w\-'`]+$/) {
+			$spelling_fix_word{lc($suspect)} = 1;
+		} else {
+			push(@multi, $suspect);
+		}
+	}
+	if (@multi) {
+		$misspellings_multi_joined = join("|", @multi);
+		# A raw (non-UTF-8) 0xDF byte case-folds to "ss" only when
+		# matched against a string with the UTF8 flag set.
+		$misspellings_multi_has_df = ($misspellings_multi_joined =~ /\xdf/);
+	}
+}
 
 sub read_words {
 	my ($wordsRef, $file) = @_;
@@ -3745,12 +3767,71 @@ sub process {
 
 # Check for various typo / spelling mistakes
 		if (defined($misspellings) &&
+		    (!$file || $check) &&
+		    show_type("TYPO_SPELLING") &&
 		    ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
-			my $rawline_utf8 = decode("utf8", $rawline);
-			while ($rawline_utf8 =~ /(?:^|[^\w\-'`])($misspellings)(?:[^\w\-'`]|$)/gi) {
-				my $typo = $1;
+			my $is_non_ascii = ($rawline =~ /[^\x00-\x7f]/);
+			my $rawline_utf8 = ($is_non_ascii || $misspellings_multi_has_df) ?
+					   decode("utf8", $rawline) : $rawline;
+			my @typo_matches = ();
+			my $use_full = ($is_non_ascii &&
+					$rawline_utf8 =~ /[\x{00df}\x{017f}\x{1e9e}\x{fb00}-\x{fb06}]/);
+			if (!$use_full && defined($misspellings_multi_joined) &&
+			    !defined($misspellings_multi_re)) {
+				$misspellings_multi_re = qr{(?:^|[^\w\-'`])($misspellings_multi_joined)(?:[^\w\-'`]|$)}i;
+			}
+			if (!$use_full && defined($misspellings_multi_re) &&
+			    $rawline_utf8 =~ $misspellings_multi_re) {
+				if (!defined($misspellings_multi_has_re)) {
+					$misspellings_multi_has_re = qr{(?=(?:^|[^\w\-'`])($misspellings_multi_joined)(?:[^\w\-'`]|$))}i;
+				}
+				my @cands = ();
+				while ($rawline_utf8 =~ /([\w\-'`]+)/g) {
+					if (exists($spelling_fix_word{lc($1)})) {
+						push(@cands, [$-[1], $+[1], $1]);
+					}
+				}
+				while ($rawline_utf8 =~ /$misspellings_multi_has_re/g) {
+					push(@cands, [$-[1], $+[1], $1]);
+				}
+				@cands = sort { $a->[0] <=> $b->[0] } @cands;
+				for (my $i = 1; $i < @cands; $i++) {
+					if ($cands[$i]->[0] == $cands[$i - 1]->[0]) {
+						$use_full = 1;
+						last;
+					}
+				}
+				if (!$use_full) {
+					my $min_start = 0;
+					foreach my $cand (@cands) {
+						next if ($cand->[0] < $min_start);
+						push(@typo_matches, $cand);
+						$min_start = $cand->[1] + 2;
+					}
+				}
+			} elsif (!$use_full) {
+				my $min_start = 0;
+				while ($rawline_utf8 =~ /([\w\-'`]+)/g) {
+					next if ($-[1] < $min_start);
+					if (exists($spelling_fix_word{lc($1)})) {
+						push(@typo_matches, [$-[1], $+[1], $1]);
+						$min_start = $+[1] + 2;
+					}
+				}
+			}
+			if ($use_full) {
+				if (!defined($misspellings_full_re)) {
+					my $all = join("|", sort keys %spelling_fix);
+					$misspellings_full_re = qr{(?:^|[^\w\-'`])($all)(?:[^\w\-'`]|$)}i;
+				}
+				while ($rawline_utf8 =~ /$misspellings_full_re/g) {
+					push(@typo_matches, [$-[1], $+[1], $1]);
+				}
+			}
+			foreach my $m (@typo_matches) {
+				my ($typo_start, $typo) = ($m->[0], $m->[2]);
 				my $blank = copy_spacing($rawline_utf8);
-				my $ptr = substr($blank, 0, $-[1]) . "^" x length($typo);
+				my $ptr = substr($blank, 0, $typo_start) . "^" x length($typo);
 				my $hereptr = "$hereline$ptr\n";
 				my $typo_fix = $spelling_fix{lc($typo)};
 				$typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);

  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 ` Bart Van Assche [this message]
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=ef7fe3e5ff5cc79dd4e419b2e7ce72d7cfe3e50f.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®