Skip to content

Improved RuntimeHelpers.ConvertLength codegen #3608

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

Merged
2 commits merged into from
Dec 8, 2020

Conversation

Sergio0694
Copy link
Member

Follow up from #3520

PR Type

What kind of change does this PR introduce?

  • Optimization

What is the current behavior?

The codegen for the second branch in RuntimeHelpers.ConvertLength does a signed division:

https://github.com/windows-toolkit/WindowsCommunityToolkit/blob/9b75c9f910f999834c64bd00e522d7ae464c121b/Microsoft.Toolkit.HighPerformance/Helpers/Internals/RuntimeHelpers.cs#L43-L46

This is not the best for the codegen, as the JIT has to handle the sign in that division, resulting in the following:

; [System.Byte, System.Private.CoreLib],[System.Numerics.Vector4, System.Numerics.Vectors]
ConvertLength[TFrom, TTo](Int32)
    L0000: mov eax, ecx
    L0002: sar eax, 0x1f
    L0005: and eax, 0xf
    L0008: add eax, ecx
    L000a: sar eax, 4
    L000d: ret

What is the new behavior?

Avoided that with a cast to uint, since the length is guaranteed to be a positive value in [0, int.MaxValue] anyway:

; [System.Byte, System.Private.CoreLib],[System.Numerics.Vector4, System.Numerics.Vectors]
    L0000: mov eax, ecx
    L0002: shr eax, 4
    L0005: ret

Perfect! 😄🎉

PR Checklist

Please check if your PR fulfills the following requirements:

  • Tested code with current supported SDKs
  • Pull Request has been submitted to the documentation repository instructions. Link:
  • Sample in sample app has been added / updated (for bug fixes / features)
  • Tests for the changes have been added (for bug fixes / features) (if applicable)
  • Header has been added to all new source files (run build/UpdateHeaders.bat)
  • Contains NO breaking changes

@Sergio0694 Sergio0694 added high-performance 🚂 Issues/PRs for the Microsoft.Toolkit.HighPerformance package optimization ☄ Performance or memory usage improvements .NET Components which are .NET based (non UWP specific) labels Dec 6, 2020
@Sergio0694 Sergio0694 added this to the 7.0 milestone Dec 6, 2020
@ghost ghost added the in progress 🚧 label Dec 6, 2020
@ghost
Copy link

ghost commented Dec 6, 2020

Thanks Sergio0694 for opening a Pull Request! The reviewers will test the PR and highlight if there is any conflict or changes required. If the PR is approved we will proceed to merge the pull request 🙌

@ghost
Copy link

ghost commented Dec 8, 2020

Hello @michael-hawker!

Because this pull request has the auto merge label, I will be glad to assist with helping to merge this pull request once all check-in policies pass.

p.s. you can customize the way I help with merging this pull request, such as holding this pull request until a specific person approves. Simply @mention me (@msftbot) and give me an instruction to get started! Learn more here.

@michael-hawker
Copy link
Member

@RosarioPulella want to take a quick look? 😉

@Sergio0694 always another instruction to remove, eh?

@Sergio0694
Copy link
Member Author

@michael-hawker Ahahah always! Trying to make the 7.0 release of the HighPerformance package as good as I possibly can! 😊

Copy link
Contributor

@Rosuavio Rosuavio left a comment

Choose a reason for hiding this comment

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

Love it! I never knew that the division of uints has so many less instructions than ints

@Rosuavio
Copy link
Contributor

Rosuavio commented Dec 8, 2020

Also I see he in progress label on the PR, is this PR ready to be merged? @Sergio0694

@Sergio0694
Copy link
Member Author

@RosarioPulella Yup, it's ready to go! The bot added that label automatically when I put this PR into the 7.0 milestone 🙂

@ghost ghost merged commit 97fd126 into CommunityToolkit:master Dec 8, 2020
@Sergio0694 Sergio0694 deleted the codegen/convert-length branch December 8, 2020 16:28
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auto merge ⚡ high-performance 🚂 Issues/PRs for the Microsoft.Toolkit.HighPerformance package .NET Components which are .NET based (non UWP specific) optimization ☄ Performance or memory usage improvements
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants