# Interpolations.jl interpolation evaluation causes allocation

**URL:** <https://discourse.julialang.org/t/interpolations-jl-interpolation-evaluation-causes-allocation/126279>\
**Category:** Performance\
**Tags:** question, package, interpolations\
**Created:** [February 25, 2025, 4:21am UTC](https://discourse.julialang.org/t/interpolations-jl-interpolation-evaluation-causes-allocation/126279 "2025-02-25T04:21:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bart\_van\_de\_Lint](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bart_van_de_lint/32/212161_2.png) [@Bart\_van\_de\_Lint](https://discourse.julialang.org/u/Bart_van_de_Lint)\
**Post date:** [February 25, 2025, 4:21am UTC](https://discourse.julialang.org/t/interpolations-jl-interpolation-evaluation-causes-allocation/126279/1 "2025-02-25T04:21:02Z")

</div>

In the following MWE, all interpolation evaluations cause 1 allocation. I want to get rid of this allocation for optimal performance. What should I do to achieve this?

```julia
using Interpolations, BenchmarkTools

# Create test data
n = 5 # Size of test matrix
alphas = range(-10, 10, n) # Angle of attack range: -10° to 10°
d_trailing_edge_angles = range(0, 30, n) # Trailing edge deflection: 0° to 30°

# Create cl_matrix with a simple aerodynamic model
cl_matrix = zeros(n, n)
for (i, alpha) in enumerate(alphas)
    for (j, delta) in enumerate(d_trailing_edge_angles)
        # Simple linear combination of angle of attack and trailing edge deflection
        cl_matrix[i,j] = 2π * (deg2rad(alpha) + 0.3 * deg2rad(delta))
    end
end

cl_interp = extrapolate(scale(interpolate(cl_matrix, BSpline(Linear())), alphas, d_trailing_edge_angles), NaN)
@btime cl_interp[0.0, 0.0]
cl_interp = scale(interpolate(cl_matrix, BSpline(Linear())), alphas, d_trailing_edge_angles)
@btime cl_interp[0.0, 0.0]
cl_interp = interpolate((alphas, d_trailing_edge_angles), cl_matrix, Gridded(Linear()))
@btime cl_interp[0.0, 0.0]

```

Output:

```julia
  62.233 ns (1 allocation: 16 bytes)
  61.350 ns (1 allocation: 16 bytes)
  84.053 ns (1 allocation: 16 bytes)

```

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [February 25, 2025, 5:33am UTC](https://discourse.julialang.org/t/interpolations-jl-interpolation-evaluation-causes-allocation/126279/2 "2025-02-25T05:33:42Z")

</div>

Use [GitHub - JuliaMath/Dierckx.jl: Julia package for 1-d and 2-d splines](https://github.com/JuliaMath/Dierckx.jl) instead?

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [February 25, 2025, 6:01am UTC](https://discourse.julialang.org/t/interpolations-jl-interpolation-evaluation-causes-allocation/126279/3 "2025-02-25T06:01:52Z")

</div>

> [@Bart\_van\_de\_Lint](#):
>
> `@btime cl_interp[0.0, 0.0]`

Try

```julia
@btime $cl_interp[0.0, 0.0]

```

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [February 25, 2025, 6:07am UTC](https://discourse.julialang.org/t/interpolations-jl-interpolation-evaluation-causes-allocation/126279/4 "2025-02-25T06:07:23Z")

</div>

You’re benchmarking a kernel with a non-`const` and `Any`-typed global variable `cl_interp`, causing a small allocation. Usually you’d pass it to an argument whose type could be inferred. BenchmarkTools does this with `$`-interpolation:

```julia
julia> @btime cl_interp[0.0, 0.0]
  84.511 ns (1 allocation: 16 bytes)
0.0

julia> @btime $cl_interp[0.0, 0.0]
  64.658 ns (0 allocations: 0 bytes)
0.0

```

Additionally, the indexing syntax is deprecated, you could see the warnings in a `--depwarn=yes` session, but the method’s file is also a hint:

```julia
julia> @which cl_interp[0.0, 0.0]
getindex(itp::AbstractInterpolation{T, N}, i::Vararg{Number, N}) where {T, N}
     @ Interpolations deprecated.jl:103

```

The replacement is just `cl_interp(0.0, 0.0)`.
