Skip to content

perf: Performance Optimization - Hot Path Improvements #4159

@thomhurst

Description

@thomhurst

Overview

This epic tracks performance optimizations targeting TUnit's hot paths in test registration, orchestration, and execution. The goal is holistic performance improvement across test discovery speed, execution throughput, and memory pressure.

Constraints

  • No public API changes - All optimizations are internal implementation details
  • AOT/Trimming compatible - Changes must work with Native AOT and IL trimming
  • Incremental & measurable - Each change should be profiled before/after

Analysis Methodology

Static code analysis identified performance anti-patterns in hot paths:

  • LINQ allocations (.Any(), .Where(), .OrderBy(), .Select().ToArray())
  • Excessive object allocations per-test
  • Lock contention limiting parallel throughput
  • Defensive copies and intermediate collections

Sub-Issues

Priority Issue Scope Status
P0 #4160 - LINQ Elimination in Hot Paths 8 files, ~25 locations Open
P1 #4161 - Object Pooling for Frequent Allocations 5 files, ~10 locations Open
P1 #4162 - Lock Contention Reduction 2 files, complex refactor Open
P2 #4163 - Allocation Reduction via Span/ArrayPool 6 files, ~15 locations Open

Profiling Approach

Using dotnet-trace + SpeedScope for profile-guided optimization:

# Capture profile
dotnet-trace collect --name TUnit.PerformanceBenchmarks -- \
    dotnet run --project TUnit.PerformanceBenchmarks -c Release

# Convert for SpeedScope
dotnet-trace convert trace.nettrace --format speedscope

Benchmark Suite

TUnit.PerformanceBenchmarks project with:

  • Mixed realistic test patterns (60% simple, 30% data-driven, 10% lifecycle)
  • Scale tiers: 100, 500, 1,000, 5,000, 10,000 tests
  • Reproducible baselines for regression testing

Success Criteria

  • Measurable improvement in test discovery time at 10k scale
  • Reduced GC pressure during test execution
  • No performance regression in any tier
  • All changes pass existing test suite

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions