Same-shape fast path for restructure(x::Array, y::Array)#497
Merged
ChrisRackauckas merged 1 commit intoJul 23, 2026
Merged
Conversation
`restructure(x::Array, y)` is `reshape(convert(Array, y), size(x)...)`. `reshape` has no same-shape short-circuit (unlike `vec`), so reshaping an array to its own shape still mints a fresh `Array` header (sharing the data) — a per-call heap allocation. Return `y` directly when it already has `x`'s shape. Semantics unchanged: the `::Array` path already returned a data-sharing reshape, so `=== y` preserves the aliasing contract, and a genuine shape change still reshapes. On Julia 1.12 this removes a 32-byte allocation per call; in NonlinearSolve's Newton descent (which restructures an already-shaped `δu` buffer once per iteration) it cuts ~56% of a NonlinearSolveAlg solve's total allocation. Co-Authored-By: Chris Rackauckas <accounts@chrisrackauckas.com> Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01NB3oFTNzGW79UtDt8AyjsM
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #497 +/- ##
==========================================
+ Coverage 60.56% 60.75% +0.19%
==========================================
Files 15 15
Lines 606 609 +3
==========================================
+ Hits 367 370 +3
Misses 239 239 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
ChrisRackauckas
marked this pull request as ready for review
July 23, 2026 23:51
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Add a same-shape fast path to
restructure(x::Array, y::Array): whenyalready hasx's shape, returnydirectly instead of reshaping.Why
restructure(x::Array, y)isreshape(convert(Array, y), size(x)...).reshapehas no same-shape short-circuit (unlikevec, which returns aVectorunchanged), so even reshaping an array to its own exact shape mints a freshArrayheader —reshape(y, size(y)) !== y, 32 bytes on Julia 1.12's Memory-backed arrays. The data buffer is shared (no copy), but the wrapper object is a per-call heap allocation.That per-call allocation shows up in hot loops. In NonlinearSolve's Newton descent, the linear solve already writes its result into the cached
δubuffer and thenrestructures it back to the state shape — for a plainVectorstate that's a no-op that allocates ~once per iteration. On anOrdinaryDiffEqNonlinearSolveAlg(Van der Pol μ=1e5, TRBDF2) that single call was 68% of the solver's per-step allocations; this fast path cuts the solve's total allocation by ~56% with identical results, and it helps every caller that restructures already-correctly-shaped arrays (RecursiveArrayTools, DiffEq, NonlinearSolve, …).Semantics
Unchanged. The
x::Arraypath already returned a data-sharingreshape, so returningy(which shares its own data) preserves the existing aliasing contract;eltype/shape of the result are identical. A genuine shape change (size(x) != size(y), e.g. vector → matrix) still reshapes exactly as before. Type-stable: for differingndimsthe size comparison is a compile-timefalse, so the reshape branch is always taken.Patch bump (7.28.0 → 7.28.1); test added to the
restructuretestset.Draft — please ignore until reviewed by @ChrisRackauckas.
🤖 Generated with Claude Code