# NearestNeighbors.jl - Can I formulate this in a way which makes it allocate less?

**URL:** <https://discourse.julialang.org/t/nearestneighbors-jl-can-i-formulate-this-in-a-way-which-makes-it-allocate-less/54778>\
**Category:** General Usage\
**Created:** [February 7, 2021, 9:41am UTC](https://discourse.julialang.org/t/nearestneighbors-jl-can-i-formulate-this-in-a-way-which-makes-it-allocate-less/54778 "2021-02-07T09:41:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [February 7, 2021, 9:41am UTC](https://discourse.julialang.org/t/nearestneighbors-jl-can-i-formulate-this-in-a-way-which-makes-it-allocate-less/54778/1 "2021-02-07T09:41:16Z")

</div>

Hello!

I am using a library called NearestNeighbors to calculate neighbouring points. A simple script below shows the general idea of what I want to do:

```julia
using NearestNeighbors
using StaticArrays

function help_me_allocate_less()
    nCoords = 3000
    coordsSA = [SVector{3,Float64}(rand(),rand(),rand()) for i in 1:nCoords]
    R = 0.2
    balltree = BallTree(coordsSA,reorder = false)
    idxs = inrange(balltree,coordsSA, R, false)

    for t = 1:100
        if mod(t,8) == 0
            balltree = BallTree(coordsSA,reorder = false)
            idxs = inrange(balltree, coordsSA, R, false)
        end
        coordsSA = [SVector{3,Float64}(rand(),rand(),rand()) for i in 1:nCoords]
    end
end

@time help_me_allocate_less()

```

And when I run this:

```julia
 @time help_me_allocate_less()
  0.270642 seconds (261.25 k allocations: 73.013 MiB, 10.49% gc time)

```

Would it be possible in some way to “replace” balltree and idxs “in place” such that it does not allocate that much?

Kind regards

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [February 7, 2021, 9:51am UTC](https://discourse.julialang.org/t/nearestneighbors-jl-can-i-formulate-this-in-a-way-which-makes-it-allocate-less/54778/2 "2021-02-07T09:51:34Z")

</div>

Why do you need to recreate `balltree` in each loop iteration, in your example you are not modifying the inputs ro `BallTree`?

---

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [February 7, 2021, 9:54am UTC](https://discourse.julialang.org/t/nearestneighbors-jl-can-i-formulate-this-in-a-way-which-makes-it-allocate-less/54778/3 "2021-02-07T09:54:48Z")

</div>

Sorry, the example was a bit too simple. I have to update the balltree since “coordsSA” is changing.

I updated the example to reflect that now, thanks!
