C# makes an distinction between value types and reference types contrast to Java where all (except primitives types) are of reference semantics. The design decision, as I understand it, is mainly to make small objects more cache friendly, that is, for efficiency reasons.

my question is;

  1. is there any study on how much efficiency gain does C# make by this design choice, based on evaluating real world projects.

  2. Assuming the answer to the first question is that there is a measurable efficiency gap, will things change when JIT compilers come into play?

  • 1
    $\begingroup$ The main performance benefit, iirc, is to keep short-lived objects on the stack so garbage collection is trivial. $\endgroup$ – Raphael Apr 14 '19 at 9:41
  • $\begingroup$ @Raphael Any quantitative results on the alleged performance improvement? $\endgroup$ – John Z. Li Apr 14 '19 at 9:45

Swift does the same thing (having both value types and reference types). I'm sure there are efficiency studies, performed by the people implementing the Swift compiler.

But the difference between reference types and value types goes a lot deeper. What you need to look at is things like mutability, semantics when passing parameters, and so on. I don't choose reference vs. value types for micro efficiencies, but for the semantics.

So your assumption "the design decision is mainly to make small objects more cache friendly", is firstly wrong, and secondly I strongly doubt it makes things more cache friendly.

  • $\begingroup$ I am not sure that the value type is meant to enforce /imply value semantics. I might be wrong. $\endgroup$ – John Z. Li Apr 14 '19 at 9:39
  • 1
    $\begingroup$ @JohnZ.Li I don't think the two are really separable. If you keep values on the stack, you (kind of) have to copy them when passing as parameter, which means you (kind of) get immutability. That is to say, any form of sane mutability (across function calls) would be really awkward to implement using stack-based values. $\endgroup$ – Raphael Apr 14 '19 at 13:23
  • $\begingroup$ And on the other hand, if a function is passed a parameter of some reference type, the value could be aliased and modified unwittingly, or could be modified by another thread behind your back. $\endgroup$ – gnasher729 Apr 15 '19 at 21:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.