-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Type test List<T>
in Enumerable.SequenceEquals and forward to MemoryExtensions.SequenceEqual(span...)
#97004
Conversation
Tagging subscribers to this area: @dotnet/area-system-linq Issue DetailsType test Seems like a simple performance win that was accidentally missed when
(sequence here is Related: #97000 once/if this is fixed,
|
It's a win when the test succeeds. It's pure overhead when it doesn't. If your scenario involves many calls to short non-lists, it's a net negative, which is why it wasn't previously done. We've been slowly measuring and opting in more cases using TryGetSpan. |
f47fcd4
to
1ed8ff9
Compare
My 5 cents: there were multiple improvements for casts over last few years so maybe we can reconsider some of the decisions against adding more fast-paths in LINQ (including ReadOnlyCollection) E.g. recent "profiled casts" improved quite a few LINQ benchmarks. |
1ed8ff9
to
98a4605
Compare
That's why we've been more lenient over the last few releases.
I'm still a little skeptical of this one. It's great for microbenchmarks in LINQ, but these APIs ends up being used from many different call sites with many different inputs. I suspect the wins that show up in microbenchmarks won't have nearly as positive impact on many uses. I'm glad it's there, and it'll certainly help some cases, but we can't use it as a crutch, and could easily be mislead if we're not careful, I think. |
You can say the same about other things optimized with PGO in general. Polymorphic casts (where different callers use different data) are not expected to be optimized with PGO as we try to optimize only monomorphic cases. Sure, it doesn't work well if an app starts to behave differently after certain point, but we don't have a better option besides recommending turning PGO off completely for such apps. Eventually, we'll improve this by enabling context-sensitive instrumentation, partial inlining, instrumentation for inlinees, etc. |
I do :-) It's just this particular optimization applies particularly well to LINQ microbenchmarks, as you've called out. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wouldn't object to this being included, however I don't think it's particularly impactful given that it only additionally targets List<T>
. Are there any other collections we could extract the span from? I can only think of ImmutableArray<T>
.
…ce comparison for arrays and lists
b548e3b
to
1aa701a
Compare
Since #97005 deemed to be unprofitable, I have reverted the rest of the change and only kept Numbers
(the non- As for extending Thanks. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks
…ce comparison for arrays and lists (dotnet#97004)
Type test
List<T>
inEnumerable.SequenceEqual
and forward toMemoryExtensions.SequenceEqual(span...)
, move the loops to local functions to allow the method to get inlined and type tests get optimized away.Seems like a simple performance win that was accidentally missed when
CollectionsMarshal.AsSpan
was added.(list here is
List<int>
, sequence here isEnumerable.Range
)Related: #97000 once/if this is fixed,
list1.SequenceEqual(list2)
will be able to get optimized down to direct call on their spans.