Skip to content

Comments

perf: restrict AbstractTestClassWithDataSourcesAnalyzer to source assembly#4923

Merged
thomhurst merged 1 commit intomainfrom
perf/abstract-analyzer-scan
Feb 19, 2026
Merged

perf: restrict AbstractTestClassWithDataSourcesAnalyzer to source assembly#4923
thomhurst merged 1 commit intomainfrom
perf/abstract-analyzer-scan

Conversation

@thomhurst
Copy link
Owner

Summary

  • Restrict type scanning to source assembly only: Changed GetAllNamedTypes(context.Compilation.GlobalNamespace) to GetAllNamedTypes(context.Compilation.Assembly.GlobalNamespace). Previously the analyzer recursively walked every type in the entire compilation (including all referenced assemblies) for each abstract test class analyzed. Now it only scans types defined in the source assembly, which is all that's needed since subclasses must be in the user's code.
  • Remove redundant IsInSource filter: Since Assembly.GlobalNamespace only contains types from the source assembly, the subsequent type.Locations.Any(l => l.IsInSource) check was redundant and has been removed.
  • Replace fragile string-based data source detection with interface check: Removed the typeName.Contains("DataSource") || typeName == "ArgumentsAttribute" string check that walked the type hierarchy. The IDataSourceAttribute interface check is comprehensive and authoritative -- all data source attributes (ArgumentsAttribute, MethodDataSourceAttribute, ClassDataSourceAttribute, etc.) implement IDataSourceAttribute either directly or through ITypedDataSourceAttribute<T> / IAsyncUntypedDataSourceGeneratorAttribute.

Closes #4864

Test plan

  • All 12 existing AbstractTestClassWithDataSourcesAnalyzerTests pass on both .NET 8 and .NET 9
  • dotnet build TUnit.Analyzers/TUnit.Analyzers.csproj succeeds with no errors

…embly types

The analyzer was recursively walking all types in the entire compilation
(including all referenced assemblies) to find concrete subclasses of
abstract test classes. This is O(n) over all types for every abstract
class analyzed.

Fix by using `Compilation.Assembly.GlobalNamespace` instead of
`Compilation.GlobalNamespace` to only scan types in the source assembly.
This also makes the redundant `IsInSource` location check unnecessary.

Additionally, remove the fragile string-based data source detection
(`typeName.Contains("DataSource") || typeName == "ArgumentsAttribute"`)
which walked the type hierarchy. The `IDataSourceAttribute` interface
check is comprehensive and authoritative since all data source attributes
implement it.

Closes #4864
@claude
Copy link
Contributor

claude bot commented Feb 19, 2026

Code review

No issues found. Checked for bugs and CLAUDE.md compliance.

Summary

This is a well-targeted performance improvement to AbstractTestClassWithDataSourcesAnalyzer. All three changes are correct:

  1. Restricting to context.Compilation.Assembly.GlobalNamespace — Concrete subclasses with [InheritsTests] can only exist in the user's source assembly, never in referenced libraries. Scanning the full compilation was pure wasted work.

  2. Removing the IsInSource filter — This guard was only needed because the previous scan included metadata types from referenced assemblies. With Assembly.GlobalNamespace, every returned type is guaranteed to be in source, making the check redundant.

  3. Replacing string-based detection with the interface check — All TUnit data source attributes (ArgumentsAttribute, MethodDataSourceAttribute, ClassDataSourceAttribute, MatrixDataSourceAttribute, CombinedDataSourcesAttribute, DataSourceGeneratorAttribute<T>, FsCheckPropertyAttribute, etc.) implement IDataSourceAttribute either directly or transitively. The AllInterfaces Roslyn API handles transitive interface resolution, so manually walking the base-type chain is unnecessary. The interface check is both more authoritative and more efficient.

@thomhurst thomhurst merged commit ac7d12f into main Feb 19, 2026
14 checks passed
@thomhurst thomhurst deleted the perf/abstract-analyzer-scan branch February 19, 2026 06:57
This was referenced Feb 22, 2026
This was referenced Feb 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

perf: AbstractTestClassWithDataSourcesAnalyzer scans all types globally

1 participant