From b3604e8c91f4ddaa0948a4c8fea131a244c9b567 Mon Sep 17 00:00:00 2001 From: Tom Longhurst <30480171+thomhurst@users.noreply.github.com> Date: Wed, 11 Nov 2026 22:53:21 +0000 Subject: [PATCH 1/2] perf(sourcegen): make EquatableArray a class to cut cold-build JIT As a struct, every incremental pipeline stage carrying an EquatableArray forced the compiler process to JIT its own copy of Roslyn's generic node and state-table code. As a class those stages share the precompiled reference-type code: about 650 fewer JIT-compiled methods per cold compile. --- .../Models/EquatableArray.cs | 30 +++++++++++++------ 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/src/TUnit.Core.SourceGenerator/Models/EquatableArray.cs b/src/TUnit.Core.SourceGenerator/Models/EquatableArray.cs index 3d7770a29ba..f49b604ec2b 100644 --- a/src/TUnit.Core.SourceGenerator/Models/EquatableArray.cs +++ b/src/TUnit.Core.SourceGenerator/Models/EquatableArray.cs @@ -6,8 +6,14 @@ namespace TUnit.Core.SourceGenerator.Models; /// A wrapper around arrays that provides value equality semantics. /// Essential for incremental source generators where proper caching depends on equality comparison. /// +/// +/// A class, not a struct, on purpose. Incremental pipeline nodes are generic over their element type, +/// and the runtime cannot share generic code across value-type arguments: as a struct, every pipeline +/// stage carrying an EquatableArray made the compiler process JIT its own copy of Roslyn's node +/// and state-table code (about 930 methods and 25 ms in a cold build of a small test project). +/// /// The element type, which must implement IEquatable<T> -public readonly struct EquatableArray : IEquatable>, IEnumerable +public sealed class EquatableArray : IEquatable>, IEnumerable where T : IEquatable { private readonly T[]? _array; @@ -16,7 +22,7 @@ namespace TUnit.Core.SourceGenerator.Models; public EquatableArray(IEnumerable? items) => _array = items?.ToArray(); - public static EquatableArray Empty => new(Array.Empty()); + public static EquatableArray Empty { get; } = new(Array.Empty()); public T[] AsArray() => _array ?? Array.Empty(); @@ -24,10 +30,15 @@ namespace TUnit.Core.SourceGenerator.Models; public T this[int index] => (_array ?? Array.Empty())[index]; - public bool Equals(EquatableArray other) + public bool Equals(EquatableArray? other) { + if (ReferenceEquals(this, other)) + { + return true; + } + var left = _array ?? Array.Empty(); - var right = other._array ?? Array.Empty(); + var right = other?._array ?? Array.Empty(); if (left.Length != right.Length) { @@ -75,19 +86,20 @@ IEnumerator IEnumerable.GetEnumerator() return GetEnumerator(); } - public static bool operator ==(EquatableArray left, EquatableArray right) + // A null reference compares equal to an empty array, as default(EquatableArray) did as a struct. + public static bool operator ==(EquatableArray? left, EquatableArray? right) { - return left.Equals(right); + return (left ?? Empty).Equals(right); } - public static bool operator !=(EquatableArray left, EquatableArray right) + public static bool operator !=(EquatableArray? left, EquatableArray? right) { - return !left.Equals(right); + return !(left == right); } public static implicit operator EquatableArray(T[]? array) => new(array); - public static implicit operator T[](EquatableArray array) => array.AsArray(); + public static implicit operator T[](EquatableArray? array) => array?.AsArray() ?? Array.Empty(); } /// From 4330e00f278ccee0d0152f61b36202b598e4a1d0 Mon Sep 17 00:00:00 2001 From: Tom Longhurst <30480171+thomhurst@users.noreply.github.com> Date: Wed, 11 Nov 2026 23:17:21 +0000 Subject: [PATCH 2/2] fix(sourcegen): make EquatableArray typed Equals reject null like object Equals --- src/TUnit.Core.SourceGenerator/Models/EquatableArray.cs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/TUnit.Core.SourceGenerator/Models/EquatableArray.cs b/src/TUnit.Core.SourceGenerator/Models/EquatableArray.cs index f49b604ec2b..31c9c41a5bb 100644 --- a/src/TUnit.Core.SourceGenerator/Models/EquatableArray.cs +++ b/src/TUnit.Core.SourceGenerator/Models/EquatableArray.cs @@ -32,13 +32,18 @@ public sealed class EquatableArray : IEquatable>, IEnumerab public bool Equals(EquatableArray? other) { + if (other is null) + { + return false; + } + if (ReferenceEquals(this, other)) { return true; } var left = _array ?? Array.Empty(); - var right = other?._array ?? Array.Empty(); + var right = other._array ?? Array.Empty(); if (left.Length != right.Length) { @@ -89,7 +94,7 @@ IEnumerator IEnumerable.GetEnumerator() // A null reference compares equal to an empty array, as default(EquatableArray) did as a struct. public static bool operator ==(EquatableArray? left, EquatableArray? right) { - return (left ?? Empty).Equals(right); + return (left ?? Empty).Equals(right ?? Empty); } public static bool operator !=(EquatableArray? left, EquatableArray? right)