diff --git a/src/kd.jl b/src/kd.jl index aa155da..6460823 100644 --- a/src/kd.jl +++ b/src/kd.jl @@ -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) @@ -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, ...