I’m seeing some behavior with accessing data members of structs that I don’t understand - depending on the value of typed field in a struct, I’m getting an allocation with getfield
.
struct MyStruct
v::Int
end
ms0 = MyStruct(0)
ms512 = MyStruct(512)
@btime getfield(ms0, :v)
15.415 ns (0 allocations: 0 bytes)
@btime getfield(ms512, :v)
20.361 ns (1 allocation: 16 bytes)
It seems that for values less than 512, there is no allocation. I also do not see any allocations for any size number if I instead define the field v
without a type. Is there a reason for this?
Also, why does accessing the member directly with .
cause more allocations and why is it so much slower?
@btime ms0.v
43.088 ns (1 allocation: 16 bytes)
@btime ms512.v
51.874 ns (3 allocations: 48 bytes)