Skip to content

SequenceComparer: Optimize by using SequenceEqual#12356

Merged
ScarletKuro merged 4 commits intoMudBlazor:devfrom
ScarletKuro:small_optimize
Dec 26, 2025
Merged

SequenceComparer: Optimize by using SequenceEqual#12356
ScarletKuro merged 4 commits intoMudBlazor:devfrom
ScarletKuro:small_optimize

Conversation

@ScarletKuro
Copy link
Member

@ScarletKuro ScarletKuro commented Dec 26, 2025

It's faster as it checks for ICollection, IList, also checks if it can use spans in case of primitive types etc.

Also improved CollectionComparer.GetHashCode.

The old implementation used Distinct + OrderBy + a combiner to normalize hash codes:

  • Sorting was used to make the combination order-independent.
  • This worked, but sorting is unnecessary, allocates memory, and costs O(n log n) time.
  • Hash codes are called frequently by dictionaries and hash sets, so they should be cheap.

The new implementation avoids sorting:

  • Uses a HashSet to process distinct elements only.
  • Combines hash codes with XOR in a single pass.
  • No extra allocations for sorting, much faster, simpler.

It directly matches the documented semantics:

Order of the sequence is not relevant, neither are multiple entries of the same value.

/// Note: Order of the sequence is not relevant, neither are multiple entries of the same value !

So the new GetHashCode is simpler, faster, and fully correct for the intended behavior.

Checklist:

  • I've read the contribution guidelines
  • My code follows the style of this project
  • I've added or updated relevant unit tests

@mudbot mudbot bot added enhancement Adds a new feature or enhances existing functionality (not fixing a defect) in the main library performance Time/memory/CPU/allocation performance characteristics labels Dec 26, 2025
@ScarletKuro ScarletKuro merged commit b592ce2 into MudBlazor:dev Dec 26, 2025
6 checks passed
@ScarletKuro ScarletKuro deleted the small_optimize branch December 26, 2025 14:50
This was referenced Feb 20, 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

enhancement Adds a new feature or enhances existing functionality (not fixing a defect) in the main library performance Time/memory/CPU/allocation performance characteristics

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant