Polymorphic Type Instance Memory Overhead


#1

How do I measure the memory overhead of an instance of a polymorphic type in Julia?

I am particularly interested in

  • union types
  • sub-types of Any

Further, does Julia have some memory-usage optimization tricks for the descriminator in union-types? For instance, if all types in the union are of similar size and share a set of bit-patterns that are all unused by all the types, these bit-patterns can be used as a descriminator.

An example would be a Union{} of different kinds of pointers all with alignment of 16, which in this case, would leave the 3 least significant bits unused. Assuming the GC is aware of this.

BTW: Is there any documentation on the internals of the current GC in Julia?


#2

I thought instances were always concrete.


#3

Ok, I meant an instance of a type that inherits (implements) Any.

But it turns out that

struct X <: Any end
sizeof(X)

returns 0 because Julia’s type model is not what I thought it was. Is union types or composition of inheritance the way to express traditional object hierarchies in Julia?

Julia’s type system is new to me. I’m used to thinking in C++, Rust, Python and D’s type systems. Is Julia’s anywhere similar to Rust’s multi-trait-system?


#4

I don’t know what a “traditional object hierarchy” is. Julia has single-ancestor subtypes, where the leafs are concrete and the rest is abstract types. These are mostly used for governing dispatch. Composition is recommended for what some OO languages solve with inheritance. See



and similar topics.

Also, I would not worry about representation in memory etc, other than what is described in the

https://docs.julialang.org/en/stable/manual/performance-tips/


#5

Adding on to that, if more details are wanted:

https://docs.julialang.org/en/stable/devdocs/types/

https://docs.julialang.org/en/stable/devdocs/object/

But this really shouldn’t be needed during normal development.