From e97dde56f505ec16b2a413f30869062033629565 Mon Sep 17 00:00:00 2001 From: chriselrod Date: Wed, 3 Feb 2021 12:10:00 -0500 Subject: [PATCH] Remove all `@pure`, fixes #115. --- .gitignore | 2 ++ src/ArrayInterface.jl | 4 ++-- src/dimensions.jl | 31 +++++++++++++++++-------------- src/static.jl | 12 ++++++------ src/stridelayout.jl | 8 ++++++-- 5 files changed, 33 insertions(+), 24 deletions(-) diff --git a/.gitignore b/.gitignore index a9edec329..c336b31b9 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,5 @@ deps/deps.jl Manifest.toml *~ +*#* +*#*# \ No newline at end of file diff --git a/src/ArrayInterface.jl b/src/ArrayInterface.jl index 7add18856..c8d893fb0 100644 --- a/src/ArrayInterface.jl +++ b/src/ArrayInterface.jl @@ -5,9 +5,9 @@ using Requires using LinearAlgebra using SparseArrays -using Base: @pure, @propagate_inbounds, tail, OneTo, LogicalIndex, Slice, ReinterpretArray +using Base: @propagate_inbounds, tail, OneTo, LogicalIndex, Slice, ReinterpretArray -Base.@pure __parameterless_type(T) = Base.typename(T).wrapper +@generated __parameterless_type(::Type{T}) where {T} = Base.typename(T).wrapper parameterless_type(x) = parameterless_type(typeof(x)) parameterless_type(x::Type) = __parameterless_type(x) diff --git a/src/dimensions.jl b/src/dimensions.jl index 7220d78e4..96e466a99 100644 --- a/src/dimensions.jl +++ b/src/dimensions.jl @@ -148,32 +148,35 @@ to_dims(x::Tuple{Vararg{Symbol}}, d::Colon) = d # `:` is the default for most end return i end -Base.@pure function _sym_to_dim(x::Tuple{Vararg{Symbol,N}}, sym::Symbol) where {N} - for i in 1:N - getfield(x, i) === sym && return i +@generated function _sym_to_dim(x::Tuple{Vararg{Symbol,N}}, sym::Symbol) where {N} + quote + $(Expr(:meta,:inline)) + Base.Cartesian.@nexprs $N n -> getfield(x, n) === sym && return n + return 0 end - return 0 end """ tuple_issubset -A version of `issubset` sepecifically for `Tuple`s of `Symbol`s, that is `@pure`. -This helps it get optimised out of existance. It is less of an abuse of `@pure` than -most of the stuff for making `NamedTuples` work. +A version of `issubset` sepecifically for `Tuple`s of `Symbol`s. """ -Base.@pure function tuple_issubset( +@generated function tuple_issubset( lhs::Tuple{Vararg{Symbol,N}}, rhs::Tuple{Vararg{Symbol,M}} ) where {N,M} N <= M || return false - for a in lhs - found = false - for b in rhs - found |= a === b + quote + $(Expr(:meta,:inline)) + Base.Cartesian.@nexprs $N n -> begin + a = lhs[n] + found = false + Base.Cartesian.@nexprs $M m -> begin + found |= a === rhs[m] + end + found || return false end - found || return false + return true end - return true end """ diff --git a/src/static.jl b/src/static.jl index bfbf6fbd2..fd76a0f4a 100644 --- a/src/static.jl +++ b/src/static.jl @@ -14,7 +14,7 @@ const One = StaticInt{1} Base.show(io::IO, ::StaticInt{N}) where {N} = print(io, "static($N)") -Base.@pure StaticInt(N::Int) = StaticInt{N}() +@inline StaticInt(N::Int) = StaticInt{N}() StaticInt(N::Integer) = StaticInt(convert(Int, N)) StaticInt(::StaticInt{N}) where {N} = StaticInt{N}() StaticInt(::Val{N}) where {N} = StaticInt{N}() @@ -195,7 +195,7 @@ Base.:(&)(x::Bool, y::StaticBool) = x & Bool(y) Base.:(&)(x::StaticBool, y::Bool) = Bool(x) & y Base.xor(y::StaticBool, x::StaticBool) = _xor(x, y) -_xor(::True, ::True) = False() +p_xor(::True, ::True) = False() _xor(::True, ::False) = True() _xor(::False, ::True) = True() _xor(::False, ::False) = False() @@ -250,7 +250,7 @@ Base.promote_rule(::Type{<:StaticBool}, ::Type{<:StaticBool}) = StaticBool Base.promote_rule(::Type{<:StaticBool}, ::Type{Bool}) = Bool Base.promote_rule(::Type{Bool}, ::Type{<:StaticBool}) = Bool -Base.@pure _get_tuple(::Type{T}, ::StaticInt{i}) where {T<:Tuple, i} = T.parameters[i] +@generated _get_tuple(::Type{T}, ::StaticInt{i}) where {T<:Tuple, i} = T.parameters[i] Base.all(::Tuple{Vararg{True}}) = true Base.all(::Tuple{Vararg{Union{True,False}}}) = false @@ -260,14 +260,14 @@ Base.any(::Tuple{Vararg{True}}) = true Base.any(::Tuple{Vararg{Union{True,False}}}) = true Base.any(::Tuple{Vararg{False}}) = false -Base.@pure nstatic(::Val{N}) where {N} = ntuple(i -> StaticInt(i), Val(N)) +@generated nstatic(::Val{N}) where {N} = ntuple(StaticInt, Val(N)) # I is a tuple of Int -@pure function _val_to_static(::Val{I}) where {I} +@inline function _val_to_static(::Val{I}) where {I} return ntuple(i -> StaticInt(getfield(I, i)), Val(length(I))) end -@pure is_permuting(perm::Tuple{Vararg{StaticInt,N}}) where {N} = perm !== nstatic(Val(N)) +@inline is_permuting(perm::Tuple{Vararg{StaticInt,N}}) where {N} = perm !== nstatic(Val(N)) permute(x::Tuple, perm::Tuple) = eachop(getindex, x, perm) function permute(x::Tuple{Vararg{Any,N}}, perm::Tuple{Vararg{Any,N}}) where {N} diff --git a/src/stridelayout.jl b/src/stridelayout.jl index 5527b5929..0f16a75e3 100644 --- a/src/stridelayout.jl +++ b/src/stridelayout.jl @@ -96,8 +96,12 @@ function contiguous_axis_indicator(::Type{A}) where {D,A<:AbstractArray{<:Any,D} end contiguous_axis_indicator(::A) where {A<:AbstractArray} = contiguous_axis_indicator(A) contiguous_axis_indicator(::Nothing, ::Val) = nothing -Base.@pure function contiguous_axis_indicator(::StaticInt{N}, ::Val{D}) where {N,D} - return ntuple(d -> StaticBool(d === N), Val{D}()) +@generated function contiguous_axis_indicator(::StaticInt{N}, ::Val{D}) where {N,D} + t = Expr(:tuple) + for d in 1:D + push!(t.args, Expr(:call, d == N ? :True : :False)) + end + t end function rank_to_sortperm(R::Tuple{Vararg{StaticInt,N}}) where {N}