Fully drop _tuple_any and improve type-based offset axes check.#45260
Fully drop _tuple_any and improve type-based offset axes check.#45260vtjnash merged 7 commits intoJuliaLang:masterfrom
_tuple_any and improve type-based offset axes check.#45260Conversation
8c42ee9 to
8dd18c3
Compare
9d37b62 to
8a02e52
Compare
base/range.jl
Outdated
|
|
||
| # Needed to ensure `has_offset_axes` can constant-fold. | ||
| has_offset_axes(::StepRange) = false | ||
| let baseints = Union{Int8,UInt8,Int16,UInt16,Int32,UInt32,Int64,UInt64,Int128,UInt128} |
There was a problem hiding this comment.
| let baseints = Union{Int8,UInt8,Int16,UInt16,Int32,UInt32,Int64,UInt64,Int128,UInt128} | |
| let baseints = BitInteger |
There was a problem hiding this comment.
BitInteger is defined later in int.jl. Maybe we can replace it with Union{bigints, smallints}
base/range.jl
Outdated
| has_offset_axes(::StepRange) = false | ||
| let baseints = Union{Int8,UInt8,Int16,UInt16,Int32,UInt32,Int64,UInt64,Int128,UInt128} | ||
| global firstindex | ||
| firstindex(::StepRange{T,<:baseints}) where {T<:baseints} = sizeof(T) < sizeof(Int) ? 1 : one(T) |
There was a problem hiding this comment.
T might be a Union and not have a size?
There was a problem hiding this comment.
Well, we should use sizeof(first(r)) instead.
The new length has similar problem.
To make sure the result is correct, we'd better split L694 into:
firstindex(r::StepRange{<:bigints,<:BitInteger}) = one(last(r)-first(r))
firstindex(::StepRange{<:smallints,<:BitInteger}) = 1| s = step(r) | ||
| isempty(r) && return 0 | ||
| return div(Int(last(r)) - Int(first(r)), s) + 1 | ||
| return Int(div(Int(last(r)) - Int(first(r)), s)) + 1 |
There was a problem hiding this comment.
Are we sure it is legal to cast this to an Int? This seems highly unrelated to the commit message
There was a problem hiding this comment.
This is for length(::StepRange{Int8,Int128}). Without this, we'll return a Int128 for non-empty r.
I believe it's safe as we convert the div result rather than step s. Int should be enough to represent the length
987a9b0 to
ab36599
Compare
`_any_tuple` should be functionally consistent. (And has no bootstrap problem.)
And define some "cheap" `firstindex`
This avoid some unneeded `axes` call, thus more possible be folded by the compiler.
Co-Authored-By: Jameson Nash <vtjnash+github@gmail.com>
And define `firstindex` accordingly. Co-Authored-By: Jameson Nash <vtjnash+github@gmail.com>
* Follow up to JuliaLang#45236 (make `length(::StepRange{Int8,Int128})` type-stable) * Fully drop `_tuple_any` (unneeded now) * Make sure `has_offset_axes(::StepRange)` could be const folded. And define some "cheap" `firstindex` * Do offset axes check on `A`'s parent rather than itself. This avoid some unneeded `axes` call, thus more possible be folded by the compiler. Co-authored-by: Jameson Nash <vtjnash+github@gmail.com>
* Follow up to JuliaLang#45236 (make `length(::StepRange{Int8,Int128})` type-stable) * Fully drop `_tuple_any` (unneeded now) * Make sure `has_offset_axes(::StepRange)` could be const folded. And define some "cheap" `firstindex` * Do offset axes check on `A`'s parent rather than itself. This avoid some unneeded `axes` call, thus more possible be folded by the compiler. Co-authored-by: Jameson Nash <vtjnash+github@gmail.com>
Base._tuple_anyis only used inBase.has_offset_axes.After #44063, we have a similar method
_any_tuple, which should be functionally consistent for offset axes check.This PR fully removes
_tuple_any, and makeBase.has_offset_axescall_any_tuple.(Ideally, we'd better call public api
anyinstead. But our compiler fails to fold some check as a const.)This PR also omits some
axescall inhas_offset_axesby checkingindices/parent'saxesrather than self'saxes.This helps #44040 a little.
Test added.