When are structs the answer?

前端 未结 12 798
广开言路
广开言路 2020-12-12 18:24

I\'m doing a raytracer hobby project, and originally I was using structs for my Vector and Ray objects, and I thought a raytracer was the perfect situation to use them: you

相关标签:
12条回答
  • 2020-12-12 18:39

    I use structs basically for parameter objects, returning multiple pieces of information from a function, and... nothing else. Don't know whether it's "right" or "wrong," but that's what I do.

    0 讨论(0)
  • 2020-12-12 18:39

    You can also make structs into Nullable objects. Custom classes will not be able to created

    as

    Nullable<MyCustomClass> xxx = new Nullable<MyCustomClass>
    

    where with a struct is nullable

    Nullable<MyCustomStruct> xxx = new Nullable<MyCustomStruct>
    

    But you will be (obviously) losing all your inheritance features

    0 讨论(0)
  • 2020-12-12 18:44

    The first thing I would look for is to make sure that you have explicitly implemented Equals and GetHashCode. Failing to do this means that the runtime implementation of each of these does some very expensive operations to compare two struct instances (internally it uses reflection to determine each of the private fields and then checkes them for equality, this causes a significant amount of allocation).

    Generally though, the best thing you can do is to run your code under a profiler and see where the slow parts are. It can be an eye-opening experience.

    0 讨论(0)
  • 2020-12-12 18:49

    An array of structs would be a single contiguous structure in memory, while items in an array of objects (instances of reference types) need to be individually addressed by a pointer (i.e. a reference to an object on the garbage-collected heap). Therefore if you address large collections of items at once, structs will give you a performance gain since they need fewer indirections. In addition, structs cannot be inherited, which might allow the compiler to make additional optimizations (but that is just a possibility and depends on the compiler).

    However, structs have quite different assignment semantics and also cannot be inherited. Therefore I would usually avoid structs except for the given performance reasons when needed.


    struct

    An array of values v encoded by a struct (value type) looks like this in memory:

    vvvv

    class

    An array of values v encoded by a class (reference type) look like this:

    pppp

    ..v..v...v.v..

    where p are the this pointers, or references, which point to the actual values v on the heap. The dots indicate other objects that may be interspersed on the heap. In the case of reference types you need to reference v via the corresponding p, in the case of value types you can get the value directly via its offset in the array.

    0 讨论(0)
  • 2020-12-12 18:49

    Have you profiled the application? Profiling is the only sure fire way to see where the actual performance problem is. There are operations that are generally better/worse on structs but unless you profile you'd just be guessing as to what the problem is.

    0 讨论(0)
  • 2020-12-12 18:52

    Basically, don't make them too big, and pass them around by ref when you can. I discovered this the exact same way... By changing my Vector and Ray classes to structs.

    With more memory being passed around, it's bound to cause cache thrashing.

    0 讨论(0)
提交回复
热议问题