Skip to content

Commit a4f40eb

Browse files
Lilith HafnerLilith Hafner
authored andcommitted
Assume length is non-negative for increased efficiency
1 parent 4995d3f commit a4f40eb

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

base/abstractarray.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ julia> axes(A)
9595
"""
9696
function axes(A)
9797
@inline
98-
map(oneto, size(A))
98+
map(unchecked_oneto, size(A))
9999
end
100100

101101
"""

base/range.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ distinction that the lower limit is guaranteed (by the type system) to
447447
be 1.
448448
"""
449449
struct OneTo{T<:Integer} <: AbstractUnitRange{T}
450-
stop::T
450+
stop::T # invariant: stop >= zero(stop)
451451
function OneTo{T}(stop) where {T<:Integer}
452452
throwbool(r) = (@noinline; throw(ArgumentError("invalid index: $r of type Bool")))
453453
T === Bool && throwbool(stop)
@@ -463,6 +463,8 @@ struct OneTo{T<:Integer} <: AbstractUnitRange{T}
463463
T === Bool && throwbool(r)
464464
return new(max(zero(T), last(r)))
465465
end
466+
467+
global unchecked_oneto(stop::Integer) = new{typeof(stop)}(stop)
466468
end
467469
OneTo(stop::T) where {T<:Integer} = OneTo{T}(stop)
468470
OneTo(r::AbstractRange{T}) where {T<:Integer} = OneTo{T}(r)
@@ -703,8 +705,6 @@ step(r::LinRange) = (last(r)-first(r))/r.lendiv
703705
step_hp(r::StepRangeLen) = r.step
704706
step_hp(r::AbstractRange) = step(r)
705707

706-
axes(r::AbstractRange) = (oneto(length(r)),)
707-
708708
# Needed to ensure `has_offset_axes` can constant-fold.
709709
has_offset_axes(::StepRange) = false
710710

0 commit comments

Comments
 (0)