Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Extensions for immutable collection builders (#21055) #31071

Merged
merged 6 commits into from
Oct 1, 2018
Merged

Extensions for immutable collection builders (#21055) #31071

merged 6 commits into from
Oct 1, 2018

Conversation

davidkaya
Copy link

Added extensions for immutable builders, which should be prefered over the extensions on IEnumerable because of performance benefits.

Fixes #21055.

@davidkaya davidkaya changed the title Extensions for immutable builders (#21055) Extensions for immutable collection builders (#21055) Jul 14, 2018
@Wraith2
Copy link
Contributor

Wraith2 commented Jul 14, 2018

No null checks?

@karelz karelz added this to the 3.0 milestone Jul 19, 2018
@joshfree joshfree requested review from AArnott and safern July 20, 2018 16:46
Copy link

@AArnott AArnott left a comment

Choose a reason for hiding this comment

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

Thanks for preparing this change. My only concern with this approach is what I just added to the discussion over here.

/// </summary>
/// <param name="builder">The builder to create the immutable array from.</param>
/// <returns>An immutable array.</returns>
[Pure]
Copy link

Choose a reason for hiding this comment

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

Given that [Pure] has no runtime impact, why isn't the attribute included in the ref assemblies so that analyzers can do their work based on it?

Copy link
Contributor

Choose a reason for hiding this comment

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

Looking at it (and not the author) I'd guess that it's because none of the other definitions in the ref assembly have them and he followed the existing pattern on the assumption that someone in the past had good reason to do this.

Adding them in ref would require a dependency on System.Diagnostics.Contracts which would seem an odd thing to do.

Copy link

Choose a reason for hiding this comment

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

Agreed: this PR itself looks consistent with precedent. So my question was perhaps more directed at the authors of the ref assemblies in general. @danmosemsft @csharpfritz perhaps?

Choose a reason for hiding this comment

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

I think I was included incorrectly on this...

Copy link
Member

Choose a reason for hiding this comment

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

[Pure] is part of Contracts, right? We don't use or enforce those and generally have been removing contract annotations. I believe we discussed this before in the context of immutable collections in fact and @AArnott did you indicate you didn't believe in them either? In which case I guess these can all be removed unless they are somehow still useful to .NET Framework consumers even though they're not on the ref assembly and probably not consistent either.

Copy link
Member

Choose a reason for hiding this comment

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

It seems to me we likely want an analyzer proving it adds value first.

Right. Based on the past experiments with Code Contracts, it is very hard to get a return on investment with this. The annotations do catch a few bugs, but they require a prohibitive amount of manual work to do that.

Copy link
Member

Choose a reason for hiding this comment

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

Annotations aside, if the compiler included an analyzer with a hard coded list of most commonly misused pure functions (even just those on String) it would provide non zero value with ?no false positives. 😃

Copy link

Choose a reason for hiding this comment

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

Based on the past experiments with Code Contracts, it is very hard to get a return on investment with this.

Code Contracts preceded roslyn analyzers, and required custom build steps, VS extensions, and/or post-compilation IL-rewriting . Roslyn analyzers are proven to be effective and popular. Let's not let Code Contracts' failure deter us from tapping Analyzers for solving this simple issue.

Choose a reason for hiding this comment

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

The Pure attribute is checked by Rule CA1806 in Microsoft.CodeQuality.Analyzers.

Copy link

Choose a reason for hiding this comment

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

Thanks for redrawing attention to this, @Grauenwolf. I've filed a separate issue to track the issue: https://github.com/dotnet/corefx/issues/34778

@karelz
Copy link
Member

karelz commented Aug 15, 2018

@safern @ianhays is the PR ready for merge?

@karelz
Copy link
Member

karelz commented Sep 5, 2018

@safern ping?

Copy link
Member

@safern safern left a comment

Choose a reason for hiding this comment

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

LGTM.

@safern
Copy link
Member

safern commented Sep 13, 2018

@davidkaya would you mind fixing the merge conflicts?

@davidkaya
Copy link
Author

@safern Should be fine now.

@safern safern merged commit 9f404b4 into dotnet:master Oct 1, 2018
@safern
Copy link
Member

safern commented Oct 1, 2018

@safern Should be fine now.

Thanks.

picenka21 pushed a commit to picenka21/runtime that referenced this pull request Feb 18, 2022
…otnet/corefx#31071)

* Extensions for immutable builders (dotnet/corefx#21055)

Added extensions for immutable builders, which should be prefered over the extensions on IEnumerable because of performance benefits.

* Extension for immutable sorted dictionary builder (dotnet/corefx#21055)

* Extensions for immutable builders in the reference api (dotnet/corefx#21055)

* Tests for immutable builder extensions (dotnet/corefx#21055)

* Null check in extensions for immutable collection builders (dotnet/corefx#21055)


Commit migrated from dotnet/corefx@9f404b4
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.