Skip to content

Commit

Permalink
Merge pull request #101 from invenia/ox/balloc
Browse files Browse the repository at this point in the history
Use ballocated for tests
  • Loading branch information
oxinabox committed Mar 11, 2020
2 parents 233a553 + d7bf6df commit 8b26b1e
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 38 deletions.
4 changes: 3 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ Requires = "ae029012-a4dd-5104-9daa-d747884805df"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"

[compat]
BenchmarkTools = "0.5"
Requires = "0.5, 1"
Tracker = "0.2.2"
julia = "1"

[extras]
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Tracker = "9f7883ad-71c0-57eb-9f7f-b5c9e6d3789c"

[targets]
test = ["Test", "SparseArrays", "Tracker"]
test = ["BenchmarkTools", "SparseArrays", "Test", "Tracker"]
3 changes: 1 addition & 2 deletions src/name_core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ function unify_names(names_a, names_b)
end
end
unify_names(a) = a
unify_names(a, b, cs...) = unify_names(unify_names(a,b), cs...)
unify_names(a, b, cs...) = unify_names(unify_names(a, b), cs...)
# @btime (()->unify_names((:a, :b), (:a, :_), (:_, :b)))()

names_are_unifiable(names_a, names_b) = try_unify_names(names_a, names_b) !== nothing
Expand Down Expand Up @@ -325,4 +325,3 @@ This is like `vcat` for tuples, it splats everything into one long tuple.
tuple_cat(x::Tuple, ys::Tuple...) = (x..., tuple_cat(ys...)...)
tuple_cat() = ()
# @btime tuple_cat((1, 2), (3, 4, 5), (6,)) # 0 allocations

2 changes: 1 addition & 1 deletion src/wrapper_array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ struct NamedDimsArray{L, T, N, A<:AbstractArray{T, N}} <: AbstractArray{T, N}
data::A
end

