Open
Conversation
Add a new mutable, fixed-size, contiguous buffer type `SizedMemory{T, n}`
to `Core`, where `T` is the element type and `n` is a compile-time integer
type parameter specifying the number of elements. This provides a primitive
for building statically-sized `AbstractArray` types that support all element
kinds (isbits, boxed, unions), unlike the `NTuple`-based approach used by
StaticArrays.jl's `MArray` which only works for isbits types.
`SizedMemory` stores element data inline with no header fields (no `length`
or `ptr`), making it minimal-overhead and a candidate for stack allocation
via escape analysis. It uses the same element storage strategies as
`GenericMemory` (arrayelem layout flags, GC pointer scanning) but is
structurally simpler — no MemoryRef indirection, no ownership modes, and
the length is derived from the type parameter rather than stored per-instance.
Zero-sized instances (n=0 or sizeof(T)==0) are singletons, following
Memory's existing pattern.
Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
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.
Add a new mutable, fixed-size, contiguous buffer type
SizedMemory{T, n}toCore, whereTis the element type andnis a compile-time integer type parameter specifying the number of elements. This provides a primitive for building statically-sizedAbstractArraytypes that support all element kinds (isbits, boxed, unions), unlike theNTuple-based approach used by StaticArrays.jl'sMArraywhich is a hack that only works for isbits types.SizedMemorystores element data inline with no header fields (nolengthorptr), making it minimal-overhead and a candidate for stack allocation via escape analysis. It uses the same element storage strategies asGenericMemory(arrayelem layout flags, GC pointer scanning) but is structurally simpler — no MemoryRef indirection, no ownership modes, and the length is derived from the type parameter rather than stored per-instance.Zero-sized instances (n=0 or sizeof(T)==0) are singletons, following Memory's existing pattern.
Example codegen
Here's an example at the REPL defining some basic copy and summation functions.
The only call is a small allocation, and bounds checks are ellided.
Here's an example of escape analysis allowing us to create a scratch space on the stack and working on that for a result:
Codegen did pretty well. I think the sum could possibly used a wider SIMD instruction for element 1 and 2?
These patterns cover most of our
@generatedfunctions in StaticArrays.jl - we should be able to write StaticArrays as standard methods, no@inboundsor manually unrolled loops, able to use imperative algorithms where they make more sense. ObviouslyMArraycan be a direct wrapper aroundSizedMemory, but it can also be used for "scratch space" to construct the elements for aSArrayand constructing theSArrayon return (or any otherStaticArrayfor that matter).