Commit 7de5585
This performance difference was found when working on #42736.
Currently, our `ReshapedArray` use stride based `MultiplicativeInverse`
to speed up index transformation.
For example, for `a::AbstractArray{T,3}` and `b = vec(a)`, the index
transformation is equivalent to:
```julia
offset = i - 1 # b[i]
d1, r1 = divrem(offset, stride(a, 3)) # stride(a, 3) = size(a, 1) * size(a, 2)
d2, r2 = divrem(r1, stride(a, 2)) # stride(a, 2) = size(a, 1)
CartesianIndex(r2 + 1, d2 +1, d1 + 1) # a has one-based axes
```
(All the `stride` is replaced with a `MultiplicativeInverse` to
accelerate `divrem`)
This PR wants to replace the above machinery with:
```julia
offset = i - 1
d1, r1 = divrem(offset, size(a, 1))
d2, r2 = divrem(d1, size(a, 2))
CartesianIndex(r1 + 1, r2 +1, d2 + 1)
```
For random access, they should have the same computational cost. But for
sequential access, like `sum(b)`, `size` based transformation seems
faster.
To avoid bottleneck from IO, use `reshape(::CartesianIndices, x...)` to
benchmark:
```julia
f(x) = let r = 0
for i in eachindex(x)
@inbounds r |= +(x[i].I...)
end
r
end
a = CartesianIndices((99,100,101));
@Btime f(vec($a)); #2.766 ms --> 2.591 ms
@Btime f(reshape($a,990,1010)); #3.412 ms --> 2.626 ms
@Btime f(reshape($a,33,300,101)); #3.422 ms --> 2.342 ms
```
I haven't looked into the reason for this performance difference.
Beside acceleration, this also makes it possible to reuse the
`MultiplicativeInverse` in some cases (like #42736).
So I think it might be useful?
---------
Co-authored-by: Andy Dienes <[email protected]>
Co-authored-by: Andy Dienes <[email protected]>
1 parent 078e4ed commit 7de5585
3 files changed
+23
-20
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3116 | 3116 | | |
3117 | 3117 | | |
3118 | 3118 | | |
3119 | | - | |
3120 | | - | |
| 3119 | + | |
| 3120 | + | |
3121 | 3121 | | |
3122 | 3122 | | |
| 3123 | + | |
| 3124 | + | |
3123 | 3125 | | |
3124 | 3126 | | |
3125 | | - | |
3126 | | - | |
3127 | 3127 | | |
3128 | 3128 | | |
3129 | 3129 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
673 | 673 | | |
674 | 674 | | |
675 | 675 | | |
676 | | - | |
677 | | - | |
678 | | - | |
679 | | - | |
| 676 | + | |
| 677 | + | |
| 678 | + | |
| 679 | + | |
| 680 | + | |
| 681 | + | |
| 682 | + | |
| 683 | + | |
680 | 684 | | |
681 | | - | |
682 | | - | |
683 | | - | |
| 685 | + | |
| 686 | + | |
684 | 687 | | |
685 | 688 | | |
686 | 689 | | |
687 | 690 | | |
688 | 691 | | |
689 | 692 | | |
690 | | - | |
| 693 | + | |
691 | 694 | | |
692 | 695 | | |
693 | 696 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
234 | 234 | | |
235 | 235 | | |
236 | 236 | | |
237 | | - | |
238 | | - | |
239 | | - | |
240 | | - | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
241 | 241 | | |
242 | 242 | | |
243 | 243 | | |
| |||
269 | 269 | | |
270 | 270 | | |
271 | 271 | | |
272 | | - | |
| 272 | + | |
273 | 273 | | |
274 | | - | |
275 | | - | |
276 | | - | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
277 | 277 | | |
278 | 278 | | |
279 | 279 | | |
| |||
0 commit comments