Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use ArrayOfArrays for return value to reduce the number of allocated arrays #167

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

KristofferC
Copy link
Owner

A long standing gripe for me has been that indices and distances are returned as standard nested Arrays. Typically, each inner array hold quite a small number of neighbors so it means that we allocate a large number of small arrays.

Using ArrayOfArrays, these are stored contigously in one large flat array instead.

The difference in allocations can be readily seen:

julia> input = rand(3, 10^6);

julia> tree = KDTree(rand(3, 10^6));

julia> @time knn(tree, input, 5);
  1.538003 seconds (2.00 M allocations: 221.253 MiB, 10.03% gc time)

julia> @time knn(tree, input, 5);
  1.489310 seconds (98 allocations: 189.884 MiB, 0.29% gc time)

…arrays

A long standing gripe for me has been that indices and distances are
returned as standard nested `Array`s. Typically, each inner array hold
quite a small number of neighbors so  it means that we allocate a large
number of small arrays.

Using ArrayOfArrays, these are stored contigously in one large flat
array instead.

The difference in allocations can be readily seen:

```julia
julia> input = rand(3, 10^6);

julia> tree = KDTree(rand(3, 10^6));

julia> @time knn(tree, input, 5);
  1.538003 seconds (2.00 M allocations: 221.253 MiB, 10.03% gc time)

julia> @time knn(tree, input, 5);
  1.489310 seconds (98 allocations: 189.884 MiB, 0.29% gc time)
```
@KristofferC
Copy link
Owner Author

One potential issue with this is that getindex is slower on AoA so depending on how people use the return value they might encounter performance regressions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant