none
Non-readonly System.Numerics.Vector2 question RRS feed

  • Question

  • I know that the .NET compiler will generate defensive copies of a readonly field in the case that the underlying struct itself is mutable.

    For example:

    struct MutableStruct {

    public int x;

    public mutate() { x++; }

    }

    readonly struct ImmutableStruct {

    public readonly MutableStruct s;

    public void test() { s.mutate(); } // <-- makes a defensive copy of 's' before calling 'mutate'

    }

    So here's my question. I really want to use the new SIMD types. System.Numerics.Vector2 in particular.

    However, it is a mutable struct. So there, if I embed it into a readonly struct, then every single time it is accessed, the compiler

    will insert defensive copies, correct?

    This seems like a major drawback to using the new Vector2 class...

    Friday, August 23, 2019 8:17 PM

Answers

All replies

  • Hi Elliott Prechter, 

    Thank you for posting here.

    According to your question, I have two questions to confirm with you.

    Why you want to embed System.Numerics.Vector2 into a readonly struct?

    In document Write safe and efficient C# code, it explains that the compiler generates more efficient code when calling members of a readonly struct.

    Do you want to know that whether it is a drawback to System.Numerics.Vector2 because it is not readonly?

    We are waiting for your update.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, August 26, 2019 8:08 AM
    Moderator
  • Why you want to embed System.Numerics.Vector2 into a readonly struct?

    Object composition. I need to define:

    readonly struct Pose2 {

       public readonly Vector2 Position;

       public readonly float Orientation;

    }

    Or in 3D:

    readonly struct Pose3 {

       public readonly Vector3 Position;

       public readonly Quaternion Orientation;

    }

    The non-readonly nature of the System.Numerics structures is, I believe, creating unnecessary copies during field access.

    In document Write safe and efficient C# code, it explains that the compiler generates more efficient code when calling members of a readonly struct.

    Do you want to know that whether it is a drawback to System.Numerics.Vector2 because it is not readonly?

    Yes, exactly. It seems like to get the full performance benefits of System.Numerics types, you would need to make every class or struct that contains a mutable System.Numerics type non-readonly.

    Monday, August 26, 2019 1:26 PM
  • Hi Elliott Prechter, 

    Thanks for your feedback.

    For your question, I make a test on my side:

            readonly struct Pose2
            {
                public readonly Vector2 Position;
                public readonly float Orientation;
            }
            static void Main(string[] args)
            {
                Vector2 v2;
                v2.X = 100;
                Pose2 pose2;
                pose2.Position.X = 100;
                Console.ReadKey();
            }

    I get the following error:

    If I add the readonly modifier to the declaration of the struct, the access will be limited:

    1. All members (properties, fields) must be read-only;
    2. I’ll need to initialize the members in the public parameterized constructor;
    3. ‘this’ parameter will be read-only in other place except in constructor.
    4. You cannot define “field-like” events;

    System.Numerics.Vector2 is not readonly because it allows us to modify the properties in it.

    Hope it can help you.

    Best Regards,

    Xingyu Zhao



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, August 27, 2019 6:50 AM
    Moderator
  • Thanks, though your answer seems to be for a different topic. I'm mainly curious as to why the new vector types are mutable, which limits their ability to efficiently compose into larger immutable structs and classes. Are there any plans to offer immutable versions of these?


    Tuesday, August 27, 2019 6:25 PM
  • Hi Elliott Prechter, 

    I find two related reference, and I hope them can help you.

    1. Immutable Structs
    2. Why are System.Windows.Point & System.Windows.Vector mutable?

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, August 28, 2019 9:25 AM
    Moderator