I write two functions that do the same thing, but one has allocations. All I change is the type from an abstract class to a concrete class.
julia> function baz((;h, t_w, E, F_y)::T) where T <: AISCSteel.Shapes.IShapes.AbstractRolledIShapes
λ = h / t_w
λ_p = 3.76 * sqrt(E / F_y)
λ_r = 5.7 * sqrt(E / F_y)
if λ <= λ_p
class = :compact
elseif λ_p < λ <= λ_r
class = :noncompact
else
class = :slender
end
return λ, λ_p, λ_r, class
end
baz (generic function with 1 method)
julia> @benchmark baz(w)
BenchmarkTools.Trial: 10000 samples with 991 evaluations per sample.
Range (min … max): 42.129 ns … 669.190 ns ┊ GC (min … max): 0.00% … 89.71%
Time (median): 45.114 ns ┊ GC (median): 0.00%
Time (mean ± σ): 45.697 ns ± 19.402 ns ┊ GC (mean ± σ): 1.30% ± 2.85%
▁▁ ▆▄▄▂ ▄█▆▄▂▁▁ ▁ ▁▄▂ ▁ ▂
███▆▁▁▁▃▃▆█████▇▅▃▃▄▄▅███████▇▆▇██▆▆████▇▇▇▅▇█▇▆▅▄▄▃▄▅▅▅▅▇█▆ █
42.1 ns Histogram: log(frequency) by time 49.7 ns <
Memory estimate: 48 bytes, allocs estimate: 1.
vs
julia> function test((;h, t_w, E, F_y)::AISCSteel.Shapes.IShapes.RolledIShapes.WShape)
λ = h / t_w
λ_p = 3.76 * sqrt(E / F_y)
λ_r = 5.7 * sqrt(E / F_y)
if λ <= λ_p
class = :compact
elseif λ_p < λ <= λ_r
class = :noncompact
else
class = :slender
end
return λ, λ_p, λ_r, class
end
test (generic function with 1 method)
julia> @benchmark test(w)
BenchmarkTools.Trial: 10000 samples with 1000 evaluations per sample.
Range (min … max): 5.667 ns … 11.625 ns ┊ GC (min … max): 0.00% … 0.00%
Time (median): 5.750 ns ┊ GC (median): 0.00%
Time (mean ± σ): 5.777 ns ± 0.109 ns ┊ GC (mean ± σ): 0.00% ± 0.00%
▃ █ █ ▃ ▂ ▂ ▂
▃▁▁▁▁▁▁▁▁█▁▁▁▁▁▁▁▁▁█▁▁▁▁▁▁▁▁█▁▁▁▁▁▁▁▁▁█▁▁▁▁▁▁▁▁▁█▁▁▁▁▁▁▁▁█ █
5.67 ns Histogram: log(frequency) by time 5.92 ns <
Memory estimate: 0 bytes, allocs estimate: 0.
Is this expected?