If my understanding is correct, this performance difference is because myT2 can directly store a pointer to .b along with .a to form a better memory layout. My question is, can the compiler automatically convert the lower-layer implementation of myT to myT2 in the future?
You didnβt define == for the new types, though. The default == method just forwards to ===:
julia> struct S end
julia> @which S() == S()
==(x, y)
@ Base Base.jl:207
So it comes down to === being cheaper for the mutable struct, because itβs implemented as a simple integer comparison, unlike for the immutable struct, where the === is recursive. This is documented, the doc string of === starts with:
Determine whether x and y are identical, in the sense that no program could distinguish them. First the types of x and y are compared. If those are identical, mutable objects are compared by address in memory and immutable objects (such as numbers) are compared by contents at the bit level.
Regarding the unfortunate default method of ==, here are some Github issues:
The behavior is documented, though, the == doc string starts with: