-
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
[API] Add MemoryExtensions.Contains #25228
Comments
We already have Contains for T = char (for ReadOnlySpan). Do we need a generic API? The examples you provided showcase only T = byte and char (which is already covered). |
Yes, the intent was for this to be generic, with specializations for T = byte / char / other "integral" type when no comparer is specified. |
@ahsonkhan |
Looked through the code a bit more. The current API is |
I do not understand the advantages of Contains over IndexOf? I do not think that Contains will be significant faster then IndexOf. |
@AlexRadch In vectorized code paths, |
We have start offset and current offset how simple subtraction can draw back performance? |
It examins n-number of bytes at a time, depending on the size of the Vector. Once it has found a place where vector.equals is non-zero, it has to look at each individual byte of the resulting vector to determine where in that specific chunk of data it actually is. The last part is what should be removed with this API. If all we care about is a yes or no answer instead of where specifically, it can save examining individual elements. |
Also and |
I am considering working on this, but trying to understand the code. There's one section that confuses me: |
The block above the |
Ah. That makes sense now - thanks |
@jkotas has advised that the PR (referenced above) should be submitted to the CoreCLR repo, not CoreFX. |
Replacement PR 19863 created in CoreCLR. |
API proposals that go through the API review process are always issues in the CoreFx repository. |
I think you will still need to make a PR to CoreFx to expose the new members to the ref assembly. |
Right, the implementation should be in CoreCLR. The reference assemblies and tests should be in CoreFX. More details on how this works is in https://github.com/dotnet/coreclr/blob/master/Documentation/project-docs/adding_new_public_apis.md |
In term of the proposed API, I am planning on doing the following: public static class MemoryExtensions {
// Following 2 members in PR-1
bool Contains<T>(this Span<T> span, T value);
bool Contains<T>(this ReadOnlySpan<T> span, T value);
// Following 2 members in PR-2
bool Contains<T>(this Span<T> span, T value, IEqualityComparer<T> comparer);
bool Contains<T>(this ReadOnlySpan<T> span, T value, IEqualityComparer<T> comparer);
} In terms of PR-2, there do not appear to be any |
The |
I can write the sequential code, but I don't know that it's the right course of action since |
Please assign this to me. The PR is passing all checks. |
Status per this recipe for adding new public apis:
|
Collaborator invitation sent to @grant-d. Let us know once you accept. |
Thanks @karelz, I have accepted the invitation. Please assign this issue to me |
Considering that the aim of this change is to improve perf of such calls, do we also need to update corresponding callsites? A quick manual scan found about 30 such calls in CoreFx, eg Is it within scope to update these to use |
Yes, I think we should.
If you want to tackle that change as well, go for it :) I wonder how much this change would impact perf on benchmarks. Do you have a rough estimate? |
Ok, I will track that in a separate PR. I will also benchmark the core change, to make sure there's no regressions and get a feel for perf impact. PR linked below |
New PR for units and benchmarks linked above. |
Perf analysis here: dotnet/corefx#32293 (comment) |
All task done (see checklist above). |
We still have the following APIs left, so we should leave the issue open until they have been implemented: bool Contains<T>(this Span<T> span, T value, IEqualityComparer<T> comparer);
bool Contains<T>(this ReadOnlySpan<T> span, T value, IEqualityComparer<T> comparer); |
@ahsonkhan just curious: Why did we take PR which implements only 2 APIs out of 4? Was there a good reason to stage it? |
I couldn't get any feedback on the last 2 api's - see https://github.com/dotnet/corefx/issues/27526#issuecomment-419511258. |
The other 2 APIs are inconsistent with the rest of System.Memory public surface (none of the several hundreds other System.Memory APIs take IEqualityComparer). I assume that this was oversight during API review. We should have a new issue opened to discuss the |
Thanks @jkotas that makes sense :) ... oversights do happen. Let's separate them in that case. @ahsonkhan can you please bring the |
@GrabYourPitchforks, did you have a particular scenario or need that motivated adding the Contains overloads with
Sure, I will create a new issue. |
@ahsonkhan, did you open an issue? I didn't see one so created https://github.com/dotnet/corefx/issues/35962. Closing this one as completed. |
Proposal:
This is different from the
IndexOf
method in that we can perform certain optimizations if we know you don't need the exact index of where the value was found. There are a few places in the framework that would benefit from this.https://github.com/dotnet/corefx/blob/f0a032e4a1c8a848574a5db3e69e8a6fa0aae91e/src/System.Net.Primitives/src/System/Net/IPAddressParser.cs#L19
https://github.com/dotnet/coreclr/blob/8ec9bdfd53220b555f0b19eebd6a2c4ab5dab8bf/src/mscorlib/shared/System/Guid.cs#L430
https://github.com/dotnet/corefxlab/blob/c3624ba2c5f4fc4926ef64e5e35f2f6e895df6eb/src/System.Buffers.Experimental/System/Buffers/BufferExtensions.cs#L101
https://github.com/dotnet/coreclr/blob/22f1bc00d018a49f9550ee3b564f5f7737960b0d/src/mscorlib/shared/System/Version.cs#L342
The text was updated successfully, but these errors were encountered: