Skip to content

Commit 2d6e298

Browse files
committed
SnoopPrecompile common methods
1 parent 6bfbcf7 commit 2d6e298

File tree

4 files changed

+64
-32
lines changed

4 files changed

+64
-32
lines changed

Project.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
name = "ArrayInterface"
22
uuid = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9"
3-
version = "6.0.24"
3+
version = "6.0.25"
44

55
[deps]
66
ArrayInterfaceCore = "30b0a656-2188-435a-8636-2ec0e6a096e2"
77
Compat = "34da2185-b29b-5c13-b0c7-acf172513d20"
88
IfElse = "615f187c-cbe4-4ef1-ba3b-2fcf58d6d173"
99
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
10+
SnoopPrecompile = "66db9d55-30c0-4569-8b51-7e840670fc0c"
1011
Static = "aedffcd0-7271-4cad-89d0-dc628f76c6d3"
1112

1213
[compat]
1314
ArrayInterfaceCore = "0.1.3"
1415
Compat = "3, 4"
1516
IfElse = "0.1"
17+
SnoopPrecompile = "1"
1618
Static = "0.8"
1719
julia = "1.6"
1820

lib/ArrayInterfaceCore/Project.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
name = "ArrayInterfaceCore"
22
uuid = "30b0a656-2188-435a-8636-2ec0e6a096e2"
3-
version = "0.1.28"
3+
version = "0.1.29"
44

55
[deps]
66
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
7+
SnoopPrecompile = "66db9d55-30c0-4569-8b51-7e840670fc0c"
78
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
89
SuiteSparse = "4607b0f0-06f3-5cda-b6b1-a6196a1729e9"
910

1011
[compat]
1112
ArrayInterface = "6"
13+
SnoopPrecompile = "1"
1214
julia = "1.6"
1315

1416
[extras]

lib/ArrayInterfaceCore/src/ArrayInterfaceCore.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1051,4 +1051,15 @@ stride_preserving_index(@nospecialize T::Type{<:Number}) = true
10511051
end
10521052
stride_preserving_index(@nospecialize T::Type) = false
10531053

1054+
using SnoopPrecompile
1055+
@precompile_setup begin
1056+
# Putting some things in `setup` can reduce the size of the
1057+
# precompile file and potentially make loading faster.
1058+
arrays = [rand(4), Base.oneto(5)]
1059+
@precompile_all_calls begin for x in arrays
1060+
known_first(x)
1061+
known_step(x)
1062+
known_last(x)
1063+
end end
1064+
end
10541065
end # module

src/ArrayInterface.jl

Lines changed: 47 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,43 @@ module ArrayInterface
22

33
using ArrayInterfaceCore
44
import ArrayInterfaceCore: allowed_getindex, allowed_setindex!, aos_to_soa, buffer,
5-
parent_type, fast_matrix_colors, findstructralnz, has_sparsestruct,
6-
issingular, isstructured, matrix_colors, restructure, lu_instance,
7-
safevec, zeromatrix, undefmatrix, ColoringAlgorithm, fast_scalar_indexing, parameterless_type,
8-
ndims_index, ndims_shape, is_splat_index, is_forwarding_wrapper, IndicesInfo, childdims,
9-
parentdims, map_tuple_type, flatten_tuples, GetIndex, SetIndex!, defines_strides,
10-
stride_preserving_index
5+
parent_type, fast_matrix_colors, findstructralnz,
6+
has_sparsestruct,
7+
issingular, isstructured, matrix_colors, restructure,
8+
lu_instance,
9+
safevec, zeromatrix, undefmatrix, ColoringAlgorithm,
10+
fast_scalar_indexing, parameterless_type,
11+
ndims_index, ndims_shape, is_splat_index, is_forwarding_wrapper,
12+
IndicesInfo, childdims,
13+
parentdims, map_tuple_type, flatten_tuples, GetIndex, SetIndex!,
14+
defines_strides,
15+
stride_preserving_index
1116

1217
# ArrayIndex subtypes and methods
13-
import ArrayInterfaceCore: ArrayIndex, MatrixIndex, VectorIndex, BidiagonalIndex, TridiagonalIndex
18+
import ArrayInterfaceCore: ArrayIndex, MatrixIndex, VectorIndex, BidiagonalIndex,
19+
TridiagonalIndex
1420
# managing immutables
1521
import ArrayInterfaceCore: ismutable, can_change_size, can_setindex
1622
# constants
1723
import ArrayInterfaceCore: MatAdjTrans, VecAdjTrans, UpTri, LoTri
1824
# device pieces
1925
import ArrayInterfaceCore: AbstractDevice, AbstractCPU, CPUPointer, CPUTuple, CheckParent,
20-
CPUIndex, GPU, can_avx, device
26+
CPUIndex, GPU, can_avx, device
2127

