Skip to content

Commit 00452ab

Browse files
authored
work around perf regression on 0.7 (#101)
1 parent 0257f33 commit 00452ab

File tree

5 files changed

+25
-7
lines changed

5 files changed

+25
-7
lines changed

Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ version = "0.7.0"
44

55
[deps]
66
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
7+
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
78

89
[targets.test.deps]
910
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"

src/Distances.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ __precompile__()
33
module Distances
44

55
using LinearAlgebra
6+
using Statistics
67

78
export
89
# generic types/functions

src/metrics.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ const ArraySlice{T} = SubArray{T,1,Array{T,2},Tuple{Base.Slice{Base.OneTo{Int}},
157157
end
158158
@inbounds begin
159159
s = eval_start(d, a, b)
160-
@simd for I in eachindex(a, b)
160+
@simd for I in 1:length(a)
161161
ai = a[I]
162162
bi = b[I]
163163
s = eval_reduce(d, s, eval_op(d, ai, bi))

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ using Distances
33
using Test
44
using LinearAlgebra
55
using Random
6+
using Statistics
67

78
include("F64.jl")
89
include("test_dists.jl")

test/test_dists.jl

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -511,9 +511,9 @@ end
511511
end
512512

513513
@testset "Bregman Divergence" begin
514-
# Some basic tests.
514+
# Some basic tests.
515515
@test_throws ArgumentError bregman(x -> x, x -> 2*x, [1, 2, 3], [1, 2, 3])
516-
# Test if Bregman() correctly implements the gkl divergence between two random vectors.
516+
# Test if Bregman() correctly implements the gkl divergence between two random vectors.
517517
F(p) = LinearAlgebra.dot(p, log.(p));
518518
(p) = map(x -> log(x) + 1, p)
519519
testDist = Bregman(F, ∇)
@@ -522,13 +522,28 @@ end
522522
p = p/sum(p);
523523
q = q/sum(q);
524524
@test evaluate(testDist, p, q) gkl_divergence(p, q)
525-
# Test if Bregman() correctly implements the squared euclidean dist. between them.
525+
# Test if Bregman() correctly implements the squared euclidean dist. between them.
526526
@test bregman(x -> norm(x)^2, x -> 2*x, p, q) sqeuclidean(p, q)
527-
# Test if Bregman() correctly implements the IS distance.
527+
# Test if Bregman() correctly implements the IS distance.
528528
F(p) = -1 * sum(log.(p))
529529
(p) = map(x -> -1 * x^(-1), p)
530530
function ISdist(p::AbstractVector, q::AbstractVector)
531531
return sum([p[i]/q[i] - log(p[i]/q[i]) - 1 for i in 1:length(p)])
532532
end
533-
@test bregman(F, ∇, p, q) ISdist(p, q)
534-
end
533+
@test bregman(F, ∇, p, q) ISdist(p, q)
534+
end
535+
536+
@testset "zero allocation colwise!" begin
537+
d = Euclidean()
538+
a = rand(2, 41)
539+
b = rand(2, 41)
540+
z = zeros(41)
541+
colwise!(z, d, a, b)
542+
# This fails when bounds checking is enforced
543+
bounds = Base.JLOptions().check_bounds
544+
if bounds == 0
545+
@test (@allocated colwise!(z, d, a, b)) == 0
546+
else
547+
@test_broken (@allocated colwise!(z, d, a, b)) == 0
548+
end
549+
end

0 commit comments

Comments
 (0)