Skip to content

Commit 94a4172

Browse files
authored
Merge pull request #184 from JuliaArrays/teh/inference
A bit of SnoopCompile-inspired maintenance
2 parents 10a7b28 + 6ce6074 commit 94a4172

File tree

5 files changed

+70
-43
lines changed

5 files changed

+70
-43
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ version = "1.5.0"
66
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
77

88
[compat]
9-
Adapt = "2"
9+
Adapt = "2, 3"
1010
julia = "0.7, 1"
1111

1212
[extras]

src/OffsetArrays.jl

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ const ArrayInitializer = Union{UndefInitializer, Missing, Nothing}
2424
OffsetArray(A, indices...)
2525
2626
Return an `AbstractArray` that shares element type and size with the first argument but
27-
uses the supplied `indices` to infer its axes. If all the indices are `AbstractUnitRange`s then
28-
these are directly used as the axis span along each dimension. Refer to the examples below for other
27+
uses the supplied `indices` to infer its axes. If all the indices are `AbstractUnitRange`s then
28+
these are directly used as the axis span along each dimension. Refer to the examples below for other
2929
permissible types.
3030
31-
Alternatively it's possible to specify the coordinates of one corner of the array
32-
and have the axes be computed automatically from the size of `A`.
31+
Alternatively it's possible to specify the coordinates of one corner of the array
32+
and have the axes be computed automatically from the size of `A`.
3333
This constructor makes it convenient to shift to
34-
an arbitrary starting index along each axis, for example to a zero-based indexing scheme followed by
34+
an arbitrary starting index along each axis, for example to a zero-based indexing scheme followed by
3535
arrays in languages such as C and Python.
3636
See [`Origin`](@ref) and the examples below for this usage.
3737
@@ -51,9 +51,9 @@ julia> A[0, 1]
5151
5
5252
```
5353
54-
Examples of range-like types are: `UnitRange` (e.g, `-1:2`), `CartesianIndices`,
55-
and `Colon()` (or concisely `:`). A `UnitRange` specifies the axis span along one particular dimension,
56-
`CartesianIndices` specify the axis spans along multiple dimensions, and a `Colon` is a placeholder
54+
Examples of range-like types are: `UnitRange` (e.g, `-1:2`), `CartesianIndices`,
55+
and `Colon()` (or concisely `:`). A `UnitRange` specifies the axis span along one particular dimension,
56+
`CartesianIndices` specify the axis spans along multiple dimensions, and a `Colon` is a placeholder
5757
that specifies that the `OffsetArray` shares its axis with its parent along that dimension.
5858
5959
```jldoctest; setup=:(using OffsetArrays)
@@ -86,9 +86,9 @@ ERROR: [...]
8686
8787
# Example: origin
8888
89-
[`OffsetArrays.Origin`](@ref) may be used to specify the origin of the OffsetArray. The term origin here
90-
refers to the corner with the lowest values of coordinates, such as the left edge for an `AbstractVector`,
91-
the bottom left corner for an `AbstractMatrix` and so on. The coordinates of the origin sets the starting
89+
[`OffsetArrays.Origin`](@ref) may be used to specify the origin of the OffsetArray. The term origin here
90+
refers to the corner with the lowest values of coordinates, such as the left edge for an `AbstractVector`,
91+
the bottom left corner for an `AbstractMatrix` and so on. The coordinates of the origin sets the starting
9292
index of the array along each dimension.
9393
9494
```jldoctest; setup=:(using OffsetArrays)
@@ -150,7 +150,7 @@ end
150150
OffsetArray{eltype(A), ndims(A), typeof(A)}(A, offsets)
151151
end
152152

153-
# These methods are necessary to disallow incompatible dimensions for
153+
# These methods are necessary to disallow incompatible dimensions for
154154
# the OffsetVector and the OffsetMatrix constructors
155155
for (FT, ND) in ((:OffsetVector, :1), (:OffsetMatrix, :2))
156156
@eval @inline function $FT(A::AbstractArray{<:Any,$ND}, offsets::Tuple{Vararg{Integer}})
@@ -185,7 +185,7 @@ for FT in (:OffsetArray, :OffsetVector, :OffsetMatrix)
185185
lA = size(A)
186186
lI = map(length, inds)
187187
lA == lI || throw_dimerr(lA, lI)
188-
$FT(A, map(_offset, axes(A), inds))
188+
$FT(A, map(_offset, axes(A), inds))
189189
end
190190

191191
@eval @inline $FT(A::AbstractArray, inds::Vararg) = $FT(A, inds)
@@ -372,8 +372,8 @@ function Base.inds2string(inds::Tuple{Vararg{Union{IdOffsetRange, IdentityUnitRa
372372
Base.inds2string(map(UnitRange, inds))
373373
end
374374
Base.showindices(io::IO, ind1::IdOffsetRange, inds::IdOffsetRange...) = Base.showindices(io, map(UnitRange, (ind1, inds...))...)
375-
376-
function Base.showarg(io::IO, a::OffsetArray, toplevel)
375+
376+
function Base.showarg(io::IO, @nospecialize(a::OffsetArray), toplevel)
377377
print(io, "OffsetArray(")
378378
Base.showarg(io, parent(a), false)
379379
Base.showindices(io, axes(a)...)
@@ -509,4 +509,9 @@ end
509509
import Adapt
510510
Adapt.adapt_structure(to, x::OffsetArray) = OffsetArray(Adapt.adapt(to, parent(x)), x.offsets)
511511

512+
if Base.VERSION >= v"1.4.2"
513+
include("precompile.jl")
514+
_precompile_()
515+
end
516+
512517
end # module

src/axes.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,3 +178,10 @@ if VERSION < v"1.5.2"
178178
@inline Base.compute_offset1(parent, stride1::Integer, dims::Tuple{Int}, inds::Tuple{IdOffsetRange}, I::Tuple) =
179179
Base.compute_linindex(parent, I) - stride1*first(inds[1])
180180
end
181+
182+
# This was deemed "too private" to extend: see issue #184
183+
# # Fixes an inference failure in Base.mapfirst!
184+
# # Test: A = OffsetArray(rand(4,4), (-3,5)); R = similar(A, (1:1, 6:9)); maximum!(R, A)
185+
# if isdefined(Base, :_firstslice)
186+
# Base._firstslice(i::IdOffsetRange) = IdOffsetRange(Base._firstslice(i.parent), i.offset)
187+
# end

src/precompile.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function _precompile_()
2+
ccall(:jl_generating_output, Cint, ()) == 1 || return nothing
3+
Base.precompile(Tuple{typeof(Base.showarg),IOBuffer,OffsetArray{Int, 0, Array{Int, 0}},Bool}) # time: 0.037824474
4+
Base.precompile(Tuple{Type{IdOffsetRange{Int, Base.OneTo{Int}}},UnitRange{Int}}) # time: 0.009825722
5+
Base.precompile(Tuple{typeof(Base.inds2string),Tuple{IdOffsetRange{Int, Base.OneTo{Int}}, IdOffsetRange{Int, Base.OneTo{Int}}}}) # time: 0.0080779
6+
Base.precompile(Tuple{typeof(getindex),StepRangeLen{Float64, Base.TwicePrecision{Float64}, Base.TwicePrecision{Float64}},IdentityUnitRange{UnitRange{Int}}}) # time: 0.008055889
7+
Base.precompile(Tuple{typeof(getindex),StepRangeLen{Int, Int, Int},IdentityUnitRange{UnitRange{Int}}}) # time: 0.006584347
8+
Base.precompile(Tuple{typeof(getindex),StepRange{Int, Int},IdentityUnitRange{UnitRange{Int}}}) # time: 0.00650066
9+
Base.precompile(Tuple{typeof(getindex),LinRange{Float64},IdentityUnitRange{UnitRange{Int}}}) # time: 0.005844317
10+
Base.precompile(Tuple{typeof(zeros),Tuple{IdOffsetRange{Int, Base.OneTo{Int}}}}) # time: 0.007713056
11+
Base.precompile(Tuple{typeof(ones),Tuple{IdOffsetRange{Int, Base.OneTo{Int}}}}) # time: 0.007713056
12+
Base.precompile(Tuple{typeof(trues),Tuple{UnitRange{Int}, UnitRange{Int}}}) # time: 0.005478372
13+
Base.precompile(Tuple{typeof(falses),Tuple{UnitRange{Int}, UnitRange{Int}}}) # time: 0.005478372
14+
Base.precompile(Tuple{typeof(firstindex),IdOffsetRange{Int, Base.OneTo{Int}}}) # time: 0.004100289
15+
end

test/runtests.jl

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -198,24 +198,24 @@ end
198198

199199
@testset "OffsetVector" begin
200200
# initialization
201-
one_based_axes = [
202-
(Base.OneTo(4), ),
203-
(1:4, ),
204-
(CartesianIndex(1):CartesianIndex(4), ),
205-
(IdentityUnitRange(1:4), ),
206-
(IdOffsetRange(1:4),),
201+
one_based_axes = Any[
202+
(Base.OneTo(4), ),
203+
(1:4, ),
204+
(CartesianIndex(1):CartesianIndex(4), ),
205+
(IdentityUnitRange(1:4), ),
206+
(IdOffsetRange(1:4),),
207207
(IdOffsetRange(3:6, -2),)
208208
]
209209

210-
offset_axes = [
211-
(-1:2, ),
212-
(CartesianIndex(-1):CartesianIndex(2), ),
213-
(IdentityUnitRange(-1:2), ),
214-
(IdOffsetRange(-1:2),),
210+
offset_axes = Any[
211+
(-1:2, ),
212+
(CartesianIndex(-1):CartesianIndex(2), ),
213+
(IdentityUnitRange(-1:2), ),
214+
(IdOffsetRange(-1:2),),
215215
(IdOffsetRange(3:6, -4),)
216216
]
217217

218-
for inds in [size.(one_based_axes[1], 1), one_based_axes...]
218+
for inds in Any[size.(one_based_axes[1], 1), one_based_axes...]
219219
# test indices API
220220
a = OffsetVector{Float64}(undef, inds)
221221
@test eltype(a) === Float64
@@ -248,7 +248,7 @@ end
248248
a = OffsetVector{Missing}(missing, inds)
249249
@test axes(a) === ax
250250

251-
for (T, t) in [(Nothing, nothing), (Missing, missing)]
251+
for (T, t) in Any[(Nothing, nothing), (Missing, missing)]
252252
a = OffsetVector{Union{T, Vector{Int}}}(undef, inds)
253253
@test !isassigned(a, -1)
254254
@test eltype(a) === Union{T, Vector{Int}}
@@ -282,7 +282,7 @@ end
282282
# nested offset array
283283
a = rand(4)
284284
oa = OffsetArray(a, -1)
285-
for inds in [.-oa.offsets, one_based_axes...]
285+
for inds in Any[.-oa.offsets, one_based_axes...]
286286
ooa = OffsetArray(oa, inds)
287287
@test typeof(parent(ooa)) <: Vector
288288
@test ooa === OffsetArray(oa, inds...) === OffsetVector(oa, inds) === OffsetVector(oa, inds...)
@@ -324,8 +324,8 @@ end
324324

325325
@testset "OffsetMatrix" begin
326326
# initialization
327-
328-
one_based_axes = [
327+
328+
one_based_axes = Any[
329329
(Base.OneTo(4), Base.OneTo(3)),
330330
(1:4, 1:3),
331331
(CartesianIndex(1, 1):CartesianIndex(4, 3), ),
@@ -338,7 +338,7 @@ end
338338
(IdOffsetRange(3:6, -2), 1:3),
339339
]
340340

341-
offset_axes = [
341+
offset_axes = Any[
342342
(-1:2, 0:2),
343343
(CartesianIndex(-1, 0):CartesianIndex(2, 2), ),
344344
(-1:2, CartesianIndex(0):CartesianIndex(2)),
@@ -351,7 +351,7 @@ end
351351
(IdOffsetRange(-1:2), 0:2),
352352
]
353353

354-
for inds in [size.(one_based_axes[1], 1), one_based_axes...]
354+
for inds in Any[size.(one_based_axes[1], 1), one_based_axes...]
355355
# test API
356356
a = OffsetMatrix{Float64}(undef, inds)
357357
ax = (IdOffsetRange(Base.OneTo(4), 0), IdOffsetRange(Base.OneTo(3), 0))
@@ -385,7 +385,7 @@ end
385385
a = OffsetMatrix{Missing}(missing, inds)
386386
@test axes(a) === ax
387387

388-
for (T, t) in [(Nothing, nothing), (Missing, missing)]
388+
for (T, t) in Any[(Nothing, nothing), (Missing, missing)]
389389
a = OffsetMatrix{Union{T, Vector{Int}}}(undef, inds)
390390
@test !isassigned(a, -1, 0)
391391
@test eltype(a) === Union{T, Vector{Int}}
@@ -421,7 +421,7 @@ end
421421
# nested offset array
422422
a = rand(4, 3)
423423
oa = OffsetArray(a, -1, -2)
424-
for inds in [.-oa.offsets, one_based_axes...]
424+
for inds in Any[.-oa.offsets, one_based_axes...]
425425
ooa = OffsetArray(oa, inds)
426426
@test ooa === OffsetArray(oa, inds...) === OffsetMatrix(oa, inds) === OffsetMatrix(oa, inds...)
427427
@test typeof(parent(ooa)) <: Matrix
@@ -496,13 +496,13 @@ end
496496
@testset "convenience constructors" begin
497497
ax = (2:3, 4:5)
498498

499-
for f in [zeros, ones]
499+
for f in (zeros, ones)
500500
a = f(Float64, ax)
501501
@test axes(a) == ax
502502
@test eltype(a) == Float64
503503
end
504504

505-
for f in [trues, falses]
505+
for f in (trues, falses)
506506
a = f(ax)
507507
@test axes(a) == ax
508508
@test eltype(a) == Bool
@@ -573,7 +573,7 @@ end
573573
end
574574
@testset "TupleOfRanges" begin
575575
Base.to_indices(A, inds, t::Tuple{TupleOfRanges{N}}) where {N} = t
576-
OffsetArrays.AxisConversionStyle(::Type{TupleOfRanges{N}}) where {N} =
576+
OffsetArrays.AxisConversionStyle(::Type{TupleOfRanges{N}}) where {N} =
577577
OffsetArrays.TupleOfRanges()
578578

579579
Base.convert(::Type{Tuple{Vararg{AbstractUnitRange{Int}}}}, t::TupleOfRanges) = t.x
@@ -584,7 +584,7 @@ end
584584
@test axes(oa) == inds.x
585585
end
586586
@testset "NewColon" begin
587-
Base.to_indices(A, inds, t::Tuple{NewColon,Vararg{Any}}) =
587+
Base.to_indices(A, inds, t::Tuple{NewColon,Vararg{Any}}) =
588588
(_uncolon(inds, t), to_indices(A, Base.tail(inds), Base.tail(t))...)
589589

590590
_uncolon(inds::Tuple{}, I::Tuple{NewColon, Vararg{Any}}) = OneTo(1)
@@ -598,7 +598,7 @@ end
598598

599599
@testset "Offset range construction" begin
600600
r = -2:5
601-
for AT in [OffsetArray, OffsetVector]
601+
for AT in Any[OffsetArray, OffsetVector]
602602
y = AT(r, r)
603603
@test axes(y) == (r,)
604604
@test step(y) == step(r)
@@ -1236,8 +1236,8 @@ end
12361236
@test sort(A, dims = 1) == OffsetArray(sort(parent(A), dims = 1), A.offsets)
12371237
@test sort(A, dims = 2) == OffsetArray(sort(parent(A), dims = 2), A.offsets)
12381238

1239-
@test mapslices(v->sort(v), A, dims = 1) == OffsetArray(mapslices(v->sort(v), parent(A), dims = 1), A.offsets)
1240-
@test mapslices(v->sort(v), A, dims = 2) == OffsetArray(mapslices(v->sort(v), parent(A), dims = 2), A.offsets)
1239+
@test mapslices(sort, A, dims = 1) == OffsetArray(mapslices(sort, parent(A), dims = 1), A.offsets)
1240+
@test mapslices(sort, A, dims = 2) == OffsetArray(mapslices(sort, parent(A), dims = 2), A.offsets)
12411241
end
12421242

12431243
@testset "rot/reverse" begin

0 commit comments

Comments
 (0)