2228
import ArrayInterfaceCore: known_first, known_step, known_last
2329

2430
using Static
2531
using Static: Zero, One, nstatic, eq, ne, gt, ge, lt, le, eachop, eachop_tuple,
26-
permute, invariant_permutation, field_type, reduce_tup, find_first_eq,
27-
OptionallyStaticUnitRange, OptionallyStaticStepRange, OptionallyStaticRange, IntType,
28-
SOneTo, SUnitRange
32+
permute, invariant_permutation, field_type, reduce_tup, find_first_eq,
33+
OptionallyStaticUnitRange, OptionallyStaticStepRange, OptionallyStaticRange,
34+
IntType,
35+
SOneTo, SUnitRange
2936

3037
using IfElse
3138

3239
using Base.Cartesian
3340
using Base: @propagate_inbounds, tail, OneTo, LogicalIndex, Slice, ReinterpretArray,
34-
ReshapedArray, AbstractCartesianIndex
41+
ReshapedArray, AbstractCartesianIndex
3542

3643
using Base.Iterators: Pairs
3744
using LinearAlgebra
@@ -41,11 +48,11 @@ import Compat
4148
_add1(@nospecialize x) = x + oneunit(x)
4249
_sub1(@nospecialize x) = x - oneunit(x)
4350

44-
@generated function merge_tuple_type(::Type{X}, ::Type{Y}) where {X<:Tuple,Y<:Tuple}
45-
Tuple{X.parameters...,Y.parameters...}
51+
@generated function merge_tuple_type(::Type{X}, ::Type{Y}) where {X <: Tuple, Y <: Tuple}
52+
Tuple{X.parameters..., Y.parameters...}
4653
end
4754

48-
abstract type AbstractArray2{T,N} <: AbstractArray{T,N} end
55+
abstract type AbstractArray2{T, N} <: AbstractArray{T, N} end
4956

5057
Base.size(A::AbstractArray2) = map(Int, ArrayInterface.size(A))
5158
Base.size(A::AbstractArray2, dim) = Int(ArrayInterface.size(A, dim))
@@ -54,7 +61,7 @@ function Base.axes(A::AbstractArray2)
5461
is_forwarding_wrapper(A) && return ArrayInterface.axes(parent(A))
5562
throw(ArgumentError("Subtypes of `AbstractArray2` must define an axes method"))
5663
end
57-
function Base.axes(A::AbstractArray2, dim::Union{Symbol,StaticSymbol})
64+
function Base.axes(A::AbstractArray2, dim::Union{Symbol, StaticSymbol})
5865
axes(A, to_dims(A, dim))
5966
end
6067

@@ -64,7 +71,7 @@ function Base.strides(A::AbstractArray2)
6471
end
6572
Base.strides(A::AbstractArray2, dim) = Int(ArrayInterface.strides(A, dim))
6673

67-
function Base.IndexStyle(::Type{T}) where {T<:AbstractArray2}
74+
function Base.IndexStyle(::Type{T}) where {T <: AbstractArray2}
6875
is_forwarding_wrapper(T) ? IndexStyle(parent_type(T)) : IndexCartesian()
6976
end
7077

@@ -94,7 +101,7 @@ end
94101
@inline function _to_cartesian(a, i::IntType)
95102
@inbounds(CartesianIndices(ntuple(dim -> indices(a, dim), Val(ndims(a))))[i])
96103
end
97-
@inline function _to_linear(a, i::Tuple{IntType,Vararg{IntType}})
104+
@inline function _to_linear(a, i::Tuple{IntType, Vararg{IntType}})
98105
_strides2int(offsets(a), size_to_strides(size(a), static(1)), i) + static(1)
99106
end
100107

@@ -106,7 +113,7 @@ Returns `static(true)` if `parent_type(T)` a type unique to `T`.
106113
has_parent(x) = has_parent(typeof(x))
107114
has_parent(::Type{T}) where {T} = _has_parent(parent_type(T), T)
108115
_has_parent(::Type{T}, ::Type{T}) where {T} = False()
109-
_has_parent(::Type{T1}, ::Type{T2}) where {T1,T2} = True()
116+
_has_parent(::Type{T1}, ::Type{T2}) where {T1, T2} = True()
110117

111118
"""
112119
is_lazy_conjugate(::AbstractArray) -> Bool
@@ -132,10 +139,10 @@ Examples
132139
False()
133140
134141
"""
135-
is_lazy_conjugate(::T) where {T<:AbstractArray} = _is_lazy_conjugate(T, False())
142+
is_lazy_conjugate(::T) where {T <: AbstractArray} = _is_lazy_conjugate(T, False())
136143
is_lazy_conjugate(::AbstractArray{<:Real}) = False()
137144

138-
function _is_lazy_conjugate(::Type{T}, isconj) where {T<:AbstractArray}
145+
function _is_lazy_conjugate(::Type{T}, isconj) where {T <: AbstractArray}
139146
Tp = parent_type(T)
140147
if T !== Tp
141148
_is_lazy_conjugate(Tp, isconj)
@@ -144,7 +151,7 @@ function _is_lazy_conjugate(::Type{T}, isconj) where {T<:AbstractArray}
144151
end
145152
end
146153

147-
function _is_lazy_conjugate(::Type{T}, isconj) where {T<:Adjoint}
154+
function _is_lazy_conjugate(::Type{T}, isconj) where {T <: Adjoint}
148155
Tp = parent_type(T)
149156
if T !== Tp
150157
_is_lazy_conjugate(Tp, !isconj)
@@ -161,17 +168,17 @@ Returns a new instance of `collection` with `item` inserted into at the given `i
161168
Base.@propagate_inbounds function insert(collection, index, item)
162169
@boundscheck checkbounds(collection, index)
163170
ret = similar(collection, length(collection) + 1)
164-
@inbounds for i = firstindex(ret):(index-1)
171+
@inbounds for i in firstindex(ret):(index - 1)
165172
ret[i] = collection[i]
166173
end
167174
@inbounds ret[index] = item
168-
@inbounds for i = (index+1):lastindex(ret)
169-
ret[i] = collection[i-1]
175+
@inbounds for i in (index + 1):lastindex(ret)
176+
ret[i] = collection[i - 1]
170177
end
171178
return ret
172179
end
173180

174-
function insert(x::Tuple{Vararg{Any,N}}, index, item) where {N}
181+
function insert(x::Tuple{Vararg{Any, N}}, index, item) where {N}
175182
@boundscheck if !checkindex(Bool, StaticInt{1}():StaticInt{N}(), index)
176183
throw(BoundsError(x, index))
177184
end
@@ -197,7 +204,8 @@ Base.@propagate_inbounds function deleteat(collection::AbstractVector, index)
197204
end
198205
return unsafe_deleteat(collection, index)
199206
end
200-
Base.@propagate_inbounds function deleteat(collection::Tuple{Vararg{Any,N}}, index) where {N}
207+
Base.@propagate_inbounds function deleteat(collection::Tuple{Vararg{Any, N}},
208+
index) where {N}
201209
@boundscheck if !checkindex(Bool, StaticInt{1}():StaticInt{N}(), index)
202210
throw(BoundsError(collection, index))
203211
end
@@ -210,7 +218,7 @@ function unsafe_deleteat(src::AbstractVector, index)
210218
if i < index
211219
dst[i] = src[i]
212220
else
213-
dst[i] = src[i+1]
221+
dst[i] = src[i + 1]
214222
end
215223
end
216224
return dst
@@ -241,8 +249,7 @@ end
241249
end
242250

243251
@inline unsafe_deleteat(x::Tuple{T}, i) where {T} = ()
244-
@inline unsafe_deleteat(x::Tuple{T1,T2}, i) where {T1,T2} =
245-
isone(i) ? (x[2],) : (x[1],)
252+
@inline unsafe_deleteat(x::Tuple{T1, T2}, i) where {T1, T2} = isone(i) ? (x[2],) : (x[1],)
246253
@inline function unsafe_deleteat(x::Tuple, i)
247254
if i === one(i)
248255
return tail(x)
@@ -262,4 +269,14 @@ include("indexing.jl")
262269
include("stridelayout.jl")
263270
include("broadcast.jl")
264271

272+
using SnoopPrecompile
273+
@precompile_setup begin
274+
# Putting some things in `setup` can reduce the size of the
275+
# precompile file and potentially make loading faster.
276+
arrays = [rand(4), Base.oneto(5)]
277+
@precompile_all_calls begin for x in arrays
278+
known_size(x)
279+
known_length(x)
280+
end end
281+
end
265282
end

0 commit comments

Comments
 (0)