Archived

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

Any reason for not supporting optional values in structures?

Hi,

In our application we have a pervasive need for optional values in structures. Is there any fundamental reason why this is not supported? If not, are there any plans to add this support?

In our case, for the sake of consistency we are using arrays everywhere an optional value is required, even when we could have used the new 3.5 optional feature. Client code would look much better, however, if we could use the new feature everywhere...

Best,

- Cesar

Comments

  • benoit
    benoit Rennes, France
    Hi,

    We decided not to support optionals with struct to not add overhead to the struct encoding. Structs are often used for small types and we felt that adding the additional one byte necessary to support optionals could be a problem for applications that rely on structs being small.

    Couldn't you just use classes instead of structs? The encoding of classes is quite compact now with the 1.1 encoding.

    Cheers,
    Benoit.
  • Thanks Benoit,

    We tried classes, but the performance penalty is not acceptable for our application.

    Best,

    - Cesar
  • benoit wrote: »
    Hi,

    We decided not to support optionals with struct to not add overhead to the struct encoding. Structs are often used for small types and we felt that adding the additional one byte necessary to support optionals could be a problem for applications that rely on structs being small.

    Couldn't you just use classes instead of structs? The encoding of classes is quite compact now with the 1.1 encoding.

    Cheers,
    Benoit.

    Hi,

    One more observation on the performance: one extra byte for structures (at least for our application) would be much less expensive than using classes. Would the additional byte be necessary for all structures, irrespective on whether they have an optional value or not?

    Best,

    - Cesar
  • benoit
    benoit Rennes, France
    Hi Cesar,

    Can you explain a little why classes are expensive in your application? Is this because of the overhead on the wire? Or is this because of the overhead related to the mapping of classes? Which language mapping do you use btw?

    Unless we introduce a new struct type, the additional byte would be required for all the struct types (you can't know whether or not the struct will have optionals in the future so you always have to assume it might).

    Cheers,
    Benoit.
  • benoit wrote: »
    Hi Cesar,

    Can you explain a little why classes are expensive in your application? Is this because of the overhead on the wire? Or is this because of the overhead related to the mapping of classes? Which language mapping do you use btw?

    Unless we introduce a new struct type, the additional byte would be required for all the struct types (you can't know whether or not the struct will have optionals in the future so you always have to assume it might).

    Cheers,
    Benoit.

    Hi Benoit,

    We use structures for static representation of objects on the client side. A "language-friendly serialization" so to speak. Some of our objects have a dozen or more properties, and some of them are optional. Usually we have thousands of these objects.

    There are various scenarios where the user wants to access this structure representation for a large percentage of the objects. Most of the scenarios are very performance sensitive, and when we compared classes versus structures, we found a performance penalty of between 40-100% when using classes. This means we have to keep using structures (and the "array solution" to optionals, which does look rather kludgy)...

    Best,

    - Cesar