Skip to content

Commit a021713

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 520c4c1 commit a021713

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
@@ -443,12 +443,12 @@ module IteratorsMD
443443
end
444444
@inline function __inc(state::Tuple{Int,Int,Vararg{Int}}, indices::Tuple{OrdinalRangeInt,OrdinalRangeInt,Vararg{OrdinalRangeInt}})
445445
rng = indices[1]
446-
I = state[1] + step(rng)
447446
if state[1] != last(rng)
447+
I = state[1] + step(rng)
448448
return true, (I, tail(state)...)
449449
end
450-
valid, I = __inc(tail(state), tail(indices))
451-
return valid, (first(rng), I...)
450+
valid, Itail = __inc(tail(state), tail(indices))
451+
return valid, (first(rng), Itail...)
452452
end
453453

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

0 commit comments

Comments
 (0)