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

[Safe code] Clean up Guid.TryFormatX #110923

Merged
merged 4 commits into from
Dec 27, 2024
Merged

[Safe code] Clean up Guid.TryFormatX #110923

merged 4 commits into from
Dec 27, 2024

Conversation

EgorBo
Copy link
Member

@EgorBo EgorBo commented Dec 24, 2024

Contributes to #94941

This PR removes unsafe code from Guid.TryFormatX + small clean up in TryParseExact.

New version is 10-15% faster.

@dotnet-issue-labeler dotnet-issue-labeler bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Dec 24, 2024
@EgorBo

This comment was marked as resolved.

@EgorBo
Copy link
Member Author

EgorBo commented Dec 24, 2024

@EgorBot -amd -arm -profiler

using System.Runtime.CompilerServices;
using BenchmarkDotNet.Attributes;

public class Benchmark
{
    static Guid guid = Guid.Parse("{D420C46B-04DC-4ABD-BF32-5132E82EE7F7}");

    [Benchmark]
    public bool FormatX()
    {
        Span<char> buffer = stackalloc char[100];
        bool result = guid.TryFormat(buffer, out int charsWritten, "X");
        Consume(buffer.Slice(0, charsWritten));
        return result;
    }

    [MethodImpl(MethodImplOptions.NoInlining)]
    static void Consume(Span<char> _){}
}

@jkotas jkotas added area-System.Runtime and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels Dec 24, 2024
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-runtime
See info in area-owners.md if you want to be subscribed.

@EgorBo

This comment was marked as resolved.

Copy link
Member

@MihaZupan MihaZupan left a comment

Choose a reason for hiding this comment

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

New version is 10-15% faster.

I'm guessing that's from HexsToCharsHexOutput not being inlined before?

@EgorBo
Copy link
Member Author

EgorBo commented Dec 26, 2024

I'm guessing that's from HexsToCharsHexOutput not being inlined before?

Yep, but the goal was to remove the unsafe code & maintain same perf as before 🙂

success = TryParseExactX(input, ref parseResult);
break;
}
bool success = (char)(format[0] | 0x20) switch
Copy link
Member

Choose a reason for hiding this comment

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

Is the cast to char actually necessary here? It should work without it and that would make smaller IL and avoid an extra zero extension that the JIT has to otherwise remove here

Copy link
Member Author

Choose a reason for hiding this comment

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

@tannergooding hm. I've removed the cast but the codegen hasn't changed. But I guess it is still makes sense to remove it as useless

Copy link
Member

Choose a reason for hiding this comment

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

I'd guess the JIT is recognizing it as unnecessary and removing it already; just something small that caught my eye as likely unneeded

@EgorBo
Copy link
Member Author

EgorBo commented Dec 27, 2024

/ba-g "browser-wasm linux Release LibraryTests is stuck"

@EgorBo EgorBo merged commit 2eb59bf into dotnet:main Dec 27, 2024
137 of 139 checks passed
@EgorBo EgorBo deleted the guid-cleanup branch December 27, 2024 20:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants