# Efficiently passing a set of constants to a function

**URL:** <https://discourse.julialang.org/t/efficiently-passing-a-set-of-constants-to-a-function/72248>\
**Category:** Performance\
**Tags:** functions, constants\
**Created:** [November 29, 2021, 4:37pm UTC](https://discourse.julialang.org/t/efficiently-passing-a-set-of-constants-to-a-function/72248 "2021-11-29T16:37:16Z")\
**Posts on this page:** 2\
**Page:** 2

<div class="post-metadata">

**Author:** ![leoflotor](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leoflotor/32/35345_2.png) [@leoflotor](https://discourse.julialang.org/u/leoflotor)\
**Post date:** [July 8, 2022, 4:30pm UTC](https://discourse.julialang.org/t/efficiently-passing-a-set-of-constants-to-a-function/72248/21 "2022-07-08T16:30:04Z")

</div>

> [@lmiq](#):
>
> The size of the struct for which it makes sense to store the constants and pass it around is probably similar to the recommended size of StaticArrays, which is roughly 100 floats.

Maybe I’m understanding it wrong but you mean that the significant size of the constants set should be up to 100 approximately to consider using a struct this way?

If it is okay using it this way for a small constant set then could it be passed several times throughout the functions of a module without a severe speed or memory penalty?

For example, if the functions do different things but do need those constants for different computations:

```julia
mutable struc Constants
    ...
end

function compute_this(x, params=Constants() )
    ...
end

function compute_that(x, y, params=Constants() )
    ...
end

# more functions below ...

```

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [July 8, 2022, 7:25pm UTC](https://discourse.julialang.org/t/efficiently-passing-a-set-of-constants-to-a-function/72248/22 "2022-07-08T19:25:15Z")

</div>

The largest number of constraints should be roughly that one. But I’m not thinking of the case in which you are repeatedly creating a new instance of the structure, in independent functions.

I’m thinking more on the case where you have nested function calls and the parameters are passed around.

In this case, a small immutable struct is a good option. If the number of parameters is too large you will end having to use a heap allocated object (an array, a dict, etc), and then it may be better for performance to copy the constants that are used in each local scope to avoid unnecessary `getindex` calls.

[Previous page](https://discourse.julialang.org/t/efficiently-passing-a-set-of-constants-to-a-function/72248.md?page=1)
