Skip to content

Alternative versions of methods for Vectors and Matrices #13899

Closed

Description

@EmptyKeys

Would be helpful to have alternative versions of methods with "out" parameter as result. Example:
this

public static Matrix CreateFromQuaternion(Quaternion quaternion) { ... }

and that

public static void CreateFromQuaternion(ref Quaternion quaternion, out Matrix result) { ... }

What do you think guys? Do you want to add that?

Activity

  1. changed the title [-]Alternative version of methods for Vectors and Matrices[/-] [+]Alternative versions of methods for Vectors and Matrices[/+] on Nov 25, 2014
  2. akoeplinger commented on Nov 25, 2014

    @akoeplinger
    Member

    Can you give an example where you think this is helpful? does it simplify a specific scenario?

  3. EmptyKeys commented on Nov 25, 2014

    @EmptyKeys
    Author

    ref and out in this case are better for memory. Important if you are actually making some render engine etc. :)

  4. MPolovyi commented on Nov 25, 2014

    @MPolovyi

    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.

  5. mikedn commented on Nov 27, 2014

    @mikedn
    Contributor

    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.

  6. benaadams commented on Feb 17, 2015

    @benaadams
    Member

    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)

  7. ellismg commented on Mar 24, 2015

    @ellismg
    Contributor

    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.

  8. EmptyKeys commented on Mar 24, 2015

    @EmptyKeys
    Author

    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.

  9. ellismg commented on Mar 25, 2015

    @ellismg
    Contributor

    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?

  10. EmptyKeys commented on Mar 25, 2015

    @EmptyKeys
    Author

    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.

  11. benaadams commented on Mar 25, 2015

    @benaadams
    Member

    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.

  12. mikedn commented on Mar 25, 2015

    @mikedn
    Contributor

    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?

  13. EmptyKeys commented on Mar 25, 2015

    @EmptyKeys
    Author

    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).

  14. axel-habermaier commented on Mar 25, 2015

    @axel-habermaier

    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?

  15. 43 remaining items

  16. mellinoe commented on Nov 28, 2016

    @mellinoe
    Contributor

    @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.

  17. karelz commented on Nov 28, 2016

    @karelz
    Member

    Thanks for explanation!

  18. danmoseley commented on Mar 1, 2017

    @danmoseley
    Contributor

    @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.

  19. mellinoe commented on Mar 1, 2017

    @mellinoe
    Contributor

    We really should not postpone this past 2.0, IMO. I should be able to get to it soon.

  20. mellinoe commented on Apr 12, 2017

    @mellinoe
    Contributor

    Unfortunately, this isn't going to make it into 2.0. Moving this out for now.

  21. richardhallett commented on Apr 12, 2017

    @richardhallett

    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.

  22. eerhardt commented on Dec 11, 2017

    @eerhardt
    Member

    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.

  23. transferred this issue fromdotnet/corefxon Jan 31, 2020
  24. added this to the 2.1.0 milestone on Jan 31, 2020
  25. ghost locked as resolved and limited conversation to collaborators on Jan 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

api-approvedarea-System.Numerics

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions