Skip to content

Cache plugin registrant in test compiler#185598

Merged
auto-submit[bot] merged 1 commit into
flutter:masterfrom
Mr-Pepe:cache-plugin-registrant-in-test-compiler
May 5, 2026
Merged

Cache plugin registrant in test compiler#185598
auto-submit[bot] merged 1 commit into
flutter:masterfrom
Mr-Pepe:cache-plugin-registrant-in-test-compiler

Conversation

@Mr-Pepe

@Mr-Pepe Mr-Pepe commented Apr 26, 2026

Copy link
Copy Markdown
Contributor

Towards #69429 ("Make flutter test faster").

This change makes TestCompiler cache the plugin registrant instead
of regenerating it for each test file. Plugins are stable for the lifetime of a single flutter test
run. However, the Dart version of test files might differ, so the cache is keyed on the test file's
Dart version.

Benchmark
Flutter foundation test suite (32 files, 220 tests), flutter test -j 4, 3 runs + 1 warmup:

master this PR
Avg compile per subsequent file 68ms 55ms

That means the startup time for flutter test is reduced by ~13ms per test file on my machine. That quickly adds up and significantly improves Flutter's TDD experience.

Results were generated with the following script:

Benchmark
#!/usr/bin/env bash
# Reports per-run, for foundation suite, -j 4:
#   - wall      : total `flutter test` invocation time
#   - first     : compile time for the first test file (cache miss)
#   - subseq    : average compile time per subsequent file (cache hit / miss)
#   - total     : sum of all per-file compile times
#
# The "subseq" and "total" rows are the ones the PR moves; "wall" is dominated
# by test execution and is reported for completeness.

set -euo pipefail

FLUTTER_ROOT="${FLUTTER_ROOT:-$(pwd)}"
FLUTTER="$FLUTTER_ROOT/bin/flutter"
TARGET="$FLUTTER_ROOT/packages/flutter/test/foundation/"
RUNS="${RUNS:-3}"

cd "$FLUTTER_ROOT/packages/flutter"

bench_one() {
  local name="$1" log
  log=$(mktemp)
  local start end ms
  start=$(date +%s%N)
  "$FLUTTER" test -j 4 -v "$TARGET" >"$log" 2>&1
  end=$(date +%s%N)
  ms=$(( (end - start) / 1000000 ))

  local compiles n first sum rest_avg
  compiles=$(grep -oP 'Compiling .+ took \K\d+(?=ms)' "$log" || true)
  n=$(echo "$compiles" | grep -c . || true)
  first=$(echo "$compiles" | head -1)
  sum=$(echo "$compiles" | awk '{s+=$1} END{print s+0}')
  rest_avg=0
  if [[ "$n" -gt 1 ]]; then
    rest_avg=$(echo "$compiles" | tail -n +2 | awk '{s+=$1} END{print int(s/NR)}')
  fi
  printf "  %-8s wall=%5dms  first=%4dms  subseq=%3dms (n=%d)  total=%5dms\n" \
    "$name" "$ms" "$first" "$rest_avg" "$((n-1))" "$sum"
  rm -f "$log"
}

echo "Foundation suite (32 files, ~220 tests), -j 4, $RUNS runs + 1 warmup"
echo "Commit: $(cd "$FLUTTER_ROOT" && git rev-parse --short HEAD) ($(cd "$FLUTTER_ROOT" && git status --porcelain | wc -l) modified files)"
bench_one "warmup"
for i in $(seq 1 "$RUNS"); do
  bench_one "run_$i"
done

Pre-launch Checklist

  • I read the [Contributor Guide] and followed the process outlined there for submitting PRs.
  • I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools.
  • I read the [Tree Hygiene] wiki page, which explains my responsibilities.
  • I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement].
  • I signed the [CLA].
  • I listed at least one issue that this PR fixes in the description above.
  • I updated/added relevant documentation (doc comments with ///).
  • I added new tests to check the change I am making, or this PR is [test-exempt].
  • I followed the [breaking change policy] and added [Data Driven Fixes] where supported.
  • All existing and new tests are passing.

@github-actions github-actions Bot added the tool Affects the "flutter" command-line tool. See also t: labels. label Apr 26, 2026
@Mr-Pepe Mr-Pepe marked this pull request as ready for review April 26, 2026 21:37

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request modifies TestCompiler to cache the generated plugin registrant, only regenerating it when the language version of the test entrypoint changes. It adds a _registrantLanguageVersion field and updates the compilation logic to check for version mismatches. New tests verify the reuse and regeneration behavior. Feedback suggests removing an unnecessary null-check operator on Cache.flutterRoot.

Comment thread packages/flutter_tools/lib/src/test/test_compiler.dart
bkonyi
bkonyi previously approved these changes May 4, 2026
chingjun
chingjun previously approved these changes May 4, 2026
@Mr-Pepe Mr-Pepe dismissed stale reviews from chingjun and bkonyi via dc4d88e May 4, 2026 16:44
@Mr-Pepe Mr-Pepe force-pushed the cache-plugin-registrant-in-test-compiler branch from 73572c9 to dc4d88e Compare May 4, 2026 16:44
@github-actions github-actions Bot removed the CICD Run CI/CD label May 4, 2026
@Mr-Pepe

Mr-Pepe commented May 4, 2026

Copy link
Copy Markdown
Contributor Author

Fixed linting and rebased on newest master.

@bkonyi bkonyi added the CICD Run CI/CD label May 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CICD Run CI/CD tool Affects the "flutter" command-line tool. See also t: labels.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants