# Allocations from scalar arguments in functions

**URL:** <https://discourse.julialang.org/t/allocations-from-scalar-arguments-in-functions/136927>\
**Category:** Performance\
**Tags:** memory-allocation, benchmark, allocations\
**Created:** [April 30, 2026, 12:03am UTC](https://discourse.julialang.org/t/allocations-from-scalar-arguments-in-functions/136927 "2026-04-30T00:03:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pablo\_Montes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pablo_montes/32/221731_2.png) [@Pablo\_Montes](https://discourse.julialang.org/u/Pablo_Montes)\
**Post date:** [April 30, 2026, 12:03am UTC](https://discourse.julialang.org/t/allocations-from-scalar-arguments-in-functions/136927/1 "2026-04-30T00:03:11Z")

</div>

Hello, I am trying to optimize a code for simulating fluids that is currently allocating massive amounts of memory. I’m focusing on some specific functions that are simple but need to be called repeatedly (thousands of times), so they should not allocate anything. I’m finding that my functions, which take arrays and scalars and do basic operations, are performing small allocations for each scalar argument provided, and again for the result. This is a minimal example:

```julia
function f(a, b1, b2, b3, c)
    return a[1]*b1 + b2*(c[2,1]*a[2]^3) + b3*a[3]
end

a = rand(4)
b1 = 1.0
b2 = 2.0
b3 = 3.0
c = rand(3,3)

@btime f($a, $b1, $b2, $b3, $c)

```

I expect this to not allocate memory at any point. However, I get:

```julia-auto
  47.861 ns (4 allocations: 64 bytes)

```

Why do I get 4 allocations? Are these allocations really a concern, or should I focus my attention elsewhere in the code?

Just in case, this is the actual function I’m trying to optimize, which comes from `Symbolics.build_function`:

```julia
Aabc_functions333 = function (ˍ₋arg1, ˍ₋arg2, χ_1, χ_2, μ, ν, ψ_1, ψ_2, ˍ₋arg9, ˍ₋arg10, ˍ₋arg11, ˍ₋arg12)
    #= /home/pablo/.julia/packages/Symbolics/eUt8E/src/build_function.jl:143 =# @inbounds begin
            #= /home/pablo/.julia/packages/Symbolics/eUt8E/src/build_function.jl:143 =#
            begin
                (+)((+)((+)((+)((+)((+)((/)((*)((*)((*)(-6, (+)((*)((*)(-0.25, ˍ₋arg12[16]), μ), (^)(ˍ₋arg1[4], 2))), ˍ₋arg1[4]), χ_1), (^)(μ, 4)), (/)((*)((*)((*)(12.0, ˍ₋arg1[4]), ˍ₋arg2[16]), χ_2), (^)(μ, 4))), (/)((*)((*)((*)(-96.0, (+)((*)((*)(-0.5, ˍ₋arg12[16]), ν), (*)((*)(2, ˍ₋arg9[4]), ˍ₋arg1[4]))), ˍ₋arg1[4]), χ_2), (^)(μ, 5))), (/)((*)((*)((*)((*)(480.0, (+)((*)((*)(-0.25, ˍ₋arg12[16]), μ), (^)(ˍ₋arg1[4], 2))), ν), ˍ₋arg1[4]), χ_2), (^)(μ, 6))), (/)((*)((*)(24.0, (+)((*)(0.5, (+)((*)((*)(2, ˍ₋arg12[16]), ˍ₋arg9[4]), (*)((*)(2, ˍ₋arg1[4]), ˍ₋arg2[16]))), (*)((*)(-0.5, ˍ₋arg12[16]), ˍ₋arg9[4]))), χ_2), (^)(μ, 4))), (/)((+)((*)((*)((*)(-96.0, (+)((*)((*)(-0.25, ˍ₋arg12[16]), μ), (^)(ˍ₋arg1[4], 2))), ˍ₋arg9[4]), χ_2), (*)((*)((*)((*)(-72.0, ˍ₋arg12[16]), ν), ˍ₋arg1[4]), χ_2)), (^)(μ, 5))), (/)((*)((*)((*)(1.5, ˍ₋arg12[16]), ˍ₋arg1[4]), χ_1), (^)(μ, 3)))
            end
        end
end

```

and this is what I’m doing to benchmark it:

```julia
χ_1 = 1.0
χ_2 = 2.0
μ = 1.0
ν = 0.5
ψ_1 = 0.3
ψ_2 = 0.7
ξᵃ = rand(4)
lᵃ = rand(4)
ηᵃ = rand(4)
ξᵃᵇ = rand(4, 4)
Mᵃᵇ = rand(4, 4)
gᵃᵇ = rand(4, 4)

@btime Aabc_functions333(
    $ξᵃ, $ξᵃᵇ, $χ_1, $χ_2, $μ, $ν, $ψ_1, $ψ_2,
    $lᵃ, $ηᵃ, $Mᵃᵇ, $gᵃᵇ
)

```

and I get:

```julia-auto
  69.661 ns (7 allocations: 112 bytes)

```

---

<div class="post-metadata">

**Author:** ![adienes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adienes/32/37459_2.png) [@adienes](https://discourse.julialang.org/u/adienes)\
**Post date:** [April 30, 2026, 12:15am UTC](https://discourse.julialang.org/t/allocations-from-scalar-arguments-in-functions/136927/2 "2026-04-30T00:15:22Z")

</div>

I can’t recreate your first example; that correctly is non-allocating for me.

for your second example, it’s because `Aabc_functions333` itself is a non-const global. note that

```julia-auto
@btime $(Aabc_functions333)(
    $ξᵃ, $ξᵃᵇ, $χ_1, $χ_2, $μ, $ν, $ψ_1, $ψ_2,
    $lᵃ, $ηᵃ, $Mᵃᵇ, $gᵃᵇ
)

```

is non-allocating. and using `const Aabc_functions333 = function ...` should fix your issue

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [April 30, 2026, 2:55pm UTC](https://discourse.julialang.org/t/allocations-from-scalar-arguments-in-functions/136927/3 "2026-04-30T14:55:07Z")

</div>

> [@adienes](#):
>
> and using `const Aabc_functions333 = function ...` should fix your issue

Or simply `function Aabc_functions333(...) ...`, since a named function is automatically `const`.

Or do this _inside another function_ (or a `let` block) — anonymous functions are fine as long as they are not global variables.

---

<div class="post-metadata">

**Author:** ![Pablo\_Montes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pablo_montes/32/221731_2.png) [@Pablo\_Montes](https://discourse.julialang.org/u/Pablo_Montes)\
**Post date:** [April 30, 2026, 2:56pm UTC](https://discourse.julialang.org/t/allocations-from-scalar-arguments-in-functions/136927/4 "2026-04-30T14:56:08Z")

</div>

Oh, I see that the second example is now fixed, but I didn’t expect to have to interpolate the function itself or to define it as constant!. I was about to post this response last night but I left it written in the editor.
