Repository: invenia/NamedDims.jl (julia)

Issue:
Allocation tests post-1.3
Many of the allocation tests fail on master (and on 1.4), essentially because this is nonzero:
```
julia> @allocated (() -> (1+1))()
62992

julia> VERSION
v"1.5.0-DEV.88"
```
On slack, kristofferc points me to https://github.com/JuliaLang/julia/pull/33717, after which the `@allocated` macro no longer excludes compilation cost. So we will need to adjust how we test.

Pull request description:
Closes #89

Patch:
diff --git a/Project.toml b/Project.toml
index 5e8227c..09f8174 100644
--- a/Project.toml
+++ b/Project.toml
@@ -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"]
diff --git a/src/name_core.jl b/src/name_core.jl
index c7bd267..781919a 100644
--- a/src/name_core.jl
+++ b/src/name_core.jl
@@ -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
@@ -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
-
diff --git a/src/wrapper_array.jl b/src/wrapper_array.jl
index 49d6ae9..a50a142 100644
--- a/src/wrapper_array.jl
+++ b/src/wrapper_array.jl
@@ -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"


Test patch:
diff --git a/test/functions_math.jl b/test/functions_math.jl
index 5fa95f9..35b1404 100644
--- a/test/functions_math.jl
+++ b/test/functions_math.jl
@@ -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
 
 
diff --git a/test/name_core.jl b/test/name_core.jl
index bbe2359..9cca305 100644
--- a/test/name_core.jl
+++ b/test/name_core.jl
@@ -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
 
@@ -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
 
 
@@ -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
 
 
@@ -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
 
 
@@ -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
 
 
@@ -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
 
@@ -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
 
 
@@ -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
diff --git a/test/runtests.jl b/test/runtests.jl
index 97d81d3..2aa0c51 100644
--- a/test/runtests.jl
+++ b/test/runtests.jl
@@ -1,4 +1,5 @@
 using NamedDims
+using BenchmarkTools
 using Test
 
 const testfiles = (
diff --git a/test/wrapper_array.jl b/test/wrapper_array.jl
index 60ef4f4..280fd9f 100644
--- a/test/wrapper_array.jl
+++ b/test/wrapper_array.jl
@@ -150,9 +150,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, :_))
@@ -161,10 +162,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