test-grep-lint is uncomfortably slow
Does anyone else feel that make test takes too long to start? There is a noticeable pause after "rm -f -r 'test-results'" before the first line of real output is shown. The time in between is likely spent on make test-lint.
Here is a time to run "make test-lint" 5 times in a row.
$ time sh -c 'for c in 1 2 3 4 5; do make test-lint; done' real 0m49.221s user 1m37.796s sys 0m4.597s
If we revert the merge c9a92e239f (Merge branch 'mm/test-grep-lint', 2026-07-19) to disable the grep-lint, it becomes somewhat tolerable.
$ time sh -c 'for c in 1 2 3 4 5; do make test-lint; done' real 0m9.166s user 0m59.156s sys 0m2.887s
Is this something we can make go faster?
Thanks.
Quoted 4 lines · Junio C Hamano
Does anyone else feel that make test takes too long to start? There is a noticeable pause after "rm -f -r 'test-results'" before the first line of real output is shown. The time in between is likely spent on make test-lint.
Yeah, "make test-greplint" takes ~6.5s to run for me. The whole test suite runs in ~111s, so that is over 5% of the time. ;) And of course it is more noticeable because there it is all at once before any output happens.
Chainlint actually takes similar amounts of CPU time (which is not surprising, because they share the parser) but it runs in parallel. We could probably do the same here.
It does feel like a lot of processing just to detect one specific type of lint error. If we amortized the parsing effort across other lint checks (or even chainlint itself!) that might be more palatable.
This patch does reduce the cost by ~50%:
t/greplint.pl +9 -0
@@ -190,6 +190,15 @@ sub check_test { } } return unless $body; + # We can never match a bare "grep" if there isn't one in the first + # place. So we can skip all of the parsing effort. Technically you + # can have: + # + # gr\ + # ep + # + # but that is sufficiently insane that I think we can ignore it. + return unless $body =~ /grep/; my $raw_lines = $self->{raw_lines};
but of course it is working against the "maybe we could amortize the parsing cost" idea.
-Peff
cover t: run greplint checks inside chainlint
In reply to Junio C HamanoAs Junio said[0], make test-lint is "uncomfortably slow".
make test-lint runs both t/chainlint.pl and t/greplint.pl which do mostly the same thing on mostly the same files, both:
- Open about 1100 files
- Parse/lex/tokenize the files
- Run checks - which is the only place chainlint and greplint vary
Plus, greplint processes files serially. So we're doing the same work twice and half the work happens in serial.
Following Peff's suggestion, this series teaches chainlint.pl to run the greplint checks while running checks on its own tokens. So "make test-lint" parses each file once, in parallel.
Keep greplint.pl as a standalone for GIT_TEST_CHAIN_LINT=0.
Peff's early return (patch 2) speeds up standalone greplint.pl (when greplint does its own parsing).
Peff: Can I add your Signed-off-by to your patch in the series? It does, indeed, cut time in half when greplint does its own parsing.
Before this series:
$ time sh -c 'for c in 1 2 3 4 5; do make test-lint; done'
real 0m25.158s
user 0m51.557s
sys 0m0.899sAfter this series:
$ time sh -c 'for c in 1 2 3 4 5; do make test-lint; done'
real 0m5.972s
user 0m41.536s
sys 0m0.943s(nproc == 16 for both)
[0]. https://lore.kernel.org/git/[email protected]/
Jeff King (1):
t/greplint: skip test bodies without the word grep
Tyler Cipriani (3):
t/interop: replace bare grep with test_grep t/greplint: move parsing and token check to library t/chainlint: add --greplint to run greplint
t/Makefile | 30 +++-
t/chainlint.pl | 24 ++-
t/greplint.pl | 240 +------------------------
t/interop/i5700-protocol-transition.sh | 10 +-
t/{greplint.pl => lib-greplint.pl} | 78 ++++----
5 files changed, 93 insertions(+), 289 deletions(-)
copy t/{greplint.pl => lib-greplint.pl} (85%)base-commit: 7e34d8174dcda3435d1404c9a30bef46afe6c13e
Signature · 1 line
2.47.3
PATCH 1/4 t/interop: replace bare grep with test_grep
Use test_grep for grep assertions in interop files.
In preparation for combining chainlint and greplint into a single test command, ensure that chainlint and greplint pass on their combined list of shell scripts under test.
--- t/interop/i5700-protocol-transition.sh | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
t/interop/i5700-protocol-transition.sh +5 -5
@@ -24,7 +24,7 @@ test_expect_success "git:// clone with $VERSION_A and protocol v1" ' git.a -C child log -1 --format=%s >actual && git.b -C "$repo" log -1 --format=%s >expect && test_cmp expect actual && - grep "version=1" log + test_grep "version=1" log ' test_expect_success "git:// fetch with $VERSION_A and protocol v1" '
@@ -35,8 +35,8 @@ test_expect_success "git:// fetch with $VERSION_A and protocol v1" ' git.a -C child log -1 --format=%s FETCH_HEAD >actual && test_cmp expect actual && - grep "version=1" log && - ! grep "version 1" log + test_grep "version=1" log && + test_grep ! "version 1" log ' stop_git_daemon
@@ -51,7 +51,7 @@ test_expect_success "file:// clone with $VERSION_A and protocol v1" ' git.a -C child2 log -1 --format=%s >actual && git.b -C parent log -1 --format=%s >expect && test_cmp expect actual && - ! grep "version 1" log + test_grep ! "version 1" log ' test_expect_success "file:// fetch with $VERSION_A and protocol v1" '
@@ -62,7 +62,7 @@ test_expect_success "file:// fetch with $VERSION_A and protocol v1" ' git.a -C child2 log -1 --format=%s FETCH_HEAD >actual && test_cmp expect actual && - ! grep "version 1" log + test_grep ! "version 1" log ' test_done
Signature · 1 line
2.47.3
PATCH 2/4 t/greplint: skip test bodies without the word grep
In reply to Tyler CiprianiFrom: Jeff King <[email protected]>
greplint.pl spends time parsing test bodies that cannot contain a bare grep. Check for the string "grep" first and skip the parse when it isn't there. This reduces the cost of "make test-greplint" by ~50%.
--- t/greplint.pl | 9 +++++++++ 1 file changed, 9 insertions(+)
t/greplint.pl +9 -0
@@ -190,6 +190,15 @@ sub check_test { } } return unless $body; + # We can never match a bare "grep" if there isn't one in the first + # place. So we can skip all of the parsing effort. Technically you + # can have: + # + # gr\ + # ep + # + # but that is sufficiently insane that I think we can ignore it. + return unless $body =~ /grep/; my $raw_lines = $self->{raw_lines};
Signature · 1 line
2.47.3
PATCH 3/4 t/greplint: move parsing and token check to library
In reply to Tyler CiprianiMove greplint.pl's parsing and token check work to a library. Modify the greplint.pl to use the library. Break out actual checks into check_tokens() sub.
In preparation to combine the work of chainlint and greplint into a single parser invocation.
---
t/greplint.pl | 249 +----------------------------
t/{greplint.pl => lib-greplint.pl} | 69 ++++----
2 files changed, 37 insertions(+), 281 deletions(-)
copy t/{greplint.pl => lib-greplint.pl} (87%)t/greplint.pl +5 -244
@@ -1,257 +1,16 @@ #!/usr/bin/env perl -# Detect bare 'grep' used as a test assertion where 'test_grep' -# should be used, and '! test_grep' where 'test_grep !' should -# be used. -# -# The shared shell parser tokenizes test bodies so that 'grep' -# inside heredocs, command substitutions like $(grep ...), and -# quoted strings is collapsed into a single token and never seen -# by our check. A line-oriented approach would need to track -# heredoc delimiters, nested $() depth, and cross-line pipe -# state to avoid false positives on patterns like: -# -# write_script foo.sh <<-\EOF -# grep pattern file # data, not an assertion -# EOF -# -# The Lexer already handles these. - +# Detect bare 'grep' used as a test assertion use warnings; use strict; use File::Basename; do(dirname($0) . "/lib-shell-parser.pl") or die "$0: failed to load lib-shell-parser.pl: $@$!\n"; +do(dirname($0) . "/lib-greplint.pl") + or die "$0: failed to load lib-greplint.pl: $@$!\n"; my $exit_code = 0; -# GrepLintParser inherits ScriptParser's ability to find -# test_expect_success/failure blocks and call check_test() -# on each body. We override check_test() to walk the token -# stream looking for bare grep assertions. -package GrepLintParser; - -our @ISA = ('ScriptParser'); - -# After these tokens, the next token is a command word. -# For example, in 'echo foo && grep bar file', the 'grep' -# after '&&' is at command position and should be flagged. -my %cmd_start = map { $_ => 1 } qw(&& || ; ;; do then else elif), "\n", '{', '('; - -# Tokens indicating grep's output is piped or redirected. -my %filter_op = map { $_ => 1 } qw(| > >> <); - -# A token is at "command word" position if the shell would -# interpret it as a program name rather than an argument. -# Only 'grep' at command position is an assertion we should -# flag; 'grep' as an argument ('test_must_fail grep') or -# value ('for cmd in grep sed') is not. -sub is_command_word { - my ($tokens, $pos) = @_; - return 1 if $pos == 0; - for (my $j = $pos - 1; $j >= 0; $j--) { - my $t = $tokens->[$j]->[0]; - # After a separator or pipe, a new command starts. - return 1 if $cmd_start{$t} || $t eq '|'; - # After '}' or ')', what follows is a separator or - # redirect on the compound command, not a new command. - return 0 if $t eq '}' || $t eq ')'; - # '!' is a prefix that does not consume command - # position; keep scanning to find what precedes it. - next if $t eq '!'; - # Any other word means we are past the command word. - return 0; - } - return 1; -} - -# lint_ok() reports whether a bare grep carries a trailing -# '# lint-ok' comment telling this linter to skip it. -# -# In practice this is needed for just one case: a grep acting -# as a data filter whose output is consumed by a redirect or -# pipe on an enclosing compound command (such as a subshell or -# brace group) rather than by grep's own pipeline, e.g. -# -# ( grep ... && # lint-ok -# sed ... ) >out -# -# { grep ... || : # lint-ok -# } >out -# -# is_filter() only scans grep's own pipeline: it stops at the -# separator before the compound command closes and never sees -# the outer redirect, so it would flag such a grep as an -# assertion. A grep that really is an assertion is better -# written as test_grep (or a guarded test_grep when the file's -# presence is conditional) than annotated with lint-ok. -sub lint_ok { - my ($raw_lines, $ln) = @_; - if ($ln < 1 || $ln > @$raw_lines) { - warn "lint_ok: line number $ln out of range (1.." . - scalar(@$raw_lines) . ")\n"; - return 0; - } - return $raw_lines->[$ln - 1] =~ /lint-ok/; -} - -# Grep is a filter (not an assertion) if it receives piped -# input or sends its output to a pipe or redirect. Check -# both directions from grep's position in the token stream. -sub is_filter { - my ($tokens, $pos) = @_; - # Backward: is grep receiving piped input? - # Newlines don't break pipes ('cmd |\n grep' is one - # pipeline), so skip past them. - for (my $j = $pos - 1; $j >= 0; $j--) { - my $t = $tokens->[$j]->[0]; - return 1 if $t eq '|'; - next if $t eq "\n"; - last if $cmd_start{$t} || $t eq '}' || $t eq ')'; - } - # Forward: is grep piping or redirecting output? - # Unlike the backward scan, we do not skip newlines here: - # a bare newline is a command boundary, and redirects or - # pipes must appear on the same line as grep (or after a - # line continuation, which the Lexer consumes). - for (my $j = $pos + 1; $j < @$tokens; $j++) { - my $t = $tokens->[$j]->[0]; - return 0 if $cmd_start{$t}; - return 1 if $filter_op{$t}; - } - return 0; -} - -# Map a body-relative line number to a file line number. -# For double-quoted bodies, backslash-continuation lines -# (\<newline>) are consumed by the Lexer without appearing -# in the body text, so the inner parser sees fewer lines -# than the source file has. We walk the source lines to -# count continuations and adjust accordingly. -sub body_to_file_line { - my ($body_lineno, $body_token, $raw_lines, $body_start) = @_; - my $body_text = $body_token->[0]; - my $body_end_line = $body_token->[4]; - unless ($body_start && $body_start >= 1) { - warn "body_start is not a positive integer\n"; - return $body_lineno; - } - my $file_lineno = $body_lineno + $body_start - 1; - # Only double-quoted bodies have line splices. - return $file_lineno unless $body_text =~ /^"/; - my $adj = 0; - my $lines_seen = 0; - unless ($body_end_line && $body_end_line >= $body_start) { - warn "body_end_line is not set for double-quoted body\n"; - return $file_lineno; - } - my $end = $body_end_line; - if ($end > @$raw_lines) { - warn "body_end_line ($end) exceeds file length (" . - scalar(@$raw_lines) . ")\n"; - return $file_lineno; - } - my $src_ln = $body_start; - while ($src_ln <= $end && $lines_seen < $body_lineno) { - my $line = $raw_lines->[$src_ln - 1]; - # Odd trailing backslashes = continuation (\<nl>). - # Even = escaped backslashes (\\), not a continuation. - if ($line =~ /(\\*)$/ && length($1) % 2 == 1) { - $adj++; - } else { - $lines_seen++; - } - $src_ln++; - } - if ($lines_seen < $body_lineno) { - warn "body_lineno ($body_lineno) not found within body range " . - "($body_start..$end)\n"; - } - return $file_lineno + $adj; -} - -# ScriptParser calls this for each test body found in the script. -sub check_test { - my $self = shift @_; - my $title = ScriptParser::unwrap(shift @_); - my $body_token = shift @_; - my $body_start = $body_token->[3]; - my $body = ScriptParser::unwrap($body_token); - # Handle heredoc-style test bodies: - # test_expect_success 'title' - <<\EOF - # grep pattern file - # EOF - # The '-' signals that the body follows as a heredoc. - if ($body eq '-') { - my $herebody = shift @_; - if ($herebody) { - $body = $herebody->{content}; - $body_start = $herebody->{start_line}; - } - } - return unless $body; - # We can never match a bare "grep" if there isn't one in the first - # place. So we can skip all of the parsing effort. Technically you - # can have: - # - # gr\ - # ep - # - # but that is sufficiently insane that I think we can ignore it. - return unless $body =~ /grep/; - - my $raw_lines = $self->{raw_lines}; - - # The outer parser gives us the body as an opaque string. - # Parse it to get individual tokens with command boundaries. - my $parser = ShellParser->new(\$body); - my @tokens = $parser->parse(); - - my $file = $self->{file}; - - for (my $i = 0; $i < @tokens; $i++) { - my $text = $tokens[$i]->[0]; - next unless is_command_word(\@tokens, $i); - - my $token_lineno = $tokens[$i]->[3]; - unless (defined($token_lineno) && $token_lineno >= 1) { - warn "token has no line number\n"; - next; - } - my $file_lineno = body_to_file_line( - $token_lineno, - $body_token, $raw_lines, $body_start); - - # '!' negates the exit code without consuming command - # position. '! test_grep' is an anti-pattern because - # test_grep only prints diagnostics on grep failure, - # and '!' inverts after that decision is already made. - if ($text eq '!') { - if ($i + 1 < @tokens && - $tokens[$i + 1]->[0] eq 'test_grep' && - !lint_ok($raw_lines, $file_lineno)) { - print "$file:$file_lineno: error: ", - 'use "test_grep !" instead of ', - '"! test_grep"', "\n"; - $exit_code = 1; - } - next; - } - - # Bare grep as a command (not a filter) is a test - # assertion that should use test_grep for better - # failure diagnostics. - if ($text eq 'grep' && - !is_filter(\@tokens, $i) && - !lint_ok($raw_lines, $file_lineno)) { - print "$file:$file_lineno: error: ", - "bare grep outside pipeline ", - "(use test_grep)\n"; - $exit_code = 1; - } - } -} - package main; for my $file (@ARGV) {
@@ -263,5 +22,7 @@ package main; $parser->{file} = $file; $parser->{raw_lines} = \@raw_lines; $parser->parse(); + print "$_\n" for @{$parser->{output}}; + $exit_code ||= $parser->{nerrs} > 0; } exit $exit_code;
t/lib-greplint.pl renamed from t/greplint.pl +32 -37
105 lines not on this page · raw message
Signature · 1 line
2.47.3
PATCH 4/4 t/chainlint: add --greplint to run greplint
In reply to Tyler CiprianiSave duplicate file handling and parsing work (and gain parallel file processing) by teaching chainlint to do the work of greplint.
Add --greplint option to chainlint.pl so that it passes tokens to GrepLintParser::check_tokens(). This change allows chainlint.pl to do greplint checks after chainlint has done all the hard work of file handling and parsing.
Update Makefile with new target that invokes chainlint.pl --greplint. In the common case, make test-lint will use the new chainlint target. Continue to respect and set GIT_TEST_CHAIN_LINT=0.
Ensure test-chainlint/test-greplint run on the same set of test files, which expands the set for both.
Local laptop testing shows make test-lint went from around 4.5s before this change to less than 1s after (with -j$(nproc) where nproc == 16).
--- t/Makefile | 30 ++++++++++++++++++++++-------- t/chainlint.pl | 24 ++++++++++++++++++++---- 2 files changed, 42 insertions(+), 12 deletions(-)
t/Makefile +22 -8
@@ -56,9 +56,9 @@ UNIT_TEST_PROGRAMS += unit-tests/bin/unit-tests$(X) UNIT_TESTS = $(sort $(UNIT_TEST_PROGRAMS)) UNIT_TESTS_NO_DIR = $(notdir $(UNIT_TESTS)) -# `test-chainlint` (which is a dependency of `test-lint`, `test` and `prove`) -# checks all tests in all scripts via a single invocation, so tell individual -# scripts not to run the external "chainlint.pl" script themselves +# `test-chainlint-greplint` (which is a dependency of `test-lint`, `test`, and +# `prove`) checks all tests in all scripts via a single invocation, so tell +# individual scripts not to run the external "chainlint.pl" script themselves CHAINLINTSUPPRESS = GIT_TEST_EXT_CHAIN_LINT=0 && export GIT_TEST_EXT_CHAIN_LINT && all:: $(DEFAULT_TEST_TARGET)
@@ -127,6 +127,7 @@ check-chainlint: clean-greplint: $(RM) -r '$(GREPLINTTMP_SQ)' +# chainlint --jobs=1 needed: parallel workers emit output in arbitrary order check-greplint: @mkdir -p '$(GREPLINTTMP_SQ)' && \ '$(PERL_PATH_SQ)' greplint-cat.pl '$(GREPLINTTMP_SQ)' $(GREPLINTTESTS) && \
@@ -134,6 +135,11 @@ check-greplint: $(patsubst %,greplint/%.test,$(GREPLINTTESTS)) \ >'$(GREPLINTTMP_SQ)'/actual 2>&1 || true; } && \ diff -u '$(GREPLINTTMP_SQ)'/expect '$(GREPLINTTMP_SQ)'/actual + @$(CHAINLINT) --greplint --jobs=1 \ + $(patsubst %,greplint/%.test,$(GREPLINTTESTS)) 2>&1 | \ + sed -e '/^# chainlint: /d' -e '/^[0-9][0-9]* /d' \ + >'$(GREPLINTTMP_SQ)'/actual-chainlint && \ + diff -u '$(GREPLINTTMP_SQ)'/expect '$(GREPLINTTMP_SQ)'/actual-chainlint check-meson: @# awk acts up when trying to match single quotes, so we use \047 instead.
@@ -154,12 +160,16 @@ check-meson: test-lint: test-lint-duplicates test-lint-executable \ test-lint-filenames ifneq ($(PERL_PATH),) -test-lint: test-lint-shell-syntax test-greplint +test-lint: test-lint-shell-syntax else GIT_TEST_CHAIN_LINT = 0 endif ifneq ($(GIT_TEST_CHAIN_LINT),0) -test-lint: test-chainlint +test-lint: test-chainlint-greplint +else +ifneq ($(PERL_PATH),) +test-lint: test-greplint +endif endif test-lint-duplicates:
@@ -176,7 +186,7 @@ test-lint-shell-syntax: @'$(PERL_PATH_SQ)' check-non-portable-shell.pl $(T) $(THELPERS) $(TPERF) test-greplint: - @'$(PERL_PATH_SQ)' greplint.pl $(T) $(THELPERS) $(TPERF) + @'$(PERL_PATH_SQ)' greplint.pl $(T) $(THELPERS) $(TPERF) $(TINTEROP) test-lint-filenames: @# We do *not* pass a glob to ls-files but use grep instead, to catch
@@ -186,8 +196,11 @@ test-lint-filenames: test -z "$$bad" || { \ echo >&2 "non-portable file name(s): $$bad"; exit 1; } +test-chainlint-greplint: + @$(CHAINLINT) --greplint $(T) $(THELPERS) $(TPERF) $(TINTEROP) + test-chainlint: - @$(CHAINLINT) $(T) $(TLIBS) $(TPERF) $(TINTEROP) + @$(CHAINLINT) $(T) $(THELPERS) $(TPERF) $(TINTEROP) aggregate-results-and-cleanup: $(T) $(MAKE) aggregate-results
@@ -204,7 +217,8 @@ perf: .PHONY: pre-clean $(T) aggregate-results clean valgrind perf \ check-chainlint clean-chainlint test-chainlint \ - check-greplint clean-greplint test-greplint $(UNIT_TESTS) + check-greplint clean-greplint test-greplint \ + test-chainlint-greplint $(UNIT_TESTS) .PHONY: libgit-sys-test libgit-rs-test libgit-sys-test:
t/chainlint.pl +20 -4
@@ -22,10 +22,13 @@ my $jobs = -1; my $show_stats; my $emit_all; +my $greplint; use File::Basename; do(dirname($0) . "/lib-shell-parser.pl") or die "$0: failed to load lib-shell-parser.pl: $@$!\n"; +do(dirname($0) . "/lib-greplint.pl") + or die "$0: failed to load lib-greplint.pl: $@$!\n"; # TestParser is a subclass of ShellParser which, beyond parsing shell script # code, is also imbued with semantic knowledge of test construction, and checks
@@ -147,9 +150,9 @@ sub format_problem { sub check_test { my $self = shift @_; my $title = ScriptParser::unwrap(shift @_); - my $body = shift @_; - my $lineno = $body->[3]; - $body = ScriptParser::unwrap($body); + my $body_token = shift @_; + my $lineno = $body_token->[3]; + my $body = ScriptParser::unwrap($body_token); if ($body eq '-') { my $herebody = shift @_; $body = $herebody->{content};
@@ -160,6 +163,12 @@ sub check_test { my @tokens = $parser->parse(); my $problems = $parser->{problems}; $self->{nerrs} += @$problems; + if ($greplint) { + my @grep_errors = GrepLintParser::check_tokens($self->{file}, + $self->{raw_lines}, $body_token, $lineno, \@tokens); + push(@{$self->{greplint}}, @grep_errors); + $self->{nerrs} += @grep_errors; + } return unless $emit_all || @$problems; my $c = main::fd_colors(1); my ($erropen, $errclose) = -t 1 ? ("$c->{rev}$c->{red}", $c->{reset}) : ('?!', '?!');
@@ -296,15 +305,21 @@ sub check_script { $emit->("?!ERR?! $path: $!\n"); next; } - my $s = do { local $/; <$fh> }; + my @raw_lines = <$fh>; close($fh); + my $s = join('', @raw_lines); my $parser = ChainlintParser->new(\$s); + $parser->{file} = $path; + $parser->{raw_lines} = \@raw_lines; + $parser->{greplint} = []; 1 while $parser->parse_cmd(); if (@{$parser->{output}}) { my $c = fd_colors(1); my $s = join('', @{$parser->{output}}); $emit->("$c->{bold}$c->{blue}# chainlint: $path$c->{reset}\n" . $s); } + $emit->(join('', map { "$_\n" } @{$parser->{greplint}})) + if @{$parser->{greplint}}; $ntests += $parser->{ntests}; $nerrs += $parser->{nerrs}; }
@@ -323,6 +338,7 @@ sub exit_code { Getopt::Long::Configure(qw{bundling}); GetOptions( "emit-all!" => \$emit_all, + "greplint!" => \$greplint, "jobs|j=i" => \$jobs, "stats|show-stats!" => \$show_stats) or die("option error\n"); $jobs = ncores() if $jobs < 1;
Signature · 1 line
2.47.3