-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Revert some of the interpolated string changes #19018
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
Conversation
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.
@daxian-dbw I'm sorry if this annoys you.
- it's important to make the string template clear and easy to read.
I don't remember anyone changing these formatting lines, it happens very rarely. I mean this code is not something anybody have to read and analyze all the time. On the other hand there are benefits of reduced allocations and increased productivity.
- the resulted interpolated string contains method calls or long ternary expression, which makes it way less readable.
I agree that they could have been taken out of the format strings where possible.
Instead of reverting I have another suggestion. We could add comments with the old formatting lines before the new code. @daxian-dbw What do you think about this compromise? We could ask CarloToso to do this.
New code:
# Format: "(?<{0}>){1}"
patterns.Add(string.Create(CultureInfo.InvariantCulture, $"(?<{FullTextRuleGroupName}>){ValuePattern}"));Old code:
patterns.Add(string.Format(CultureInfo.InvariantCulture, "(?<{0}>){1}", FullTextRuleGroupName, ValuePattern));
...rosoft.PowerShell.Commands.Utility/commands/utility/FormatAndOutput/format-hex/Format-Hex.cs
Show resolved
Hide resolved
| sb.AppendFormat( | ||
| CultureInfo.InvariantCulture, | ||
| " {0}{1} {2}{3};\n", | ||
| MapAttributesToMof(enumNames, attributes, embeddedInstanceType), | ||
| mofType, | ||
| member.Name, | ||
| arrayAffix); |
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.
This will only increase the number of allocations in the parser and make it slower.
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.
In this particular case, sb.AppendFormat will incur an array allocation because there are 4 objects passed in.
Given that this is in a loop, I updated the code to continue using sb.Append(, but in a readable manner.
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.
Such allocations will are removed in .Net 8.0 (for all APIs with params) so you can revert to sb.AppendFormat if you prefer.
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.
Since the new update is already in a readable way, I'm fine keeping it as is.
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.
But a question, @iSazonov, if .NET 8 is going to support 4 or more formating objects in string.Format or StringBuilder.AppendFormat, then will most of the changes to string.Create(<interpolated-string>) be in vain?
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.
This will only remove allocating array for arguments. One more optimization.
Interpolated strings already use InterpolatedStringHandler which utilizes stackalloc to avoid allocations at all in formatting process. Also it excludes parsing format string again and again. (They added new API CompositeFormat on this week to cache format parsing.)
PaulHigin
left a comment
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.
LGTM
I agree that this is much more readable and maintainable. @daxian-dbw thanks for making the changes.
|
@iSazonov I totally understand the good intention behind those changes -- some temporary array allocations will be saved for those For remoting connection strings, message packets, and WSMan XML, it's best to not touch this code unnecessarily for stability reason, and certainly not make it less readable.
Comments are easy to be out of sync with the code. Some |
|
This PR has Quantification details
Why proper sizing of changes matters
Optimal pull request sizes drive a better predictable PR flow as they strike a
What can I do to optimize my changes
How to interpret the change counts in git diff output
Was this comment helpful? 👍 :ok_hand: :thumbsdown: (Email) |
|
🎉 Handy links: |
|
I'm not sure if this is by design, but it would not be my preference. I don't think datetime values should be converted to InvariantCulture in general, it's a bit confusing for anyone outside US.. perhaps a more "common" use case, I'm not sure if it's affecting other commands. |
|
The use of Invariant culture needs enormous care As @trackd mentions above, it results in "Least significant in the middle" date formatting which is considered broken outside the US, and a uses "." as a decimal separator which is not used in France, Germany, etc. (Worse Any replacement of a |
PR Summary
There have been refactoring to the code base about replacing
string.Formatwithstring.Create(<interpolate-string>). The list of relevant PRs are captured in #18974 (comment).After reviewing those PRs, I found not all the changes are appropriate, and need to revert some of them for one of the following 2 reasons:
Note that, since there are tons of changes, my review cannot be thorough enough to catch all those that needs to be reverted. If anyone notice any changes from those PRs that are questionable but not captured in this PR, please leave a comment or submit a new PR.
PR Checklist
.h,.cpp,.cs,.ps1and.psm1files have the correct copyright headerWIP:or[ WIP ]to the beginning of the title (theWIPbot will keep its status check atPendingwhile the prefix is present) and remove the prefix when the PR is ready.