Type defintions of composite types with concrete dimensions


I would like to improve the performance of my code. And now starting to make stable types. Is it possible to create types with a concrete dimension in a struct with other fields of the struct? I know that this is not working, but I hope, that it is clear what I would like to do. And does it have a big effect to specify the concrete dimension of the variables instead of just specify the type?

#Code of the struct
mutable struct Ind
    obj_func_goal_val::Array{Float64,2}(num_obj_func, 1)
    par_names::Array{String,1}(num_parm_types, 1)
    sorting_method::Array{String,1}(num_parm_types, 1)
    dist_method::Array{String,1}(num_parm_types, 1)
    parms_lim ::Array{Array{Float64(2, 1),1},1}(num_parm_types, 1)
    par_size::Array{Int64,1}(num_parm_types, 1)
    min_gap::Array{Float64,1}(num_parm_types, 1)
    parms ::Array{Array{Float64,2}(undef, 1),1}(num_parm_types, 1)

# Code of the initialization of an instance of the struct Ind
num_parm_types = 2
num_obj_func = 2
obj_func_names = ["f1", "f2"]
par_names = ["x1", "x2"]
sorting_method = ["None", "None"]
dist_method = ["Random", "Random"]
obj_func_goal = ["Minimum", "Minimum"]
min_gap = [0.0,0.0]
par_size = [1, 1]
parms_lim = [[-5, 10], [-5.0, 10.0]]

ind = Individual.Ind(obj_func_goal, num_parm_types, num_obj_func, obj_func_names, par_names, sorting_method, dist_method,
    parms_lim, par_size, min_gap)

I think what you’re looking for is basically StaticArrays.jl. These are arrays whose size is saved in their type and hence known at compile time, so the compiler can do various optimizations that wouldn’t be possible otherwise. Whether this will actually give a speedup in your case probably depends on what exactly you’re doing, if you’re doing a lot of indexing operations into these arrays there’s a chance it might. To do this, the size of your arrays needs to be known at compile time, hence needs to be part of the type’s parameters, so it would look something like this:

using StaticArrays

struct Ind{num_parm_types}
    obj_func_goal :: StaticVector{num_parm_types,String}

Ind(@SVector["f1", "f2"]) # would create an Ind object where num_parm_types=2
Ind(@SVector["f1", "f2", "f2"]) # would create an Ind object where num_parm_types=3, etc... 

Depending on what exactly you’re trying to do, you might also just look at using Tuples, or NamedTuples directly (StaticArrays are in fact just wrappers around tuples anyways). Something like parms_lim = (x1=(-5,10), x2=(-5,10)) might give the same type stability you want.