Skip to content

Conversation

xtqqczze
Copy link
Contributor

@xtqqczze xtqqczze commented Aug 13, 2025

Improvements diffs expected.

Related: #118655

Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-collections
See info in area-owners.md if you want to be subscribed.

@xtqqczze
Copy link
Contributor Author

@MihuBot

@@ -161,7 +161,7 @@ private void AddRange(IEnumerable<KeyValuePair<TKey, TValue>> enumerable)
}
else if (enumerable.GetType() == typeof(List<KeyValuePair<TKey, TValue>>))
{
span = CollectionsMarshal.AsSpan((List<KeyValuePair<TKey, TValue>>)enumerable);
span = CollectionsMarshal.AsSpan(Unsafe.As<List<KeyValuePair<TKey, TValue>>>(enumerable));
Copy link
Member

@stephentoub stephentoub Aug 13, 2025

Choose a reason for hiding this comment

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

Thanks, but I do not want to introduce unsafe code for things like this. If it's material, the cast should be elided by the JIT, if it's not already.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@EgorBo The diffs show the cast is not elided.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, but I do not want to introduce unsafe code for things like this.

We already use the unsafe method CollectionsMarshal.AsSpan here.

Copy link
Member

Choose a reason for hiding this comment

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

Thanks, but I do not want to introduce unsafe code for things like this.

We already use the unsafe method CollectionsMarshal.AsSpan here.

That doesn't mean we need to add more. They're also "unsafe" in very different ways. Unsafe.As lets you completely break type safety. AsSpan is on the marshal class only because there's a potential for someone to make changes that get lost if they get the span, then edit the list in a way that causes it to grow, and then edit the span; it's not a memory safety issue.

Copy link
Member

Choose a reason for hiding this comment

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

@EgorBo The diffs show the cast is not elided.

Yes, this is a known issue, I can't find the exact github issue, but I do remember it exists. Basically, the same repro I mentioned here: #118655 (comment)
I'll try to fix it in .NET 11.0. In case if you're looking for an inspiration on what to contribute to dotnet/runtime repo, anything that replaces unsafe code with safe (ideally, with zero overhead) would be more than appreciated 🙂

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Collections community-contribution Indicates that the PR has been added by a community member NO-MERGE The PR is not ready for merge yet (see discussion for detailed reasons)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants