| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
There was a problem hiding this comment.
Dongbo Wang (@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. Dongbo Wang (@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));
Sorry, something went wrong.
| sb.AppendFormat( | ||
| CultureInfo.InvariantCulture, | ||
| " {0}{1} {2}{3};\n", | ||
| MapAttributesToMof(enumNames, attributes, embeddedInstanceType), | ||
| mofType, | ||
| member.Name, | ||
| arrayAffix); |
There was a problem hiding this comment.
This will only increase the number of allocations in the parser and make it slower.
Sorry, something went wrong.
There was a problem hiding this comment.
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.
Sorry, something went wrong.
There was a problem hiding this comment.
Such allocations will are removed in .Net 8.0 (for all APIs with params) so you can revert to sb.AppendFormat if you prefer.
Sorry, something went wrong.
There was a problem hiding this comment.
Since the new update is already in a readable way, I'm fine keeping it as is.
Sorry, something went wrong.
There was a problem hiding this comment.
But a question, Ilya (@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?
Sorry, something went wrong.
There was a problem hiding this comment.
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.)
Sorry, something went wrong.
There was a problem hiding this comment.
LGTM
I agree that this is much more readable and maintainable. Dongbo Wang (@daxian-dbw) thanks for making the changes.
Sorry, something went wrong.
|
Ilya (@iSazonov) I totally understand the good intention behind those changes -- some temporary array allocations will be saved for those string.Format usages with more than 3 formatting arguments. However, readability and stability are equally important (if not more important), and therefore, the perf change should not sacrifice readability and stability. 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 string.Format instances don't have the overhead of temp array allocation. For some other that do have an overhead, they may not be important for optimization (such as the ToString overloads in Binders). If it's more important to keep the template clear and readable, it's OK to not optimize it with the interpolated string. This micro-optimization is good add-on when it's appropriate, but it's not necessary to apply it everywhere unconditionally. |
Sorry, something went wrong.
|
This PR has 380 quantified lines of changes. In general, a change size of upto 200 lines is ideal for the best PR experience! Quantification details
Label : Large Size : +298 -82 Percentile : 78% Total files changed: 35 Change summary by file extension: .cs : +298 -82 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) |
Sorry, something went wrong.
|
🎉v7.4.0-preview.2 has been released which incorporates this pull request.:tada: Handy links: |
Sorry, something went wrong.
|
I'm not sure if this is by design, but it would not be my preference. # 7.3.8
PS7.3> pwsh -nop -c '(Get-Date | Group-Object).Name'
2023-10-19 13:28:30
# 7.4.0-preview.6
PS7.4> pwsh -nop -c '(Get-Date | Group-Object).Name'
10/19/2023 13:28:26
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. |
Sorry, something went wrong.
|
The use of Invariant culture needs enormous care As Andree Renneus (@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 . is the thousand separator in some cultures). Any replacement of a .ToString() - which will use local culture - with a something which is effectively .ToString(US Culture) will appear to non US users as a regression. |
Sorry, something went wrong.
| Back | FazBrowse Home | New Git URL |
PR Summary
There have been refactoring to the code base about replacing string.Format with string.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