# Efficient definition of function which uses fixed allocated constants

**URL:** <https://discourse.julialang.org/t/efficient-definition-of-function-which-uses-fixed-allocated-constants/127955>\
**Category:** General Usage\
**Created:** [April 11, 2025, 2:43am UTC](https://discourse.julialang.org/t/efficient-definition-of-function-which-uses-fixed-allocated-constants/127955 "2025-04-11T02:43:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![UselessAnsatz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uselessansatz/32/26459_2.png) [@UselessAnsatz](https://discourse.julialang.org/u/UselessAnsatz)\
**Post date:** [April 11, 2025, 2:43am UTC](https://discourse.julialang.org/t/efficient-definition-of-function-which-uses-fixed-allocated-constants/127955/1 "2025-04-11T02:43:13Z")

</div>

I need to define a whole group of functions, each of which depends on large heap allocated arrays which do not change run to run. While I could declare these arrays const, this clutters the namespace as the array names used are similar for each function, and defining a separate module to separate the namespace for each is something I would rather not do. Here is a toy example of such a definition:

```julia
const B = [1,2,45,3]
non_alloc_const(x) = sum(x^a for a in B)

```

However, declaring the arrays within the function leads to them being reallocated each time, as in the function below, which is undesirable given the size of the arrays.

```julia
function alloc(x)
    A = [1,2,45,3]
    return sum(x^a for a in A)
end

```

And something like the below code does exactly what I want, but I tend to develop by repeatedly executing the code in the same REPL in vscode, so an error is thrown as the const is redefined every time. Removing the const makes the function a non-constant global and rather slow.

```julia
const non_alloc_let = let
    A = [1,2,45,3]
    g(x) = sum(x^a for a in A)
end

```

Is there another, easier way to define such a function? Am I missing something obvious?

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [April 11, 2025, 5:19am UTC](https://discourse.julialang.org/t/efficient-definition-of-function-which-uses-fixed-allocated-constants/127955/2 "2025-04-11T05:19:24Z")

</div>

How about a callable `struct`, aka functor?

```julia
julia> struct SumPowers{V<:AbstractVector}
           A::V
       end

julia> (sp::SumPowers)(x::Number) = sum(x^a for a in sp.A)

julia> A = [1, 2, 45, 3];

julia> sp = SumPowers(A)
SumPowers{Vector{Int64}}([1, 2, 45, 3])

julia> sp(1.1)
76.53148368510332

```

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [April 11, 2025, 8:21am UTC](https://discourse.julialang.org/t/efficient-definition-of-function-which-uses-fixed-allocated-constants/127955/3 "2025-04-11T08:21:50Z")

</div>

> [@UselessAnsatz](#):
>
> And something like the below code does exactly what I want, but I tend to develop by repeatedly executing the code in the same REPL in vscode, so an error is thrown as the const is redefined every time. […]

You can make the function global directly like this:

```julia
let A = [1,2,45,3]
    global g(x) = sum(x^a for a in A)
end

```
