(feat): add reshape! — zero-allocation array reshaping via pool wrapper cache#21
Merged
(feat): add reshape! — zero-allocation array reshaping via pool wrapper cache#21
reshape! — zero-allocation array reshaping via pool wrapper cache#21Conversation
…ape! - @with_pool function with mixed acquire!/reshape!/zeros! scenarios - Zero-allocation test for the combined function pattern (v1.11+) - @maybe_with_pool pooling vs no-pooling allocation comparison proving pool is the source of zero-alloc (alloc==0 vs alloc>0)
… cache
- Export reshape!(pool, A, dims...) public API with type touch recording
- _reshape_impl! with setfield!-based wrapper reuse (Julia 1.11+):
same-dim updates size in-place, cross-dim claims slot + reuses cached wrapper
- _claim_slot! for wrapper-only slot reservation (no backing memory)
- Macro integration: type extraction, _RESHAPE_IMPL_REF call transformation
- DisabledPool{:cpu} fallback delegates to Base.reshape
- Include test_reshape.jl in both v1.11+ and legacy test paths
- README, api.md, api-essentials.md, quick-start.md: add reshape! entries - api-essentials.md: new "Reshaping with reshape!" section with example - test_reshape.jl: assert external data unchanged after @with_pool call
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #21 +/- ##
==========================================
- Coverage 95.38% 95.21% -0.18%
==========================================
Files 13 13
Lines 1756 1818 +62
==========================================
+ Hits 1675 1731 +56
- Misses 81 87 +6
🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull request overview
Adds a new reshape!(pool, A, dims...) convenience API that uses the pool’s cached N-D wrappers to avoid repeated reshape-wrapper allocations (notably enabling zero-allocation cross-dimensional reshapes on Julia ≥ 1.11 after warmup).
Changes:
- Introduces
_claim_slot!and anAdaptiveArrayPool-specialized_reshape_impl!that reuses cachedArray{T,N}wrappers viasetfield!. - Adds the public
reshape!API (with macro integration via_reshape_impl!rewriting) and DisabledPool fallbacks. - Adds extensive tests and updates user docs/README/API reference to include
reshape!.
Reviewed changes
Copilot reviewed 10 out of 10 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
src/acquire.jl |
Adds wrapper-only slot claiming and the Julia ≥ 1.11 zero-allocation reshape implementation using cached wrappers. |
src/convenience.jl |
Introduces the public reshape! API + fallback _reshape_impl! and DisabledPool support. |
src/macros.jl |
Extends type-extraction and call-rewriting so reshape! participates in typed/lazy macro optimizations. |
src/AdaptiveArrayPools.jl |
Exports reshape! as part of the public API. |
test/test_reshape.jl |
New test suite covering correctness, error cases, macro integration, and allocation behavior (version-gated). |
test/runtests.jl |
Includes test_reshape.jl in the test run. |
docs/src/reference/api.md |
Adds reshape! to the API reference table. |
docs/src/basics/quick-start.md |
Adds reshape! to the quick-start convenience table. |
docs/src/basics/api-essentials.md |
Adds a reshape! section and a quick-reference table entry. |
README.md |
Documents reshape! in the top-level convenience-function table. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
…ocs and edge cases - Extract `_warn_pool_growing`/@noinline + `_check_pool_growth` helper to deduplicate pool growth warning logic and reduce hot-path code size - Add growth warning to `_claim_slot!` (mirrors `get_view!` behavior) - Use zero-length dims for cache-miss wrapper to avoid orphaned 1-element buffer - Add negative-dim guard to fallback `_reshape_impl!` (Julia 1.10 parity) - Update `reshape!` docstring with in-place mutation and scope-escape caveats - Fix API docs table return type from `Array{T,N}` to generic description
mgyoo86
referenced
this pull request
Mar 6, 2026
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.
Summary
Add
reshape!(pool, A, dims...)that reshapes arrays using the pool's N-D wrapper cache. On Julia 1.11+, cross-dimensional reshapes are zero-allocation after warmup viasetfield!-based wrapper reuse. The result shares memory with the source array.Motivation
Base.reshapeallocates a new wrapper object on every call (~40-80 bytes). In hot loops that repeatedly reshape pooled arrays (e.g., treating a flat vector as a matrix for BLAS), these wrapper allocations accumulate.reshape!eliminates this by reusing cached wrappers from the pool's existingnd_wrappersinfrastructure.Changes
_claim_slot!(wrapper-only slot reservation) and_reshape_impl!forAdaptiveArrayPoolusingsetfield!(:ref, :size)reshape!API with_record_type_touch!,DisabledPoolfallbacks, and_impl!delegatorsreshape!→_reshape_impl!inside@with_pool/@maybe_with_pool; extracteltype(A)for typed-lazy modeDesign
setfield!(A, :size, dims)in-place — no pool interaction, effectively free_claim_slot!reserves a pool slot without backing memory, then reuses a cachedArray{T,N}wrapper viasetfield!(:ref, :size)pointing toA'sMemoryRefDimensionMismatchthrown before slot reservation ifprod(dims) != length(A)_reshape_impl!delegates toBase.reshape