Archived

This forum has been archived. Please start a new discussion on GitHub.

Mapping Slice structs to C# structs

kwaclaw
kwaclaw Oshawa, Canada
Currently one cannot force the code generator to map a Slice struct to a C# struct if the Slice struct contains reference types. Why is that?

Karl

Comments

  • The main reason is that, if a structure contains a Slice class member and that class member is mapped to C# class, there is no way to unmarshal the struct corrrectly. This is because of the way Slice classes are marshaled. (See Section 39.2 in the manual.) There is simply no way to instantiate a C# struct and then, when the value of the class instance contained by the struct arrives, patch the corresponding data member in the struct.

    Also, pragmatically, as soon as a C# struct contains a reference type, there is no point in using a struct any longer--any minor performance advantage provided by using a struct effectively disappears, so you might as well use a class.

    Cheers,

    Michi.
  • kwaclaw
    kwaclaw Oshawa, Canada
    michi wrote: »
    The main reason is that, if a structure contains a Slice class member and that class member is mapped to C# class, there is no way to unmarshal the struct corrrectly. This is because of the way Slice classes are marshaled. (See Section 39.2 in the manual.) There is simply no way to instantiate a C# struct and then, when the value of the class instance contained by the struct arrives, patch the corresponding data member in the struct.

    I guess that answers my question. :-)
    michi wrote: »
    Also, pragmatically, as soon as a C# struct contains a reference type, there is no point in using a struct any longer--any minor performance advantage provided by using a struct effectively disappears, so you might as well use a class.

    Let's say I need to allocate a large array of structs, which can be done using a contiguous block of memory. Now, even if the structs contain references to some (possibly pre-existing) reference types, why would that negate the performance advantage of allocating one large chunk of memory vs instantiating a large number of individual objects?

    Karl
  • kwaclaw wrote: »
    Let's say I need to allocate a large array of structs, which can be done using a contiguous block of memory. Now, even if the structs contain references to some (possibly pre-existing) reference types, why would that negate the performance advantage of allocating one large chunk of memory vs instantiating a large number of individual objects?

    It won't negate the performance advantage for allocating the array. However, if each struct contains a reference member, that performance advantage is likely to drown among all the allocations for those reference members.

    In addition, when we unmarshal such an array, we then have to copy all the structs into the array by value which, unless the structs are small and simple, is more expensive than instantiating lots of classes and assigning their references. So, there really is little point in having a struct with a reference member because, in practice, it doesn't provide any advantage over mapping to a class.

    In general, C# structs should be used for small collections of data with value semantics, such as an xy coordinate or a complex number.

    Cheers,

    Michi.
  • kwaclaw
    kwaclaw Oshawa, Canada
    michi wrote: »
    It won't negate the performance advantage for allocating the array. However, if each struct contains a reference member, that performance advantage is likely to drown among all the allocations for those reference members.

    Yes, I can see that.

    Thanks for the explanation,

    Karl