Skip to content
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

Code modernization in System.Linq #110910

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Code modernization in System.Linq #110910

wants to merge 2 commits into from

Conversation

roji
Copy link
Member

@roji roji commented Dec 23, 2024

Here are three relatively impactful code cleanups, done automatically and split into three separate commits for easier reviewing/undoing etc.

@xtqqczze
Copy link
Contributor

This might be too big to review. How about merging the most impactful change first, i.e. IDE0161: Convert to file-scoped namespace.

@stephentoub
Copy link
Member

stephentoub commented Dec 23, 2024

Use file-scoped namespace declarations in System.Linq

The repo doesn't use file-scoped namespaces; I don't think we want to change the style per library.

@@ -101,10 +101,10 @@ public override List<TSource> ToList()
if (count == 1)
{
// If GetCount returns 1, then _source is empty and only _item should be returned
return new List<TSource>(1) { _item };
return [_item];
Copy link
Member

Choose a reason for hiding this comment

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

In the common case where the consumer doesn't add more to the returned list, this is increasing the memory allocated.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good catch, will revert these.

Copy link
Member

Choose a reason for hiding this comment

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

Initial capacity shouldn't be an issue anymore as Roslyn will emit something like this now

List<TSource> list = new List<TSource>(1);
CollectionsMarshal.SetCount(list, 1);
Span<TSource> span = CollectionsMarshal.AsSpan(list);
span[0] = _item;

should we continue avoiding collection expressions in such cases?

@eiriktsarpalis
Copy link
Member

Use file-scoped namespace declarations in System.Linq

The repo doesn't use file-scoped namespaces; I don't think we want to change the style per library.

@stephentoub do we want to consider switching the repo to file-scoped namespaces in the future? Would be nice if we could save ourselves one tab of indentation.

@roji
Copy link
Member Author

roji commented Dec 23, 2024

This might be too big to review.

@xtqqczze note that each change is a distinct commit, that should help.

@EgorBo
Copy link
Member

EgorBo commented Dec 23, 2024

Use file-scoped namespace declarations in System.Linq

The repo doesn't use file-scoped namespaces; I don't think we want to change the style per library.

@stephentoub do we want to consider switching the repo to file-scoped namespaces in the future? Would be nice if we could save ourselves one tab of indentation.

afair some *.cs files have multiple namespace scopes in the same file?
I guess such a massive change will impact git blame experience (and, well, put all active PRs into a "in-conflict" state 😆)

@stephentoub
Copy link
Member

Use file-scoped namespace declarations in System.Linq

The repo doesn't use file-scoped namespaces; I don't think we want to change the style per library.

@stephentoub do we want to consider switching the repo to file-scoped namespaces in the future? Would be nice if we could save ourselves one tab of indentation.

afair some *.cs files have multiple namespace scopes in the same file? I guess such a massive change will impact git blame experience (and, well, put all active PRs into a "in-conflict" state 😆)

It also makes servicing much harder.

@roji
Copy link
Member Author

roji commented Dec 23, 2024

Thanks for the reviewing. Have removed the file-scoped namespaces, and corrected the other stuff.

@roji
Copy link
Member Author

roji commented Dec 23, 2024

It also makes servicing much harder.

Yeah, FWIW in EF and Npgsql we try to do this kind of thing (and general large code cleanups) before we lock down a major release, to make servicing easier. It's true file-scoped namespaces do make git blaming slightly harder - I personally do think it's worth prioritizing code niceness with them, and worth the one-time pain (but I get that it's non-trivial and removed that commit).

@stephentoub
Copy link
Member

before we lock down a major release, to make servicing easier

Can you elaborate?

@roji
Copy link
Member Author

roji commented Dec 23, 2024

Can you elaborate?

Just that before a release is locked down, we usually do an automated code cleanup, to align code to the repo settings in .editorconfig and similar; this fixes any violations that have accumulated over the year from contributions where some rule wasn't respected. The timing - just before locking down the major version - is specifically done to make servicing easier, compared to e.g. if we did the cleanup right after the cleanup, at which point any backport would risk a conflict because of styling etc.

@stephentoub
Copy link
Member

The timing - just before locking down the major version - is specifically done to make servicing easier, compared to e.g. if we did the cleanup right after the cleanup, at which point any backport would risk a conflict because of styling etc.

Thanks. dotnet/runtime invariably has multiple release branches active at any one time. That complicates this, eg when locking down the .NET 10 release, we'll still have release/8.0 and release/9.0 active for another 6-12 months.

@roji
Copy link
Member Author

roji commented Dec 23, 2024

@stephentoub EF has the same servicing policy, with the multiple release branches - and it's indeed not ideal... Though the number of patches that go into older releases is typically very small compared to the number that goes into the just-released version (for example, there's typically a larger number of backports needed just after a major release, but far fewer that needs to go back even further).

Not necessarily trying to argue for anything here - just sharing info.

@eiriktsarpalis
Copy link
Member

Should we perhaps open an issue tracking the introduction of file-scoped namespaces? Might be easier to invite debate from the wider team.

@SamMonoRT SamMonoRT requested a review from Copilot December 26, 2024 21:30

Choose a reason for hiding this comment

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

Copilot reviewed 73 out of 88 changed files in this pull request and generated no comments.

Files not reviewed (15)
  • src/libraries/System.Linq/src/System/Linq/Single.cs: Evaluated as low risk
  • src/libraries/System.Linq/src/System/Linq/GroupJoin.cs: Evaluated as low risk
  • src/libraries/System.Linq/src/System/Linq/Skip.SizeOpt.cs: Evaluated as low risk
  • src/libraries/System.Linq/src/System/Linq/Last.cs: Evaluated as low risk
  • src/libraries/System.Linq/src/System/Linq/Select.SpeedOpt.cs: Evaluated as low risk
  • src/libraries/System.Linq/src/System/Linq/Average.cs: Evaluated as low risk
  • src/libraries/System.Linq/src/System/Linq/SequenceEqual.cs: Evaluated as low risk
  • src/libraries/System.Linq/src/System/Linq/MaxMin.cs: Evaluated as low risk
  • src/libraries/System.Linq/src/System/Linq/AppendPrepend.SpeedOpt.cs: Evaluated as low risk
  • src/libraries/System.Linq/src/System/Linq/SkipTake.SpeedOpt.cs: Evaluated as low risk
  • src/libraries/System.Linq/src/System/Linq/First.cs: Evaluated as low risk
  • src/libraries/System.Linq/src/System/Linq/Skip.cs: Evaluated as low risk
  • src/libraries/System.Linq/src/System/Linq/Join.cs: Evaluated as low risk
  • src/libraries/System.Linq/src/System/Linq/OrderedEnumerable.SpeedOpt.cs: Evaluated as low risk
  • src/libraries/System.Linq/src/System/Linq/Count.cs: Evaluated as low risk
@roji
Copy link
Member Author

roji commented Dec 28, 2024

@eiriktsarpalis it doesn't seem from the above that there's a lot of appetite for doing that, but I'm definitely OK with it if @stephentoub, @EgorBo think it's worth discussing...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants