A macro does not change the fact that you’d allocate memory.
Don’t create a vector, use a generator. Which is what I did in the macro above.
julia> call_on_fields(f, s) = f(getfield(s, field) for field in fieldnames(typeof(s)))
call_on_fields (generic function with 1 method)
julia> call_on_fields(sum, mystruct(1, 2))
3
julia> @benchmark call_on_fields(sum, mystruct(1, 2))
BenchmarkTools.Trial: 10000 samples with 1000 evaluations.
Range (min … max): 1.384 ns … 12.575 ns ┊ GC (min … max): 0.00% … 0.00%
Time (median): 1.432 ns ┊ GC (median): 0.00%
Time (mean ± σ): 1.454 ns ± 0.437 ns ┊ GC (mean ± σ): 0.00% ± 0.00%
▃ ▄ █▆ ▁ ▂ ▂ ▁
█▁█▄▁▁▁▁▁██▅▁▁▁▁█▁██▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▃█ █
1.38 ns Histogram: log(frequency) by time 1.66 ns <
Memory estimate: 0 bytes, allocs estimate: 0.
The call_on_fields function above is the plain-function version of the macro in my previous message. Which one do you find more readable? ![]()