-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Assume size is non-negative for increased efficiency #50530
Conversation
Can't we add an assert on |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SGTM
Removing the dispatch on julia> axes(1:∞, 1)
OneToInf()
julia> axes(1:∞, 1)[1]
1 whereas on this PR julia> axes(1:∞, 1)
Base.OneTo(ℵ₀)
julia> axes(1:∞, 1)[1]
ERROR: MethodError: no method matching Infinities.InfiniteCardinal{0}(::Int64) |
I'm not sure what the point of julia> using BenchmarkTools
[ Info: Precompiling BenchmarkTools [6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf]
julia> function f(x)
@assert x > 0
x
end
f (generic function with 1 method)
julia> @btime f(x) setup=(x=rand())
3.083 ns (0 allocations: 0 bytes)
0.21786033077166012
julia> function g(x)
x
end
g (generic function with 1 method)
julia> @btime g(x) setup=(x=rand())
1.500 ns (0 allocations: 0 bytes)
0.7776567415362038
Together, I don't think that breakage is sufficient to block this PR. @vtjnash, do you agree? |
Asserts are a debugging tool/testing tool. In the C code they do nothing when disabled. We should probably disable the julia level assertions if the C ones aren't on, because they are there to enforce invariants in |
Still LGTM |
julia> Base.OneTo(ℵ₀) |> typeof |> supertype
AbstractUnitRange{InfiniteCardinal{0}} This appears to be a range with elements of type |
Or |
I noticed here that
lastindex(x::Base.OneTo)
is not simplyx.stop
. This PR performs that optimization and many more by assumingsize
always returns positive numbers.Also removed
axes(r::AbstractRange) = (oneto(length(r)),)
(added in #40382, @vtjnash) as redundant with the generalaxes
method.The obvious downside here is that if someone defines an object with negative size, its axes will include Base.OneTo with negative stop. I think that is acceptable, but if not, we can gate this optimization to a set of known types (all AbstractArray types defined in Base should have non-negative size)