Skip to content

Commit a415836

Browse files
authored
Merge pull request #58 from vchuravy/vc/range
micro-optimise axes
2 parents e814c7a + 668c62b commit a415836

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

src/OffsetArrays.jl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,17 @@ Base.size(A::OffsetArray, d) = size(parent(A), d)
6868
# performance-critical and relies on axes, these are usually worth
6969
# optimizing thoroughly.
7070
@inline Base.axes(A::OffsetArray, d) =
71-
1 <= d <= length(A.offsets) ? Base.Slice(axes(parent(A))[d] .+ A.offsets[d]) : (1:1)
71+
1 <= d <= length(A.offsets) ? _slice(axes(parent(A))[d], A.offsets[d]) : (1:1)
7272
@inline Base.axes(A::OffsetArray) =
7373
_axes(axes(parent(A)), A.offsets) # would rather use ntuple, but see #15276
7474
@inline _axes(inds, offsets) =
75-
(Base.Slice(inds[1] .+ offsets[1]), _axes(tail(inds), tail(offsets))...)
75+
(_slice(inds[1], offsets[1]), _axes(tail(inds), tail(offsets))...)
7676
_axes(::Tuple{}, ::Tuple{}) = ()
7777
Base.axes1(A::OffsetArray{T,0}) where {T} = 1:1 # we only need to specialize this one
7878

79+
# Avoid the kw-arg on the range(r+x, length=length(r)) call in r .+ x
80+
@inline _slice(r, x) = Base.Slice(Base._range(first(r) + x, nothing, nothing, length(r)))
81+
7982
const OffsetAxis = Union{Integer, UnitRange, Base.Slice{<:UnitRange}, Base.OneTo}
8083
function Base.similar(A::OffsetArray, ::Type{T}, dims::Dims) where T
8184
B = similar(parent(A), T, dims)

0 commit comments

Comments
 (0)