Skip to content

optimize length(::OrdinalRange) for large bit-ints #58864

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

Merged
merged 5 commits into from
Jul 16, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 4 additions & 11 deletions base/range.jl
Original file line number Diff line number Diff line change
Expand Up @@ -805,17 +805,10 @@ let bigints = Union{Int, UInt, Int64, UInt64, Int128, UInt128},
s = step(r)
diff = last(r) - first(r)
isempty(r) && return zero(diff)
# if |s| > 1, diff might have overflowed, but unsigned(diff)÷s should
# therefore still be valid (if the result is representable at all)
# n.b. !(s isa T)
if s isa Unsigned || -1 <= s <= 1 || s == -s
a = div(diff, s) % typeof(diff)
elseif s < 0
a = div(unsigned(-diff), -s) % typeof(diff)
else
a = div(unsigned(diff), s) % typeof(diff)
end
return a + oneunit(a)
# compute `(diff ÷ s) + 1` in a manner robust to overflow
# by using the absolute values as unsigneds for non-empty ranges
a = div(unsigned(flipsign(diff, s)), unsigned(abs(s))) % typeof(diff)
return max(a + oneunit(a), zero(a))
end
function checked_length(r::OrdinalRange{T}) where T<:bigints
s = step(r)
Expand Down