function NamedDimsArray{L}(orig::AbstractArray{T, N}) where {L, T, N}
@inline function NamedDimsArray{L}(orig::AbstractArray{T, N}) where {L, T, N}
if !(L isa NTuple{N, Symbol})
throw(ArgumentError(
"A $N dimensional array, needs a $N-tuple of dimension names. Got: $L"
Expand Down
4 changes: 2 additions & 2 deletions test/functions_math.jl
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ end
end
end
@testset "allocations: matmul names" begin
@test 0 == @allocated (() -> matrix_prod_names((:foo, :bar), (:bar,)))()
@test 0 == @allocated (() -> symmetric_names((:foo, :bar), 1))()
@test 0 == @ballocated (() -> matrix_prod_names((:foo, :bar), (:bar,)))()
@test 0 == @ballocated (() -> symmetric_names((:foo, :bar), 1))()
end


Expand Down
50 changes: 25 additions & 25 deletions test/name_core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ using Test
end
end
@testset "allocations: dim" begin
@test 0 == @allocated (()->dim((:a, :b), :b))()
@test 0 == @allocated (()->dim_noerror((:a, :b, :c), :c))()
@test 0 == @ballocated (()->dim((:a, :b), :b))()
@test 0 == @ballocated (()->dim_noerror((:a, :b, :c), :c))()
if VERSION >= v"1.1"
@test 0 == @allocated (()->dim((:a,:b), (:a,:b)))()
@test 0 == @ballocated (()->dim((:a,:b), (:a,:b)))()
else
@test_broken 0 == @allocated (()->dim((:a,:b), (:a,:b)))()
@test_broken 0 == @ballocated (()->dim((:a,:b), (:a,:b)))()
end
end

Expand Down Expand Up @@ -74,24 +74,24 @@ end
end
@testset "allocations: unify_names_*" begin
for unify in (unify_names, unify_names_longest, unify_names_shortest)
if VERSION >= v"1.2"
@test 0 == @allocated (()->unify((:a, :b), (:a, :_)))()
if VERSION >= v"1.1"
@test 0 == @ballocated (()->$unify((:a, :b), (:a, :_)))()
else
@test_broken 0 == @allocated (()->unify((:a, :b), (:a, :_)))()
@test_broken 0 == @ballocated (()->$unify((:a, :b), (:a, :_)))()
end
end
if VERSION >= v"1.1"
@test 0 == @allocated (()->unify_names_longest((:a, :b), (:a, :_, :c)))()
@test 0 == @allocated (()->unify_names_shortest((:a, :b), (:a, :_, :c)))()
@test 0 == @allocated (()->names_are_unifiable((:a, :b), (:a, :_)))()
@test 0 == @allocated (()->names_are_unifiable((:a, :b), (:a, :c)))()
@test 0 == @ballocated (()->unify_names_longest((:a, :b), (:a, :_, :c)))()
@test 0 == @ballocated (()->unify_names_shortest((:a, :b), (:a, :_, :c)))()
@test 0 == @ballocated (()->names_are_unifiable((:a, :b), (:a, :_)))()
@test 0 == @ballocated (()->names_are_unifiable((:a, :b), (:a, :c)))()
else
@test_broken 0 == @allocated (()->unify_names_longest((:a, :b), (:a, :_, :c)))()
@test_broken 0 == @allocated (()->unify_names_shortest((:a, :b), (:a, :_, :c)))()
@test_broken 0 == @allocated (()->names_are_unifiable((:a, :b), (:a, :_)))()
@test_broken 0 == @allocated (()->names_are_unifiable((:a, :b), (:a, :c)))()
@test_broken 0 == @ballocated (()->unify_names_longest((:a, :b), (:a, :_, :c)))()
@test_broken 0 == @ballocated (()->unify_names_shortest((:a, :b), (:a, :_, :c)))()
@test_broken 0 == @ballocated (()->names_are_unifiable((:a, :b), (:a, :_)))()
@test_broken 0 == @ballocated (()->names_are_unifiable((:a, :b), (:a, :c)))()
end
@test 0 == @allocated (()->names_are_unifiable((:a, :b), (:a, :b)))()
@test 0 == @ballocated (()->names_are_unifiable((:a, :b), (:a, :b)))()
end


Expand All @@ -106,8 +106,8 @@ end
@test order_named_inds(Val((:x, :y)); x=30, y=20) == (30, 20)
end
@testset "allocations: order_named_inds" begin
@test 0 == @allocated (()->order_named_inds(Val((:a, :b, :c)); b=1, c=2))()
@test 0 == @allocated (()->order_named_inds(Val((:a, :b, :c)), (b=1, c=2)))()
@test 0 == @ballocated (()->order_named_inds(Val((:a, :b, :c)); b=1, c=2))()
@test 0 == @ballocated (()->order_named_inds(Val((:a, :b, :c)), (b=1, c=2)))()
end


Expand All @@ -121,7 +121,7 @@ end
@test remaining_dimnames_from_indexing((:a, :b, :c), (CartesianIndex(1,1), :)) == (:c,)
end
@testset "allocations: remaining_dimnames_from_indexing" begin
@test 0 == @allocated (()->remaining_dimnames_from_indexing((:a, :b, :c), (:,390,:)))()
@test 0 == @ballocated (()->remaining_dimnames_from_indexing((:a, :b, :c), (:,390,:)))()
end


Expand All @@ -132,8 +132,8 @@ end
@test remaining_dimnames_after_dropping((:a, :b, :c), (3,1)) == (:b,)
end
@testset "allocations: remaining_dimnames_after_dropping" begin
@test 0 == @allocated remaining_dimnames_after_dropping((:a,:b,:c,:d,:e), 4)
@test 0 == @allocated (()->remaining_dimnames_after_dropping((:a,:b,:c,:d,:e), (1,3)))()
@test 0 == @ballocated remaining_dimnames_after_dropping((:a,:b,:c,:d,:e), 4)
@test 0 == @ballocated (()->remaining_dimnames_after_dropping((:a,:b,:c,:d,:e), (1,3)))()
end


Expand All @@ -150,7 +150,7 @@ end
end
@testset "allocations: permute_dimnames" begin
if VERSION >= v"1.1"
@test 0 == @allocated permute_dimnames((:a,:b,:c), (1,3,2))
@test 0 == @ballocated permute_dimnames((:a,:b,:c), (1,3,2))
end
end

Expand All @@ -160,8 +160,8 @@ end
@test tuple_issubset((:a, :b, :c), (:a, :c)) == false
end
@testset "allocations: tuple_issubset" begin
@test 0 == @allocated tuple_issubset((:a, :c), (:a, :b, :c))
@test 0 == @allocated tuple_issubset((:a, :b, :c), (:a, :c))
@test 0 == @ballocated tuple_issubset((:a, :c), (:a, :b, :c))
@test 0 == @ballocated tuple_issubset((:a, :b, :c), (:a, :c))
end


Expand All @@ -170,5 +170,5 @@ end
@test tuple_cat((1, 2)) == (1, 2)
end
@testset "allocations: tuple_cat" begin
@test 0 == @allocated tuple_cat((1, 2), (3, 4, 5), (6,))
@test 0 == @ballocated tuple_cat((1, 2), (3, 4, 5), (6,))
end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using NamedDims
using BenchmarkTools
using Test

const testfiles = (
Expand Down
15 changes: 8 additions & 7 deletions test/wrapper_array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,10 @@ end

const cnda = NamedDimsArray([10 20; 30 40], (:x, :y))
@testset "allocations: wrapper" begin
@test 0 == @allocated parent(cnda)
@test 0 == @allocated dimnames(cnda)
@test 0 == @ballocated parent(cnda)
@test 0 == @ballocated dimnames(cnda)

# These tests use `@allocated` as for some reason `@ballocated` reports 1 alloc
@test 0 == @allocated refine_names(cnda, (:x, :y))
if VERSION >= v"1.1"
@test 0 == @allocated refine_names(cnda, (:x, :_))
Expand All @@ -167,10 +168,10 @@ const cnda = NamedDimsArray([10 20; 30 40], (:x, :y))
end

# indexing
@test 0 == @allocated cnda[1,1]
@test 0 == @allocated cnda[1,1] = 55
@test 0 == @ballocated cnda[1,1]
@test 0 == @ballocated cnda[1,1] = 55

@test 0 == @allocated cnda[x=1, y=1]
@test @allocated(cnda[x=1]) == @allocated(cnda[1, :])
@test 0 == @allocated cnda[x=1, y=1] = 66
@test 0 == @ballocated cnda[x=1, y=1]
@test @ballocated(cnda[x=1]) == @ballocated(cnda[1, :])
@test 0 == @ballocated cnda[x=1, y=1] = 66
end

0 comments on commit 8b26b1e

Please sign in to comment.