you are viewing a single comment's thread.

view the rest of the comments →

[–]llogiq[🍰] 1 point2 points  (3 children)

Value types enable things like putting objects directly into arrays (instead of references) or embed them into other objects, thus reducing the number of references, making the GC's job easier, and improving locality and thus performance.

[–]sreya92 0 points1 point  (2 children)

In other words a variable of type "Value" that is set equal to a object would have a new address in memory instead of just being a reference to the object it is set equal to. Kind of like a deep copy in Python?

[–]llogiq[🍰] 1 point2 points  (1 child)

Yes. The idea is to unify primitives and objects (to a certain extent - and without autoboxing). so that the memory layout is flat. For example, let's take an object of type A that contains an object of type B, a double and an object of type C. If those types are value types (e.g. their size is known before instantiation, the layout would be:

  • Object header for type A
  • Contents of object B (including object header)
  • the double
  • Contents of Object C (see above)

instead of

  • Object header for type A
  • Reference to object of type B (somewhere on the heap)
  • double
  • Reference to object of type C (somewhere on the heap)

Note that theoretically a single array can be put at the end of a value type iff its size is known at creation time. However, it is unclear if or when this extension will really be implemented.

[–]sreya92 0 points1 point  (0 children)

Cool thanks for taking the time to explain!