Skip to content

Commit 5d94660

Browse files
jishnubKristofferC
authored andcommitted
Increment state conditionally in CartesianIndices iteration (#58742)
Fixes #53430 ```julia julia> a = rand(100,100); b = similar(a); av = view(a, axes(a)...); bv = view(b, axes(b)...); bv2 = view(b, UnitRange.(axes(b))...); julia> @Btime copyto!($bv2, $av); # slow, indices are UnitRanges 12.352 μs (0 allocations: 0 bytes) # master, v"1.13.0-DEV.745" 1.662 μs (0 allocations: 0 bytes) # this PR julia> @Btime copyto!($bv, $av); # reference 1.733 μs (0 allocations: 0 bytes) ``` The performances become comparable after this PR. I've also renamed the second `I` to `Itail`, as the two variables represent different quantities. (cherry picked from commit 36a4616)
1 parent f650e9f commit 5d94660

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

base/multidimensional.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -446,12 +446,12 @@ module IteratorsMD
446446
end
447447
@inline function __inc(state::Tuple{Int,Int,Vararg{Int}}, indices::Tuple{OrdinalRangeInt,OrdinalRangeInt,Vararg{OrdinalRangeInt}})
448448
rng = indices[1]
449-
I = state[1] + step(rng)
450449
if state[1] != last(rng)
450+
I = state[1] + step(rng)
451451
return true, (I, tail(state)...)
452452
end
453-
valid, I = __inc(tail(state), tail(indices))
454-
return valid, (first(rng), I...)
453+
valid, Itail = __inc(tail(state), tail(indices))
454+
return valid, (first(rng), Itail...)
455455
end
456456

457457
# 0-d cartesian ranges are special-cased to iterate once and only once

0 commit comments

Comments
 (0)