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

Prefer use of interpolated strings in PresentationCore #8616

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

Conversation

halgab
Copy link
Contributor

@halgab halgab commented Dec 30, 2023

Follow-up to #8519

Description

We replace as many manual string concatenations and string.Format with interpolated strings. This saves allocations in many instances, and makes for a more readable code.

Customer Impact

Less allocations

Regression

No

Testing

CI

Risk

Low. Most changes are automated using the IDE

Microsoft Reviewers: Open in CodeFlow

@ghost ghost assigned halgab Dec 30, 2023
@ghost ghost added the PR metadata: Label to tag PRs, to facilitate with triage label Dec 30, 2023
@ghost ghost requested review from dipeshmsft and singhashish-wpf December 30, 2023 19:45
@ghost ghost added the Community Contribution A label for all community Contributions label Dec 30, 2023
@ghost ghost added the draft label Dec 30, 2023
@halgab halgab marked this pull request as ready for review December 30, 2023 21:33
@halgab halgab requested a review from a team as a code owner December 30, 2023 21:33
@halgab halgab force-pushed the interpolated-string-core branch from 429535a to 4dd05ab Compare December 30, 2023 21:50
@halgab halgab changed the title Prefer use interpolated strings in PresentationCore Prefer use of interpolated strings in PresentationCore Dec 30, 2023
@ghost ghost removed the draft label Dec 31, 2023
Copy link
Contributor

Choose a reason for hiding this comment

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

@halgab A DRT(test) is failing on this PR: DrtColorAPI
You can find this test on our public repo: https://github.com/dotnet/wpf-test

I think so the test is failing due to some missing elements in this file after change.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

After having another read, the difference between the current version and mine is that when nativeColorValue is empty, the current version still appends a separator value, which is not the case with my version. I feel like my version is more correct.
Tell me if you prefer that I maintain the current behaviour.

Copy link
Contributor

Choose a reason for hiding this comment

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

Your changes are functionally different. Floats were formatted using format previously but are now always formatted using R. Also, the loop in your changes always prepends the separator instead of prepending the separator only when between items: ",A,B,C" vs "A,B,C". Also, there should be a space here between {uriString} and {scRgbColor.a:R}, it was there before your changes.

I would recommend testing your changes because by looking at the code I don't see how your changes returns the same result as the existing code.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Those are valid points. I'll rollback the functional changes. I'd like to keep the constant format though, as format is always set to R before this loop, and that's what makes it possible to use interpolated strings. What do you think?

+ StrokeFIndices.GetStringRepresentation(_inSegment.BeginFIndex) + ","
+ StrokeFIndices.GetStringRepresentation(_inSegment.EndFIndex) + ","
+ StrokeFIndices.GetStringRepresentation(_hitSegment.EndFIndex) + "}";
return $"{{{StrokeFIndices.GetStringRepresentation(_hitSegment.BeginFIndex)},{StrokeFIndices.GetStringRepresentation(_inSegment.BeginFIndex)},{StrokeFIndices.GetStringRepresentation(_inSegment.EndFIndex)},{StrokeFIndices.GetStringRepresentation(_hitSegment.EndFIndex)}}}";
Copy link
Member

Choose a reason for hiding this comment

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

@halgab IMO, this change makes it a bit harder to understand what's happening here. Maybe we can use string.Format here ?

Copy link
Member

@h3xds1nz h3xds1nz Aug 27, 2024

Choose a reason for hiding this comment

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

@dipeshmsft We can just use regular interpolation and space it out, like this:

return $"{Int32.Parse("1")}," +
       $"{Int32.Parse("1")}";

It will produce the following:

DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(1, 2);
defaultInterpolatedStringHandler.AppendFormatted<int>(int.Parse("1"));
defaultInterpolatedStringHandler.AppendLiteral(",");
defaultInterpolatedStringHandler.AppendFormatted<int>(int.Parse("1"));
defaultInterpolatedStringHandler.ToStringAndClear();

So the change like this:

return $"{{{StrokeFIndices.GetStringRepresentation(_hitSegment.BeginFIndex)}," +
         $"{StrokeFIndices.GetStringRepresentation(_inSegment.BeginFIndex)}," +
         $"{StrokeFIndices.GetStringRepresentation(_inSegment.EndFIndex)}," +
         $"{StrokeFIndices.GetStringRepresentation(_hitSegment.EndFIndex)}}}";

Copy link
Member

Choose a reason for hiding this comment

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

@h3xds1nz, I was just going through DefaultInterpolatedStringHandler in your PR and your suggestion sounds good.

((IFormattable)StartPoint).ToString(format, provider) +
(segments != null ? segments.ConvertToString(format, provider) : "") +
(IsClosed ? "z" : "");
return $"M{((IFormattable)StartPoint).ToString(format, provider)}{(segments != null ? segments.ConvertToString(format, provider) : "")}{(IsClosed ? "z" : "")}";
Copy link
Member

Choose a reason for hiding this comment

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

Again, this seems hard to be understood. Maybe consider using string.Concat / string.Format here.

Copy link
Member

@h3xds1nz h3xds1nz Aug 27, 2024

Choose a reason for hiding this comment

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

Same here, we can just use regular string interpolation and space it out over multiple lines, no need for string.Format ugliness.

{
sb.AppendFormat(provider,"{0:" + format + "}",nativeColorValue[i]);
sb.Append(provider, $"{nativeColorValue[i]:R}");
Copy link
Member

Choose a reason for hiding this comment

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

Rather than this, it would be better to keep using format here. Although, I don't think we are going to change static format, this keeps using format and keeps it maintainable.

Suggested change
sb.Append(provider, $"{nativeColorValue[i]:R}");
sb.AppendFormat(provider, $"{{0:{format}}}", nativeColorValue[i]);

sb.AppendFormat(provider, "{0}{1} ", Parsers.s_ContextColor, uriString);
sb.AppendFormat(provider,"{1:" + format + "}{0}",separator,scRgbColor.a);
for (int i= 0; i< nativeColorValue.GetLength(0); ++i )
sb.Append(provider, $"{Parsers.s_ContextColor}{uriString} {scRgbColor.a:R}{separator}");
Copy link
Member

Choose a reason for hiding this comment

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

We can split this back in two lines and use the same suggestion as below for the second line.

@dipeshmsft
Copy link
Member

@halgab, can you take a look at the above PR and make the necessary changes ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Community Contribution A label for all community Contributions PR metadata: Label to tag PRs, to facilitate with triage
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants