You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Alternative versions of methods for Vectors and Matrices #13899
changed the title [-]Alternative version of methods for Vectors and Matrices[/-][+]Alternative versions of methods for Vectors and Matrices[/+]on Nov 25, 2014
Also, in ref-out scenario, you can reuse already created and allocated matrix. In case you need a lot of quaternion-matrix conversions, It can help you save some miliseconds.
Hovever, with such code as in Matrix.CreateFromQuaternion(...), I don't think memory allocation must be the point of concern in application heavily using this method.
ref/out avoid copying the value, for large value types such as Matrix that can be quite an improvement. Not only that you don't pay the cost of the copying but the generated code is much smaller because of that.
That said, using ref/out is kind of ugly and in some cases it simply doesn't work (you can't use ref/out with properties for example). Having to extend the API with such overloads just because the JIT compiler can't handle this case properly is a rather unfortunate situation.
The example of CreateFromQuaternion shouldn't allocate any memory as its entirely stack based?
You would pay a 64 byte copy cost if you were storing the return value rather than just using it.
Reusing a local declared vary by ref shouldn't gain much and I think things get weird when you start passing addresses of local stack variables around if not careful. (If you are storing the return its probably not hotpath as you are already caching)
Is the proposal here to do this for all methods on these types or a subset? It would be helpful to understand what exactly is in scope and have some sample code fragments that exhibit bad codegen today so we can compare the present codegen to the codegen we would get if we made changes.
pretty much what @mikedn said. If you want better performance ref/out methods are good candidate and should be implemented, where it makes sense ie big struct types like Matrix/Quaternion etc.
For cases like public static Matrix CreateFromQuaternion(Quaternion quaternion), I can imagine how passing the argument by ref could provide different codegen that is better in some cases (since you don't have the copy the data into the callee).
However, is the rewrite of the return value to an out parameter really that helpful? I would have sort of assumed there would be some return value optimization going on.
For me, it would be helpful to see some representative samples where the codegen is unacceptable so we can understand the best way to address the issue. Is it via additional APIs? Is there something we can do to make the methods more likely to be inlined and hence data need not be copied, are there other optimizations the JIT could be doing?
I worry about creating a bunch of new APIs to address problems we don't have good samples for. How can we know if we are doing better?
Almost 9 years old thread. XNA managed to get that API correct on the end. Something changed since then in compiler for this issue?
If you can change something somewhere, which will not copy bunch of floats there and back, then we don't need this change in API. What ever solution you find is good. And if you want some good samples just try your examples for this library with ref/out (or any solution you pick) implementation and compare difference.
I don't know what the jitter does but a byval Quaternion can be a single cpu register and if so, a byref would give you worse performance as it could be referenced in multiple places on multiple threads so always needs to memory backed rather than register backed.
However, is the rewrite of the return value to an out parameter really that helpful? I would have sort of assumed there would be some return value optimization going on.
Good question, struct returns are already converted to out returns by the JIT compiler since there's no other (sane) way of returning a struct that doesn't fit in a register. And it turns out that the JIT compiler does an optimization and eliminates one of 2 copies that are normally required. For code like field = CreateFromQuaternion(...) the JIT compiler generates code that looks identical to CreateFromQuaternion(..., out field) instead of passing a pointer to a temporary and then copying the temporary to the actual destination.
The remaining issue is a copy that's performed inside CreateFromQuaternion. The matrix is normally computed in a local variable and at the end of the method the local variable is copied to the out parameter. It's difficult to avoid this copy, attempting to compute the matrix straight into the out parameter would result in a corrupted return value if an exception gets thrown during computation.
I can imagine how passing the argument by ref could provide different codegen that is better in some cases (since you don't have the copy the data into the callee).
This is something where the JIT compiler might be able to do better. I think it could always pass structs by ref and have the callee perform the copy when necessary to preserve the value semantics. In some cases the copy would simply transform into loading the necessary struct fields in registers.
I don't know what the jitter does but a byval Quaternion can be a single cpu register
Yes, it could be but it appears that currently it isn't. It's also debatable if passing a quaternion in a register actually helps. If the callee needs to call Quaternion methods that cannot be inlined then the compiler will have to spill the register to memory anyway so it can take the address for this.
XNA managed to get that API correct on the end.
That's debatable. Using ref and out for performance reasons is a workaround rather than "correct". Not to say that this shouldn't be done but don't be surprised if people will discuss this over and over again.
The way I see it: I do care about performance but I also want the code to be readable. What can be done so that one can use operator overloads (think matrix multiplication for example) and also get good performance?
well compiler could check if struct is big (ie Matrix) and do that optimization (ref) for us, but not sure if there is any issue with this kind of behavior.
Yeah, you are right. Ref/out solution was always workaround (even for XNA), but I thought that changing API (in one small library) would be easier than changing how compiler works (for everyone).
Could the (relatively new) AggressiveInlining flag be used to force the jitter to inline those methods? If SIMD code is emitted, it shouldn't bloat the generated code too much, should it?
@karelz Yes, it did, and I have a branch with the changes. I need to set aside some time to finish it up and push it through a code review. We should do this for .NET Core 1.2.
Just to share my interest in this topic, it is a shame it's moving to 2.1, I'm also one of these people who has written a .net maths library written using by ref, for performance reasons.
However I also not sure where to bring this up but the issue of performance as well as the API Design is important on this issue but the topic crops up in numerous issues across the CLR and CoreFX.
How we go about getting efficient SIMD instructions for graphics/games/whatever might come down to exposing intrinsics themselves or for library writers to wrap System.Numerics.Vectors, though as mentioned by others as long as there is no cost involved.
If i'm missing some discussion somewhere then I apologise or for incorrect posting but given I tracked down a variety of issues which I feel really is all related to the same thing of having high performance vector math, I wanted to make sure others could see the discussions and any API design changes were considered in the broader scope.
In regards to this issue explicitly, if we assume the API will still expose matrix/quat and such, it's probably still an improvement to add the ref overloads, given this doesn't affect any JIT stuff is this a case of adding in the overloads because I can probably add a PR if desired? as it's very similar to what I'm doing already in my own lib.
Activity
[-]Alternative version of methods for Vectors and Matrices[/-][+]Alternative versions of methods for Vectors and Matrices[/+]akoeplinger commentedon Nov 25, 2014
Can you give an example where you think this is helpful? does it simplify a specific scenario?
EmptyKeys commentedon Nov 25, 2014
ref and out in this case are better for memory. Important if you are actually making some render engine etc. :)
MPolovyi commentedon Nov 25, 2014
Also, in ref-out scenario, you can reuse already created and allocated matrix. In case you need a lot of quaternion-matrix conversions, It can help you save some miliseconds.
Hovever, with such code as in Matrix.CreateFromQuaternion(...), I don't think memory allocation must be the point of concern in application heavily using this method.
mikedn commentedon Nov 27, 2014
ref/out avoid copying the value, for large value types such as Matrix that can be quite an improvement. Not only that you don't pay the cost of the copying but the generated code is much smaller because of that.
That said, using ref/out is kind of ugly and in some cases it simply doesn't work (you can't use ref/out with properties for example). Having to extend the API with such overloads just because the JIT compiler can't handle this case properly is a rather unfortunate situation.
benaadams commentedon Feb 17, 2015
The example of CreateFromQuaternion shouldn't allocate any memory as its entirely stack based?
You would pay a 64 byte copy cost if you were storing the return value rather than just using it.
Reusing a local declared vary by ref shouldn't gain much and I think things get weird when you start passing addresses of local stack variables around if not careful. (If you are storing the return its probably not hotpath as you are already caching)
ellismg commentedon Mar 24, 2015
Is the proposal here to do this for all methods on these types or a subset? It would be helpful to understand what exactly is in scope and have some sample code fragments that exhibit bad codegen today so we can compare the present codegen to the codegen we would get if we made changes.
EmptyKeys commentedon Mar 24, 2015
pretty much what @mikedn said. If you want better performance ref/out methods are good candidate and should be implemented, where it makes sense ie big struct types like Matrix/Quaternion etc.
ellismg commentedon Mar 25, 2015
For cases like
public static Matrix CreateFromQuaternion(Quaternion quaternion), I can imagine how passing the argument by ref could provide different codegen that is better in some cases (since you don't have the copy the data into the callee).However, is the rewrite of the return value to an out parameter really that helpful? I would have sort of assumed there would be some return value optimization going on.
For me, it would be helpful to see some representative samples where the codegen is unacceptable so we can understand the best way to address the issue. Is it via additional APIs? Is there something we can do to make the methods more likely to be inlined and hence data need not be copied, are there other optimizations the JIT could be doing?
I worry about creating a bunch of new APIs to address problems we don't have good samples for. How can we know if we are doing better?
EmptyKeys commentedon Mar 25, 2015
this thread and posts are like Déjà vu, it's almost surreal.
https://social.msdn.microsoft.com/Forums/en-US/e715c16b-6b02-454a-b836-d402093bd36f/end-users-shouldnt-have-to-rewrite-xna-math-functions-in-order-to-improve-performance?forum=xnaframework
Almost 9 years old thread. XNA managed to get that API correct on the end. Something changed since then in compiler for this issue?
If you can change something somewhere, which will not copy bunch of floats there and back, then we don't need this change in API. What ever solution you find is good. And if you want some good samples just try your examples for this library with ref/out (or any solution you pick) implementation and compare difference.
benaadams commentedon Mar 25, 2015
I don't know what the jitter does but a byval Quaternion can be a single cpu register and if so, a byref would give you worse performance as it could be referenced in multiple places on multiple threads so always needs to memory backed rather than register backed.
mikedn commentedon Mar 25, 2015
Good question, struct returns are already converted to
outreturns by the JIT compiler since there's no other (sane) way of returning a struct that doesn't fit in a register. And it turns out that the JIT compiler does an optimization and eliminates one of 2 copies that are normally required. For code likefield = CreateFromQuaternion(...)the JIT compiler generates code that looks identical toCreateFromQuaternion(..., out field)instead of passing a pointer to a temporary and then copying the temporary to the actual destination.The remaining issue is a copy that's performed inside CreateFromQuaternion. The matrix is normally computed in a local variable and at the end of the method the local variable is copied to the out parameter. It's difficult to avoid this copy, attempting to compute the matrix straight into the out parameter would result in a corrupted return value if an exception gets thrown during computation.
This is something where the JIT compiler might be able to do better. I think it could always pass structs by ref and have the callee perform the copy when necessary to preserve the value semantics. In some cases the copy would simply transform into loading the necessary struct fields in registers.
Yes, it could be but it appears that currently it isn't. It's also debatable if passing a quaternion in a register actually helps. If the callee needs to call Quaternion methods that cannot be inlined then the compiler will have to spill the register to memory anyway so it can take the address for
this.That's debatable. Using
refandoutfor performance reasons is a workaround rather than "correct". Not to say that this shouldn't be done but don't be surprised if people will discuss this over and over again.The way I see it: I do care about performance but I also want the code to be readable. What can be done so that one can use operator overloads (think matrix multiplication for example) and also get good performance?
EmptyKeys commentedon Mar 25, 2015
well compiler could check if struct is big (ie Matrix) and do that optimization (ref) for us, but not sure if there is any issue with this kind of behavior.
Yeah, you are right. Ref/out solution was always workaround (even for XNA), but I thought that changing API (in one small library) would be easier than changing how compiler works (for everyone).
axel-habermaier commentedon Mar 25, 2015
Could the (relatively new)
AggressiveInliningflag be used to force the jitter to inline those methods? If SIMD code is emitted, it shouldn't bloat the generated code too much, should it?43 remaining items
mellinoe commentedon Nov 28, 2016
@karelz Yes, it did, and I have a branch with the changes. I need to set aside some time to finish it up and push it through a code review. We should do this for .NET Core 1.2.
karelz commentedon Nov 28, 2016
Thanks for explanation!
danmoseley commentedon Mar 1, 2017
@mellinoe is this still necessary for 2.0? if so is it still feasible for 2.0 with your other commitments?
if no or no please set to future.
mellinoe commentedon Mar 1, 2017
We really should not postpone this past 2.0, IMO. I should be able to get to it soon.
mellinoe commentedon Apr 12, 2017
Unfortunately, this isn't going to make it into 2.0. Moving this out for now.
richardhallett commentedon Apr 12, 2017
Just to share my interest in this topic, it is a shame it's moving to 2.1, I'm also one of these people who has written a .net maths library written using by ref, for performance reasons.
However I also not sure where to bring this up but the issue of performance as well as the API Design is important on this issue but the topic crops up in numerous issues across the CLR and CoreFX.
How we go about getting efficient SIMD instructions for graphics/games/whatever might come down to exposing intrinsics themselves or for library writers to wrap System.Numerics.Vectors, though as mentioned by others as long as there is no cost involved.
For reference this has been brought up in https://github.com/dotnet/corefx/issues/17214 and https://github.com/dotnet/coreclr/issues/6906 although the latter is more generally exposing intrinsics.
https://github.com/dotnet/coreclr/issues/4356 talks about the issue raised above regarding Matrix/Quaternions not using SIMD intrinsics, but the last comment is interesting in terms of the API design which might influence what is done here.
If i'm missing some discussion somewhere then I apologise or for incorrect posting but given I tracked down a variety of issues which I feel really is all related to the same thing of having high performance vector math, I wanted to make sure others could see the discussions and any API design changes were considered in the broader scope.
In regards to this issue explicitly, if we assume the API will still expose matrix/quat and such, it's probably still an improvement to add the ref overloads, given this doesn't affect any JIT stuff is this a case of adding in the overloads because I can probably add a PR if desired? as it's very similar to what I'm doing already in my own lib.
Merge pull request #157 from mairaw/patch-3
eerhardt commentedon Dec 11, 2017
This change was rejected in PR. See dotnet/corefx#25388 and its discussion for reasoning.
Instead, the plan is to fix the cases where the JIT should generate better code using the existing by value methods.
For example, the first case identified is being tracked by: https://github.com/dotnet/coreclr/issues/15467.
Closing this issue since the change was not approved.
Merge pull request #157 from mono/corefx-System.IO-trivial-types