Skip to content

Prefer use of interpolated strings in PresentationCore #8616

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

Open
wants to merge 5 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
@halgab halgab force-pushed the interpolated-string-core branch from 4dd05ab to 4ea7631 Compare January 18, 2024 21:08
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.

@dipeshmsft
Copy link
Member

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

@halgab
Copy link
Contributor Author

halgab commented Feb 25, 2025

Hi. I'm sorry, I'm not sure when I'll have time to get back to this...

@dipeshmsft
Copy link
Member

@halgab, is it okay we take this up ?

@halgab
Copy link
Contributor Author

halgab commented Feb 26, 2025

Yes, of course

@dipeshmsft dipeshmsft force-pushed the interpolated-string-core branch from e7de550 to 412880e Compare March 18, 2025 07:09
@dipeshmsft dipeshmsft requested a review from a team as a code owner March 18, 2025 07:09
@dipeshmsft dipeshmsft self-assigned this Mar 18, 2025
Copy link

codecov bot commented Mar 18, 2025

Codecov Report

Attention: Patch coverage is 1.11732% with 177 lines in your changes missing coverage. Please review.

Project coverage is 11.45626%. Comparing base (a354bec) to head (cde707c).
Report is 82 commits behind head on main.

Additional details and impacted files
@@                 Coverage Diff                 @@
##                main       #8616         +/-   ##
===================================================
- Coverage   11.54183%   11.45626%   -0.08557%     
===================================================
  Files           3214        3214                 
  Lines         648502      648353        -149     
  Branches       71510       71552         +42     
===================================================
- Hits           74849       74277        -572     
- Misses        572401      572912        +511     
+ Partials        1252        1164         -88     
Flag Coverage Δ
Debug 11.35262% <1.11732%> (-0.08662%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

- Made change to CollectionHelper generator.
- Regenerated the collection files.
sb.AppendFormat(provider, "{0}{1} ", Parsers.s_ContextColor, uriString);
sb.AppendFormat(provider,"{1:" + format + "}{0}",separator,scRgbColor.a);
for (int i = 0; i < nativeColorValue.Length; ++i )
sb.Append(provider, $"{Parsers.s_ContextColor}{uriString} {scRgbColor.a:R}{separator}");
Copy link
Contributor

@himgoyalmicro himgoyalmicro Apr 14, 2025

Choose a reason for hiding this comment

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

We should use the variable format here.

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 Included in test pass PR metadata: Label to tag PRs, to facilitate with triage Status:Completed Status:Proposed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants