Skip to content

Conversation

@TimothyMakkison
Copy link
Contributor

Call ToArray instead of ToList, most of the time the underlying collection contains zero items so it will return an empty array.

Before

image

After

image image

@thomhurst
Copy link
Owner

Summary

Changes Artifacts property from ToList() to ToArray() to reduce allocations when the collection is empty.

Critical Issues

None found ✅

Suggestions

Consider caching to eliminate repeated allocations:

The Artifacts property is accessed multiple times in hot paths (TUnit.Engine/Extensions/TestExtensions.cs:144,146 calls it twice). Each call allocates a new array/list from the ConcurrentBag.

While ToArray() is better for empty collections (returns cached Array.Empty), both ToArray() and ToList() allocate for non-empty collections on every access.

Consider caching if the bag is immutable after test execution starts. This would eliminate all repeated allocations.

Verdict

✅ APPROVE - The change improves performance for the common case (empty collections).

@thomhurst thomhurst merged commit 97bb1f1 into thomhurst:main Jan 21, 2026
9 of 10 checks passed
This was referenced Jan 25, 2026
This was referenced Jan 26, 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.

2 participants