Skip to content

Conversation

@jishnub
Copy link
Member

@jishnub jishnub commented Nov 18, 2025

This avoids allocating an extra Array if the reshape is a no-op.

julia> a = ones(2,2);

julia> @btime reshape($a, size($a));
  9.793 ns (1 allocation: 48 bytes)

this PR:

julia> a = ones(2,2);

julia> @btime reshape($a, size($a));
  4.534 ns (0 allocations: 0 bytes)

@jishnub jishnub changed the title Short-circuit reshape for Array if result is identical Short-circuit reshape for Array if the result has the same shape Nov 18, 2025
@jishnub jishnub added performance Must go faster arrays [a, r, r, a, y, s] labels Nov 18, 2025
@adienes
Copy link
Member

adienes commented Nov 18, 2025

it looks like it used to be implemented this way and was intentionally changed in #56236 , ref comment

## reshape(::Array, ::Dims) returns a new Array (to avoid conditionally aliasing the structure, only the data)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

arrays [a, r, r, a, y, s] performance Must go faster

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants