Skip to content

Commit 36a4616

Browse files
authored
Increment state conditionally in CartesianIndices iteration (JuliaLang#58742)
Fixes JuliaLang#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.
1 parent 89dfb68 commit 36a4616

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
@@ -472,12 +472,12 @@ module IteratorsMD
472472
end
473473
@inline function __inc(state::Tuple{Int,Int,Vararg{Int}}, indices::Tuple{OrdinalRangeInt,OrdinalRangeInt,Vararg{OrdinalRangeInt}})
474474
rng = indices[1]
475-
I = state[1] + step(rng)
476475
if state[1] != last(rng)
476+
I = state[1] + step(rng)
477477
return true, (I, tail(state)...)
478478
end
479-
valid, I = __inc(tail(state), tail(indices))
480-
return valid, (first(rng), I...)
479+
valid, Itail = __inc(tail(state), tail(indices))
480+
return valid, (first(rng), Itail...)
481481
end
482482

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

0 commit comments

Comments
 (0)