Skip to content
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

copyto! sometimes copies whole source array when there is possibility of aliasing #50900

Open
LilithHafner opened this issue Aug 13, 2023 · 1 comment
Labels
bug Indicates an unexpected problem or unintended behavior performance Must go faster

Comments

@LilithHafner
Copy link
Member

julia> using OffsetArrays, BenchmarkTools

julia> function _copyto!(dest, desto, src, srco, n)
           for i in 0:n-1
               dest[i + desto] = src[i + srco]
           end
           dest
       end
_copyto! (generic function with 1 method)

julia> x = rand(100_000);

julia> y = OffsetArray(x, 0);

julia> @btime copyto!(x, 20, x, 10, 5);
  20.728 ns (0 allocations: 0 bytes)

julia> @btime _copyto!(x, 20, x, 10, 5);
  18.036 ns (0 allocations: 0 bytes)

julia> @btime copyto!(y, 20, y, 10, 5);
  11.875 μs (2 allocations: 781.30 KiB) # That's microseconds, not nanoseconds!

julia> @btime _copyto!(y, 20, y, 10, 5);
  17.827 ns (0 allocations: 0 bytes)

The issue is that unaliasing checks for possible aliasing, and if present copies the entire source array. This causes major performance losses here. It can also cause bugs when reading from an array has side effects (this is responsible for a test failure in JuliaCollections/SortingAlgorithms.jl#71)

@LilithHafner LilithHafner added bug Indicates an unexpected problem or unintended behavior performance Must go faster labels Aug 13, 2023
@KristofferC KristofferC changed the title copyto! 500x slower than expected in pathological cases copyto! sometimes copies whole source array when there is possibility of aliasing Aug 13, 2023
@KristofferC
Copy link
Member

Retitled this since a number like 500x is a bit meaningless when it can be made into whatever number desired by changing the inputs.

LSchwerdt added a commit to LSchwerdt/SortingAlgorithms.jl that referenced this issue Aug 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Indicates an unexpected problem or unintended behavior performance Must go faster
Projects
None yet
Development

No branches or pull requests

2 participants