airspaced-nk5 / GeometricalOptics.jl

A simple, compact, and extensible tool for optical raytrace evaluation in the Julia Programming Language.
https://airspaced-nk5.github.io/GeometricalOptics.jl
MIT License
4 stars 0 forks source link

`struct`s could be more efficient #17

Open jlapeyre opened 2 years ago

jlapeyre commented 2 years ago

You are hiding type information from the compiler.

struct bundle_as_array{T<: Real}
    x::AbstractArray{T,2}
    y::AbstractArray{T,2}
    angx::AbstractArray{T,2}# these are direction tangents.
    angy::AbstractArray{T,2}
    zpos:: Union{AbstractArray{T,2},Real}
end

function (cb::bundle_as_array)()

    if length(size(cb.zpos))==2
        R0=[[cb.x[i,j].+eps(),cb.y[i,j].+eps(),cb.zpos[i,j].+eps()] for i in 1:size(cb.x)[1],j in 1:size(cb.y)[2]]
    else
        R0=[[cb.x[i,j].+eps(),cb.y[i,j].+eps(),cb.zpos.+eps()] for i in 1:size(cb.x)[1],j in 1:size(cb.y)[2]]
    end
    D0=[[sin(cb.angx[i,j]),sin(cb.angy[i,j]),sqrt(1-sin(cb.angx[i,j])^2-sin(cb.angy[i,j])^2)] for i in 1:size(cb.x)[1],j in 1:size(cb.y)[2]]
    return R0,D0
end

struct bundle_as_array2{T, V}
    x::T
    y::T
    angx::T
    angy::T
    zpos::V
end

function (cb::bundle_as_array2)()

    if length(size(cb.zpos))==2
        R0=[[cb.x[i,j].+eps(),cb.y[i,j].+eps(),cb.zpos[i,j].+eps()] for i in 1:size(cb.x)[1],j in 1:size(cb.y)[2]]
    else
        R0=[[cb.x[i,j].+eps(),cb.y[i,j].+eps(),cb.zpos.+eps()] for i in 1:size(cb.x)[1],j in 1:size(cb.y)[2]]
    end
    D0=[[sin(cb.angx[i,j]),sin(cb.angy[i,j]),sqrt(1-sin(cb.angx[i,j])^2-sin(cb.angy[i,j])^2)] for i in 1:size(cb.x)[1],j in 1:size(cb.y)[2]]
    return R0,D0
end

eval_bundle = bundle_as_array([0.5 1.], [0.5 1.], [0. 0.], [0. 0.], 0.)

eval_bundle2 = bundle_as_array2([0.5 1.], [0.5 1.], [0. 0.], [0. 0.], 0.)
julia> @btime $eval_bundle();
  5.250 μs (77 allocations: 2.41 KiB)

julia> @btime $eval_bundle2();
  187.365 ns (6 allocations: 448 bytes)

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

airspaced-nk5 commented 2 years ago

Thanks for the tip @jlapeyre ! I implemented changes to composite types to address this.