# Avoid allocations in one-dimensional Optim when target expects vector

**URL:** https://discourse.julialang.org/t/avoid-allocations-in-one-dimensional-optim-when-target-expects-vector/131159
**Category:** General Usage
**Tags:** allocations
**Created:** [July 29, 2025, 7:40am UTC](https://discourse.julialang.org/t/avoid-allocations-in-one-dimensional-optim-when-target-expects-vector/131159 "2025-07-29T07:40:32Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![JADekker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jadekker/32/210281_2.png) [@JADekker](https://discourse.julialang.org/u/JADekker)
#### Post date: [July 29, 2025, 7:40am UTC](https://discourse.julialang.org/t/avoid-allocations-in-one-dimensional-optim-when-target-expects-vector/131159/1 "2025-07-29T07:40:32Z")

</div>

Hi, I’m in an optimization situation where I have an objective function that expects a vector input, but the optimization problem is over just one variable, so I’d like to use Brent’s algorithm. To make it compatible with my function, I simply use `x -> test_vector([x])` in the below, which seems to add a lot of allocations. Is there a way to avoid this? (In my use-case, I have something that looks like `test_vector`, not `test_scalar`). See below MWE

```julia-auto
using BenchmarkTools
using Optim

test_scalar(x::Real) = x^2
function optims_scalar()
    ress = 0.
    for i in 1:10
        ress += Optim.optimize(test_scalar, 0.0, 1.0, Brent()).minimizer
    end
    return ress
end

test_vector(x::AbstractVector) = @inbounds x[1]^2
function optims_vector()
    ress = 0.
    for i in 1:10
        ress += Optim.optimize(x -> test_vector([x]), 0.0, 1.0, Brent()).minimizer[1]
    end
    return ress
end

function run_tests()
    display(@benchmark optims_scalar())
    display(@benchmark optims_vector())
end

run_tests()

```

gives

```julia-auto

BenchmarkTools.Trial: 10000 samples with 1 evaluation per sample.
 Range (min … max): 12.000 μs … 49.500 μs ┊ GC (min … max): 0.00% … 0.00%
 Time (median): 12.125 μs ┊ GC (median): 0.00%
 Time (mean ± σ): 12.383 μs ± 1.590 μs ┊ GC (mean ± σ): 0.00% ± 0.00%

  █▇▆▄▄▃▁▂▃▂ ▂
  ████████████▇▇▇▆▅▆▅▆▅▅▃▆▄▄▃▄▅▅▄▅▅▄▅▃▄▆▁▅▄▅▅▄▃▄▄▁▃▆▅▅▅▅▅▆▅▆▇ █
  12 μs Histogram: log(frequency) by time 17.4 μs <

 Memory estimate: 1.72 KiB, allocs estimate: 20.
BenchmarkTools.Trial: 10000 samples with 1 evaluation per sample.
 Range (min … max): 17.416 μs … 32.571 ms ┊ GC (min … max): 0.00% … 99.88%
 Time (median): 18.334 μs ┊ GC (median): 0.00%
 Time (mean ± σ): 22.684 μs ± 326.642 μs ┊ GC (mean ± σ): 16.69% ± 2.19%

   ▇█ ▁ ▄▂                                                  
  ▅██▇▇█▅▄▃▂██▆▅▇▄▃▃▃▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▁▂▂▂▂ ▃
  17.4 μs Histogram: frequency by time 27 μs <

 Memory estimate: 47.97 KiB, allocs estimate: 760.

```

fwiw, I’m on v1.10.4 for compatibility reasons

---

<div class="post-metadata">

### Author: ![yolhan\_mannes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yolhan_mannes/32/220485_2.png) [@yolhan\_mannes](https://discourse.julialang.org/u/yolhan_mannes)
#### Post date: [July 29, 2025, 8:09am UTC](https://discourse.julialang.org/t/avoid-allocations-in-one-dimensional-optim-when-target-expects-vector/131159/2 "2025-07-29T08:09:33Z")

</div>

Can you try using StaticArrays.jl and just do `SA[x]` instead, in 1.12 everything is fine but it may be because of the compiler improvment. BTW if you can write your problem as a system of Optim its far better than solving for each i like that

---

<div class="post-metadata">

### Author: ![JADekker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jadekker/32/210281_2.png) [@JADekker](https://discourse.julialang.org/u/JADekker)
#### Post date: [July 29, 2025, 8:20am UTC](https://discourse.julialang.org/t/avoid-allocations-in-one-dimensional-optim-when-target-expects-vector/131159/3 "2025-07-29T08:20:02Z")

</div>

I’ll try the static version in a bit. What do you mean with a system of Optim?

---

<div class="post-metadata">

### Author: ![yolhan\_mannes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yolhan_mannes/32/220485_2.png) [@yolhan\_mannes](https://discourse.julialang.org/u/yolhan_mannes)
#### Post date: [July 29, 2025, 8:22am UTC](https://discourse.julialang.org/t/avoid-allocations-in-one-dimensional-optim-when-target-expects-vector/131159/4 "2025-07-29T08:22:01Z")

</div>

I mean if you can minimize a vector instead, the only reason you couldn’t is if you need previous results

---

<div class="post-metadata">

### Author: ![JADekker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jadekker/32/210281_2.png) [@JADekker](https://discourse.julialang.org/u/JADekker)
#### Post date: [July 29, 2025, 8:25am UTC](https://discourse.julialang.org/t/avoid-allocations-in-one-dimensional-optim-when-target-expects-vector/131159/5 "2025-07-29T08:25:04Z")

</div>

In my use case, each ‘i’ actually gets a slightly different function, so I’m afraid that won’t work for me

---

<div class="post-metadata">

### Author: ![JADekker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jadekker/32/210281_2.png) [@JADekker](https://discourse.julialang.org/u/JADekker)
#### Post date: [July 29, 2025, 8:48am UTC](https://discourse.julialang.org/t/avoid-allocations-in-one-dimensional-optim-when-target-expects-vector/131159/6 "2025-07-29T08:48:35Z")

</div>

Works like a charm, both in my MWE and in the code where I need it! Thanks! (Saves me about 5% runtime and 6% reduction in memory allocation size in my actual code, which is a nice improvement for adding two letters…)
