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

Use Span directly when available #9215

Merged
merged 4 commits into from
Oct 19, 2022
Merged

Use Span directly when available #9215

merged 4 commits into from
Oct 19, 2022

Conversation

emmauss
Copy link
Contributor

@emmauss emmauss commented Oct 17, 2022

What does the pull request do?

Adds implicit string operator for SpanCompat, and helpers for parsing span to value types to avoid string allocation.

What is the current behavior?

What is the updated/expected behavior with this PR?

How was the solution implemented (if it's not obvious)?

Checklist

Breaking changes

Obsoletions / Deprecations

Fixed issues

Addresses #7912

@avaloniaui-team
Copy link
Contributor

You can test this PR using the following package version. 11.0.999-cibuild0024871-beta. (feed url: https://nuget.avaloniaui.net/repository/avalonia-all/index.json) [PRBUILDID]

inString.Substring(0, percentIndex),
NumberStyles.Number,
CultureInfo.InvariantCulture,
var result = inString.Slice(0, percentIndex).TryParseNumberToDouble(
Copy link
Contributor

@robloo robloo Oct 17, 2022

Choose a reason for hiding this comment

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

I'm not a big fan of the new TryParseNumberToDouble type methods here:

  1. It assumes invariant culture and number styles (which I guess that's why the name is so long).
  2. Using the actual double.TryParse() is so much cleaner and more standards-friendly. It's clear culture is being handled correctly.
  3. The new methods don't actually do anything right unless compiled for .net versions that use spans. In .net standard you are actually going string -> span -> string. This is why I really didn't do this before. With this in mind you can't actually close Add internal Span-helpers to speed up parsing in some code parts #7912 either.

Copy link
Contributor

Choose a reason for hiding this comment

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

I would really just add a new version of double.TryParse() that supports span and .net standard 2.0 then use it here. Mentioned: https://github.com/AvaloniaUI/Avalonia/pull/9215/files#r997344774. That makes the code more future-proof, readable and culture-aware IMO.

Copy link
Member

Choose a reason for hiding this comment

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

About #3, I would actually say that we don't need to overcomplicate task just for netstandard2.0.
Majority of supported platforms will use .NET 6 or higher. Where netstandard2.0 is kept for small percent of .NET Framework apps build with Avalonia (mostly, as an integration with older framework apps) and some exotic Mono devices.

Copy link
Contributor

Choose a reason for hiding this comment

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

1-2. This is easier to read and should help avoid culture issues now that new parameters are added for number style and culture. Thanks.
3. I don't know the platforms or applications netstandard 2.0 is used for so if you think it's fine I won't disagree.

return double.TryParse(span.ToString(), NumberStyles.Number, CultureInfo.InvariantCulture,
out value);
#else
return double.TryParse(span, NumberStyles.Number, CultureInfo.InvariantCulture,
Copy link
Contributor

Choose a reason for hiding this comment

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

This is really the correct fix. Honestly, I would copy-paste this method out of newer .net versions and just add it Avalonia. Then we can use it uniformly in new code.

Copy link
Contributor

@robloo robloo Oct 17, 2022

Choose a reason for hiding this comment

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

https://github.com/dotnet/runtime/blob/4574ccb53981aaecfbf5e514a5114da2321e0843/src/libraries/System.Private.CoreLib/src/System/Number.Parsing.cs#L2439-L2498

It would be similar in concept to MathUtilities.Clamp() which just re-implements exactly what .net does in newer versions. Then when we eventually move over, the code can quickly be updated by just using the new method. No need to re-work the code structure or properties.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't know if copying the newer span implementations would be necessary here. The issue only required using the provided span overloads from Net 6/ Net standard 2.1 if available, and fallback to plain string methods if not. There's nothing to fallback unto with the MathUtilities helpers

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, the point was add methods to fallback to in .net standard 2.0. MathUtilities was just an example. Different direction is fine though since it's already merged.

@avaloniaui-team
Copy link
Contributor

You can test this PR using the following package version. 11.0.999-cibuild0024901-beta. (feed url: https://nuget.avaloniaui.net/repository/avalonia-all/index.json) [PRBUILDID]

@maxkatz6 maxkatz6 merged commit a8c56f3 into AvaloniaUI:master Oct 19, 2022
@avaloniaui-team
Copy link
Contributor

You can test this PR using the following package version. 11.0.999-cibuild0025001-beta. (feed url: https://nuget.avaloniaui.net/repository/avalonia-all/index.json) [PRBUILDID]

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

Successfully merging this pull request may close these issues.

4 participants