Skip to content

Add benchmark for ImmutableArrayExtensions#4915

Merged
LoopedBard3 merged 6 commits intodotnet:mainfrom
prozolic:ImmutableArrayExtensions_Benchmark
Sep 24, 2025
Merged

Add benchmark for ImmutableArrayExtensions#4915
LoopedBard3 merged 6 commits intodotnet:mainfrom
prozolic:ImmutableArrayExtensions_Benchmark

Conversation

@prozolic
Copy link
Contributor

Performance benchmark for ImmutableArrayExtensions.SequenceEqual method (related to dotnet/runtime#118932).
I have created a new ImmutableArrayExtensions class and added one benchmark. While there are other extension methods for ImmutableArrayExtensions, this implementation focuses only on ImmutableArrayExtensions.SequenceEqual.

prozolic and others added 3 commits August 26, 2025 07:23
…sions.cs


Agreed, adding it.

Co-authored-by: xtqqczze <45661989+xtqqczze@users.noreply.github.com>
…sions.cs

Co-authored-by: xtqqczze <45661989+xtqqczze@users.noreply.github.com>
@prozolic
Copy link
Contributor Author

File name changed from ImmutableArrayExtensions.cs to Perf.ImmutableArrayExtensions.cs as well.

@DrewScoggins
Copy link
Member

How long does a single operation end up taking? We just want to make sure that we are not in the low nanosecond range.

@prozolic
Copy link
Contributor Author

The results themselves are the same as those listed in dotnet/runtime#118932.
The benchmark results are as follows:

| Method        | input       | Mean       | Error     | StdDev    | Median     | Min        | Max        | Gen0   | Allocated |
|-------------- |------------ |-----------:|----------:|----------:|-----------:|-----------:|-----------:|-------:|----------:|
| SequenceEqual | Array       |   5.671 ns | 0.3384 ns | 0.3897 ns |   5.544 ns |   5.419 ns |   7.055 ns |      - |         - |
| SequenceEqual | ICollection | 103.143 ns | 1.9016 ns | 1.7788 ns | 102.466 ns | 101.597 ns | 107.242 ns | 0.0037 |      32 B |
| SequenceEqual | IEnumerable | 107.665 ns | 2.1441 ns | 2.3832 ns | 106.737 ns | 105.110 ns | 113.926 ns | 0.0034 |      32 B |
| SequenceEqual | IList       |  53.489 ns | 0.1662 ns | 0.1555 ns |  53.459 ns |  53.242 ns |  53.848 ns |      - |         - |
| SequenceEqual | List        |   6.130 ns | 0.3136 ns | 0.3612 ns |   5.973 ns |   5.922 ns |   7.384 ns |      - |         - |

@prozolic prozolic requested a review from xtqqczze August 27, 2025 15:46
Copy link
Contributor

@xtqqczze xtqqczze left a comment

Choose a reason for hiding this comment

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

@prozolic I don’t have enough familiarity with this repository to provide a proper review.

@DrewScoggins
Copy link
Member

I will take a look at this today.

@xtqqczze
Copy link
Contributor

I think it would sense to get this in before dotnet/runtime#118932 so we can properly validate the new implementation.

DrewScoggins
DrewScoggins previously approved these changes Aug 28, 2025
Copy link
Member

@DrewScoggins DrewScoggins left a comment

Choose a reason for hiding this comment

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

LGTM

…Extensions.cs

Co-authored-by: xtqqczze <45661989+xtqqczze@users.noreply.github.com>
@xtqqczze
Copy link
Contributor

@prozolic Please update branch with merge commit to restart CI.

@prozolic
Copy link
Contributor Author

@xtqqczze Ok, I've updated the branch.

Copy link
Member

@LoopedBard3 LoopedBard3 left a comment

Choose a reason for hiding this comment

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

Looks good.

@LoopedBard3
Copy link
Member

Looks good and was previously approved. Current failures are unrelated, so merging.

@LoopedBard3 LoopedBard3 merged commit 33a9145 into dotnet:main Sep 24, 2025
71 of 80 checks passed
@xtqqczze
Copy link
Contributor

@prozolic I'm trying to run this benchmark locally, but I'm having issues, have created #4975.

@prozolic prozolic deleted the ImmutableArrayExtensions_Benchmark branch September 25, 2025 10:08
@xtqqczze

This comment was marked as outdated.

jeffhandley pushed a commit to dotnet/runtime that referenced this pull request Feb 16, 2026
# Summary

This PR improves the performance of
ImmutableArrayExtensions.SequenceEqual method through optimized
implementation strategies:

Key Performance Improvements:
 - Array: ~90% faster (68.885ns → 6.856ns)
 - List: ~91% faster (85.014ns → 7.676ns)
 - IList: ~54% faster (125.897ns → 57.836ns)
 - ICollection: No statistically significant performance difference
- IEnumerable: Uses existing implementation for this case because the
existing implementation had better performance and didn't allocate a
second enumerator
  
# Implementation Approach

The implementation was refined based on review feedback from
@neon-sunset and @xtqqczze to achieve optimal performance while
balancing complexity:

1. Fast path for common collections: Detects when items implement
`ICollection<T>` and delegates to the highly optimized
`Enumerable.SequenceEqual` using the underlying array
2. Compatibility preservation: Falls back to existing implementation for
other `IEnumerable<T>` types to maintain behavioral consistency

# Benchmark (dotnet/performance#4915)

Full benchmark results:
[MihuBot/runtime-utils#1565](MihuBot/runtime-utils#1565)
(X64)

| Method | Toolchain | input | Mean | Error | Ratio | Allocated | Alloc
Ratio |
|-------------- |---------- |------------
|-----------:|-----------:|------:|----------:|------------:|
| SequenceEqual | Main | Array | 68.885 ns | 0.1938 ns | 1.00 | - | NA |
| SequenceEqual | PR | Array | 6.856 ns | 0.2262 ns | 0.10 | - | NA |
| | | | | | | | |
| SequenceEqual | Main | ICollection | 120.542 ns | 7.4864 ns | 1.00 |
32 B | 1.00 |
| SequenceEqual | PR | ICollection | 114.458 ns | 1.2362 ns | 0.95 | 32
B | 1.00 |
| | | | | | | | |
| SequenceEqual | Main | IEnumerable | 121.923 ns | 7.7103 ns | 1.00 |
32 B | 1.00 |
| SequenceEqual | PR | IEnumerable | 118.370 ns | 1.4443 ns | 0.98 | 32
B | 1.00 |
| | | | | | | | |
| SequenceEqual | Main | IList | 125.897 ns | 9.9231 ns | 1.01 | 32 B |
1.00 |
| SequenceEqual | PR | IList | 57.836 ns | 0.9568 ns | 0.46 | - | 0.00 |
| | | | | | | | |
| SequenceEqual | Main | List | 85.014 ns | 1.7593 ns | 1.00 | - | NA |
| SequenceEqual | PR | List | 7.676 ns | 0.2719 ns | 0.09 | - | NA |

---------

Co-authored-by: Pranav Senthilnathan <pranav.senthilnathan@live.com>
Co-authored-by: xtqqczze <45661989+xtqqczze@users.noreply.github.com>
Co-authored-by: Pavel Savara <pavel.savara@gmail.com>
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.

4 participants

Comments