Skip to content

Optimize CollectionAssert: hoist GetTypeInfo() and avoid redundant eumerations#7536

Open
Evangelink wants to merge 1 commit intomainfrom
dev/amauryleve/collection-assert
Open

Optimize CollectionAssert: hoist GetTypeInfo() and avoid redundant eumerations#7536
Evangelink wants to merge 1 commit intomainfrom
dev/amauryleve/collection-assert

Conversation

@Evangelink
Copy link
Member

  • AllItemsAreInstancesOfType: Move expectedType.GetTypeInfo() outside the loop since it is loop-invariant, avoiding a redundant call per collection element.

  • AreNotEquivalent: Cache Count() results in local variables instead of calling Count() and then Any() separately, eliminating a third full enumeration of the collection.

  • AreEquivalent: Replace Any() call with a check against the already-cached count variable.

Copilot AI review requested due to automatic review settings March 12, 2026 15:05
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR optimizes a few hot paths in CollectionAssert by avoiding redundant reflection calls and repeated full enumerations of input sequences, improving efficiency without changing assertion semantics.

Changes:

  • AreEquivalent: reuse the already-computed expectedCollectionCount instead of calling expected.Any() again.
  • AreNotEquivalent: cache Count() results for both sequences and reuse the cached count for the empty-check.
  • AllItemsAreInstancesOfType: hoist expectedType.GetTypeInfo() outside the loop to avoid repeating it per element.

You can also share your feedback on Copilot code review. Take the survey.

…umerations

- AllItemsAreInstancesOfType: Move expectedType.GetTypeInfo() outside the loop since it is loop-invariant, avoiding a redundant call per collection element.

- AreNotEquivalent: Cache Count() results in local variables instead of calling Count() and then Any() separately, eliminating a third full enumeration of the collection.

- AreEquivalent: Replace Any() call with a check against the already-cached count variable.
@Evangelink Evangelink force-pushed the dev/amauryleve/collection-assert branch from 89b213b to efea5ac Compare March 12, 2026 15:38
@Evangelink Evangelink enabled auto-merge March 12, 2026 15:38
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.

3 participants