Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Update to Faster math conversions - #8392

#8392
Open
Doprez wants to merge 5 commits into
MonoGame:developMonoGame/MonoGame:developfrom
Doprez:fast-math-conversionsDoprez/MonoGame:fast-math-conversionsCopy head branch name to clipboard
Open

Update to Faster math conversions#8392
Doprez wants to merge 5 commits into
MonoGame:developMonoGame/MonoGame:developfrom
Doprez:fast-math-conversionsDoprez/MonoGame:fast-math-conversionsCopy head branch name to clipboard

Conversation

@Doprez

@Doprez Doprez commented Jun 18, 2024

Copy link
Copy Markdown

I was looking through Mono to see if you all were having the same problems of the existing System.Numerics library built into .NET or if you were still using your own math classes.

I noticed the conversion methods were creating new values instead of using the Unsafe class which has been very helpful for our Bepu to Stride conversions that were a bottleneck before finding this option.

This PR changes the built in conversions to use Unsafe.As to convert instead of creating a new value.

The one thing I dont understand is why the implicit conversions for Matrix are as fast as the Unsafe.As but the implicit for Vector3 and Quaternion are what I would expect from using the new keyword.

This is the example where I forced Matrix to use the new keyword the rest are using the existing implicit conversion:
image

And this is the example of using the Matrix implcit conversion:
image

here is the gist files of the benchmarks I ran https://gist.github.com/Doprez/04d61e03915a9527a482cddbaa826544

Contributor Declaration

  • I certify that no LLM's were used to generate any code or documentation in this contribution.

/// <summary>
/// Returns a <see cref="Matrix"/>.
/// </summary>
public static Matrix ToMonoGame(this System.Numerics.Matrix4x4 matrix)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Wondering if there needs to be copious doc in Matrix/Vector* structs etc to always mirror Numerics layout? These are not things that will ever change but mostly as a way to ensure across platforms/byte-order etc it should hold true. (Tests?)

Also, it's kind of implicit but ref <struct> doesn't cause an alloc (cool) but may need a doc comment explaining the gymnastics happening here to help the compiler use the Numerics without any allocs.. i.e. this

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

are you talking about docs to inform developers using the xml comments? or are you talking about the layout attribute to ensure the struct matches the numerics attribute explicitly? I may be missing something so please let me know but both are row major matrices so I believe the memory layout should match. Tests would be a great idea though, just to be sure.

Instead of ToMonoGame(this System.Numerics.Matrix4x4 matrix) it should be ToMonoGame(this ref System.Numerics.Matrix4x4 matrix) to ensure the clarity of whats expected right? I can add a better description to the xml doc as well if it feels needed.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

are you talking about docs to inform developers using the xml comments? or are you talking about the layout attribute to ensure the struct matches the numerics attribute explicitly? I may be missing something so please let me know but both are row major matrices so I believe the memory layout should match. Tests would be a great idea though, just to be sure.

I meant both: xml comment and a layout attribute. especially people may not be aware that there is link between the two. (note: am not a maintainer, so this is mostly a drive-by :) )

Instead of ToMonoGame(this System.Numerics.Matrix4x4 matrix) it should be ToMonoGame(this ref System.Numerics.Matrix4x4 matrix) to ensure the clarity of whats expected right? I can add a better description to the xml doc as well if it feels needed.

Here, I meant that you are also avoiding extra allocs (because of ref <struct>) which is kinda nice - but to me it was not obvious at first glance and hence I had to double check dotnet docs :) simply adding that to the xml comment will be nice...

@ThomasFOG

Copy link
Copy Markdown
Contributor

This will need to wait for the console runtime to be fully switched to NativeAOT and will also need to be tested on consoles, I have some doubts about System.Runtime.CompilerServices in that context.

@SimonDarksideJ SimonDarksideJ added this to the 3.9.0 milestone Jan 26, 2026
@AristurtleDev AristurtleDev added enhancement An improvement to an existing feature. scope: performance Profiling, allocations, throughput, memory pressure, and performance improvements. scope: math Numerical correctness, transforms, geometry, interpolation, and math helpers. status: blocked Cannot proceed until something external changes or a decision is made. labels Jul 10, 2026
@AristurtleDev

Copy link
Copy Markdown
Contributor

This will need to wait for the console runtime to be fully switched to NativeAOT and will also need to be tested on consoles, I have some doubts about System.Runtime.CompilerServices in that context.

@tomspilman is this still a blocking concern noted by @ThomasFOG ?

@AristurtleDev

Copy link
Copy Markdown
Contributor

@Doprez we would need you to update your initial PR description to include the new statement regarding our LLM policy as shown in the updated PR templates here: https://github.com/MonoGame/MonoGame/blob/develop/.github/PULL_REQUEST_TEMPLATE.md

Just need to add the following and check it off at the bottom of your PR description

### Contributor Declaration

- [ ] I certify that no LLM's were used to generate any code or documentation in this contribution.

@Doprez

Doprez commented Jul 10, 2026

Copy link
Copy Markdown
Author

It has been done, I will come back to this later and fix the conflicts as well when I have a bit more time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement An improvement to an existing feature. scope: math Numerical correctness, transforms, geometry, interpolation, and math helpers. scope: performance Profiling, allocations, throughput, memory pressure, and performance improvements. status: blocked Cannot proceed until something external changes or a decision is made.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants

Morty Proxy This is a proxified and sanitized view of the page, visit original site.