Skip to content

Commit

Permalink
Move the call to partialsort! to a separate function to avoid
Browse files Browse the repository at this point in the history
overhead caused by the closure passed as the by argument
  • Loading branch information
andreasnoack committed Aug 15, 2023
1 parent 1b34e35 commit ad4252f
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/kd.jl
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ function diameter(bounds::Matrix)
euclidean(vec(bounds[1,:]), vec(bounds[2,:]))
end

# Helper function to add a function barrier aorund the closure passed
# as the by argument. There is significant overhead without the barrier.
function _partialsort!(perm, mid1, mid2, xs, j)
return partialsort!(perm, mid1:mid2, by = i -> xs[i, j])
end

"""
build_kdtree(xs, perm, bounds, leaf_size_cutoff, leaf_diameter_cutoff, verts)
Expand Down Expand Up @@ -185,7 +190,7 @@ function build_kdtree(xs::AbstractMatrix{T},
offset = -offset + (offset <= 0)
continue
end
p12 = partialsort!(perm, mid1:mid2, by = i -> xs[i, j])
p12 = _partialsort!(perm, mid1, mid2, xs, j)
if xs[p12[1], j] == xs[p12[2], j]
@debug "tie! Adjusting offset" xs[p12[1], j] xs[p12[2], j] offset
# This makes the offset 0, 1, -1, 2, -2, ...
Expand Down

0 comments on commit ad4252f

Please sign in to comment.