mir-algorithm and mir-core (D)
The D ecosystem's N-dimensional array library: mir.ndslice's Slice!(Iterator, N, kind) is a strided view over any random-access iterator, with contiguity encoded in the type, lazy topology selectors, and allocation kept apart from views; it is the existing D candidate for the memory model under a Sparkles Image type.
| Field | Value |
|---|---|
| Language | D (DMD and LDC; LDC-specific @fmamath and SIMD paths) |
| License | Apache-2.0 (both repositories) (LICENSE, dub.sdl) |
| Repository | libmir/mir-algorithm, with its dependency libmir/mir-core |
| Revision read | mir-algorithm 2da212382e434ef86e93f7b028bc2309205ae00e (v3.22.4-5, February 4, 2026); mir-core 4397b03a804b0368100e99252016c7b32aa562d1 (v1.7.4) |
| Documentation | mir-algorithm.libmir.org (generated DDoc, including mir.ndslice.slice) |
| Category | D landscape: N-D arrays, views and iteration |
| Tier | A |
| Last reviewed | November 7, 2026 |
NOTE
The project is maintained, not archived. The GitHub API on November 7, 2026 reported archived: false for both repositories and a mir-algorithm push on November 6, 2026; git ls-remote lists a tag v3.22.5 (ba6d483). The pin read here is nine months older than that tag. The documentation site's title, "mir-algorithm API Reference · Libmir Archive", is the site's name, not a statement about the repositories; the generic-image-programming thread now says so too.
IMPORTANT
Claims marked verified were demonstrated by compiling and running programs against the pinned sources (extracted with git archive, compiled with LDC 1.43.0 and DMD 2.112.1 from the Sparkles dev shell, in a scratch directory outside the repository). Everything else was read in the source and is documented. No performance numbers are stated; only struct sizes, which were printed.
Overview
What it solves
D's built-in slice T[] is a pointer and a length. It has no shape, no strides and no notion of rows, so every image library in D has either hand-rolled a (data, width, height, pitch) struct or flattened images into one dimension and computed indices by hand. The Sparkles baseline records exactly this: pixels travel as const(ubyte)[] plus a Size, as ColourRegion (data plus stride) or as ImagePixels (no padding), and no package carries a strided view type (existing code, G2).
mir.ndslice supplies the missing type. A Slice is lengths, strides and an iterator; every view operation (sub-rectangle, transpose, reversal, striding, windows, tiles, element-wise maps, zipping) rewrites those three fields or wraps the iterator, and never touches the data. mir.algorithm.iteration then supplies the loops (each, reduce, all, equal, count, ...) that walk any slice kind, and mir.ndslice.allocation supplies the few functions that allocate, from the GC, a std.experimental.allocator allocator, malloc, or a reference-counted heap block.
Design philosophy
The Slice documentation states the separation of views from storage outright (slice.d):
"Multidimensional Slice is a structure that consists of lengths, strides, and a iterator (pointer). … With the exception of $(MREF mir,ndslice,allocation) module, no functions in this package move or copy data. The operations are only carried out on lengths, strides, and pointers."
The $(MREF …) is the DDoc macro for mir.ndslice.allocation.
and the topology module opens with the same promise (topology.d):
"Selectors create new views and iteration patterns over the same data, without copying."
Three further commitments shape the design:
Contiguity is a type parameter, not a run-time flag.
SliceKindhas three members, and a slice of each kind stores a different number of strides (slice.d). Algorithms dispatch on it withstatic if.Everything is an iterator or a field. A
Sliceis generic over any type that passesisIterator(pointer-like arithmetic and indexing,traits.d); a "field" is anything withopIndex(ptrdiff_t)(field.d). Lazy views are new iterator types (MapIterator,ZipIterator,StrideIterator,SliceIterator, ...), listed initerator.d.No runtime dependency where possible. mir-core's README states the constraint for the base layer (
README.md):"no runtime dependency : betterC compatible when compiled with LDC in release mode. Exceptions:
@nogcmir.exception."mir-algorithm inherits the stance without stating it as a rule; its
mir.algorithm.iterationheader credits a "Mir & BetterC rework" of Phobos code (iteration.d).
The safety contract is stated at the top of slice.d (slice.d):
"User-defined iterators should care about their safety except bounds checks. Bounds are checked in ndslice code."
How those bounds checks are made, and what they miss, is the subject of dimension 5.
How it works
The Slice struct and its three kinds
Slice is an alias for mir_slice (slice.d), declared with a fixed shape constraint (slice.d):
// source/mir/ndslice/slice.d (abridged)
enum mir_slice_kind
{
/// A slice has strides for all dimensions.
universal,
/// A slice has >=2 dimensions and row dimension is contiguous.
canonical,
/// A slice is a flat contiguous data without strides.
contiguous,
}
struct mir_slice(Iterator_, size_t N_ = 1, SliceKind kind_ = Contiguous, Labels_...)
if (0 < N_ && N_ < 255 && !(kind_ == Canonical && N_ == 1) && Labels_.length <= N_ && isIterator!Iterator_)
{
@fmamath:
enum size_t S = kind == Universal ? N : kind == Canonical ? N - 1 : 0;
// ...
public alias _Structure = AliasSeq!(size_t[N], ptrdiff_t[S]);
public _Structure _structure;
public alias _lengths = _structure[0];
public alias _strides = _structure[1];
public Iterator _iterator;
public Labels _labels;Lengths are size_t[N]; strides are signed ptrdiff_t[S], counted in elements of the iterator, not in bytes; a negative stride is how reversed works without moving the iterator past the data. The Labels parameter carries per-dimension label iterators (the mir.series data-frame use) and is empty for plain arrays.
What the kinds store, and what they promise:
| Kind | Strides stored | Promise | Typical source |
|---|---|---|---|
Contiguous | 0 | the whole N-D block is one run of elements, row-major | slice!T(h, w), sliced over an array |
Canonical | N - 1 | the last dimension is contiguous (stride 1); others are free | a sub-rectangle img[y0 .. y1, x0 .. x1], padded rows |
Universal | N | none; every dimension has its own stride | transposed, reversed, strided, a column of pixels |
The documentation compares the kinds with established layouts: "BLAS/LAPACK matrices are Canonical" and "NumPy strides are Universal" (slice.d).
Verified on x86_64: the struct sizes for a pointer iterator are Slice!(ubyte*, 2) 24 bytes, Slice!(ubyte*, 2, Canonical) 32, Slice!(ubyte*, 2, Universal) 40, Slice!(ubyte*, 3, Canonical) 48 and Slice!(ubyte*, 3, Universal) 56. The cost of a kind is one word per stored stride, plus whatever the inner loop loses when it cannot assume stride 1 (not measured).
Kinds only weaken through views: universal and canonical convert up the chain, and assumeCanonical/assumeContiguous convert down; the module index says each "Does only assert checks" (topology.d). Element access goes through indexStride, which sums index * stride per dimension after an assert(index < length) (slice.d), and the opIndex overloads apply it to the iterator (slice.d).
Construction
sliced builds a Contiguous slice over an array, pointer or iterator (slice.d). For arrays it checks the size once:
// source/mir/ndslice/slice.d, sliced
static if (isDynamicArray!Iterator)
{
assert(lengthsProduct(_lengths) <= iterator.length,
"array length should be greater or equal to the product of constructed ndslice lengths");
auto ptr = iterator.length ? &iterator[0] : null;
return Slice!(typeof(C.init[0])*, N)(_lengths, ptr);
}For any other kind, construction is the struct's literal constructor: Slice!(uint*, 2, Universal)([2, 2], [4, 1], array.ptr) is the module's own test (slice.d). No function validates explicit strides against a backing extent; the fields are public.
Topology: views without copies
The selectors that matter for images, all in mir.ndslice.topology unless noted:
| Selector | Result kind | Image use |
|---|---|---|
img[y0 .. y1, x0 .. x1] | Canonical (from Contiguous) | region of interest, crop |
transposed (dynamic.d) | Universal | rotate by 90 degrees with reversed; interleaved to planar ([c, y, x]) |
reversed (dynamic.d) | Universal | flips; bottom-up BMP rows |
stride (topology.d) | 1-D only, StrideIterator | subsampling one row; strided (in dynamic) for N-D |
pack!P, ipack!P, unpack (pack, ipack, unpack) | outer Universal, inner by kind | a 2-D slice of pixels ([h, w, 4].pack!1), or of rows |
windows (topology.d) | packed, outer Universal | overlapping neighbourhoods: convolution, morphology |
blocks (topology.d) | packed, outer Universal | tiles; "residual blocks are ignored" |
map, vmap (topology.d) | same kind, MapIterator | per-pixel functions, lazily |
as!T (topology.d) | same kind, map!(to!T) | ubyte to float promotion |
zip (topology.d) | ZipIterator, same strides required | planes or images in lock-step |
bitwise, bitpack!k (topology.d) | FieldIterator!BitField | 1-bit masks, sub-byte samples |
bytegroup!(g, T) (topology.d) | BytegroupIterator | 24-bit RGB as packed groups, endian-correct |
pack!P "Packs the last P dimensions" (topology.d) by calling ipack, which moves the iterator into a SliceIterator that carries the inner lengths and strides (topology.d):
// source/mir/ndslice/topology.d, ipack (abridged)
auto sl = slice.move.universal;
return Ret(
cast( size_t[P]) sl._lengths[0 .. P],
cast(ptrdiff_t[P]) sl._strides[0 .. P],
It(
cast( size_t[EN]) sl._lengths[P .. $],
cast(ptrdiff_t[ES]) sl._strides[P .. $ - (It.Element.kind == Canonical)],
sl._iterator.move));The outer slice always becomes Universal: a packed slice stores every outer stride.
Iteration
each has one fast path. If every argument is Contiguous, it flattens all of them to one dimension and loops once; otherwise it recurses one dimension at a time with front/popFront (iteration.d):
// source/mir/algorithm/iteration.d, each
static if (Slices.length > 1)
slices.checkShapesMatch;
static if (areAllContiguousSlices!Slices)
{
import mir.ndslice.topology: flattened;
.each!fun(allFlattened!(allLightScope!slices));
}
else
{
if (slices[0].anyEmpty)
return;
eachImpl!fun(allLightScope!slices);
}eachImpl (iteration.d) and reduceImpl (iteration.d) are the same nested do ... while over dimensions. A Canonical image therefore runs as one loop per row with a unit-stride inner loop, which is the shape LLVM can vectorise; checkShapesMatch asserts that zipped arguments agree in shape (iteration.d).
Allocation
allocation.d groups the allocating functions by source:
| Family | Functions | Failure behaviour |
|---|---|---|
| GC | slice, uninitSlice, bitSlice, ndarray, uninitAlignedSlice | no check in mir (left to the GC); uninitAlignedSlice is @system |
| Allocator | makeSlice, makeUninitSlice, makeNdarray | if (mem.length == 0) assert(0); |
| C runtime | stdcSlice, stdcUninitSlice, stdcFreeSlice, aligned variants | if (p is null) assert(0); |
| Reference counting | rcslice, mininitRcslice, bitRcslice | throws a preallocated OutOfMemoryError |
Every one returns a Contiguous slice; none takes a row pitch or alignment per row. rcslice stores a mir_rc_context header (deallocator, type info, a shared size_t counter and the length) in front of the payload in one malloc block (rc/context.d, rc/context.d); the iterator is mir_rci!T, and the module states "The implementation never adds roots into the GC." (rc/array.d).
Analysis
1. Pixel and memory model
Slice is an N-D strided view, so the three image layouts the focus names all map onto it, each with a different cost. Verified with a 5 x 3 RGBA image whose rows are 24 bytes (20 bytes of pixels, 4 of padding):
// scratch experiment, layouts.d
auto il = Slice!(ubyte*, 3, Canonical)([size_t(H), W, 4], [ptrdiff_t(pitchBytes), 4], &buf[0]);
auto px = Slice!(RGBA*, 2, Canonical)([size_t(H), W], [ptrdiff_t(pitchBytes / RGBA.sizeof)], cast(RGBA*) &buf[0]);
auto planar = il.transposed!2; // [4, H, W], Universal, strides 1, 24, 4
auto packed = il.pack!1; // 2-D slice of 1-D pixel slices- Interleaved RGBA has two spellings. As
Slice!(ubyte*, 3, Canonical)with lengths[h, w, 4], channels are a dimension, the row pitch is any number of bytes, andreduceover it sums only the pixels (verified: 2010, the padding excluded). AsSlice!(RGBA*, 2, Canonical)the pixel is the element, which reads better but forces the pitch to be a multiple ofRGBA.sizeof, because strides count elements. - Planar is
[c, h, w]: eithertransposed!2of the interleaved view (aUniversalslice whose channel stride is 1, verified) or, for genuinely separate plane storage, one slice per plane. - Planar YUV 4:2:0 with independent per-plane pitches does not fit one
Slice. The planes differ in lengths (chroma is half size in both axes) and in strides, andSlicehas one iterator and one stride per dimension. It is threeSlice!(ubyte*, 2, Canonical)values, one per plane. A luma-resolution chroma view is a lazy index map (ndiota(h, w).map!(ij => U[ij[0] / 2, ij[1] / 2]), verified to compile and produce aContiguous2-D view); a semi-planar NV12 chroma plane is[h / 2, w / 2, 2].
Limits, all consequences of element strides:
- A row pitch that is not a multiple of the element size cannot be represented. An RGBA struct over a 22-byte pitch has no element stride; the
ubyteview must be used, or the decoder must realign. The Python-buffer bridge makes the same rule explicit withassert(view.strides[i] % T.sizeof == 0)(connect/cpython.d). - Sub-byte formats (1-bit masks, 4-bit palette indices, 10-bit packed video) are not pointer-strided at all;
bitwiseandbitpack!kgive them aBitFielditerator, andbitwiserequires a non-Universalslice for N greater than 1 (topology.d). - Tiles and N-D extents are views (
blocks,windows, any N up to 254), not storage layouts: a tiled TIFF or a GPU-swizzled texture needs a custom iterator or field. An iterator can do this (slicedField,slicedNdFieldexist for exactly that), but nothing in mir provides one. blocksdrops edge tiles: a 3 x 5 image in 2 x 2 blocks gives 1 x 2 tiles (verified).mir.ndslice.chunkshandles residuals (chunks.d).
2. Genericity mechanism
The mechanism is D templates with structural constraints, the design-by-introspection style in all but name:
Sliceis generic overIterator, gated byisIterator, a__traits(compiles, ...)test for difference, increment, decrement and indexing (traits.d). Any pointer, any field wrapped inFieldIterator, and every lazy iterator initerator.dqualifies.SliceKindis a non-type template parameter, so kind-specific code isstatic if (kind == Contiguous);zip,each,bitwiseandbytegroupall branch this way.- Qualifiers propagate through mir-core's
lightConst,lightImmutableandlightScopeprotocol (mir.qualifier): aconstslice of a reference-counted iterator becomes a slice of aconstiterator, andlightScopestrips the reference count for the duration of a loop.
There is no pixel concept. A pixel is whatever DeepElement is: ubyte, a user struct, a refTuple from zip, or the result of a map. Channel order, alpha mode, transfer function and bit depth are not part of the type. A Sparkles image type would add them as a layer above (isImageView, hasScanlines, isPlanar, as the paper thread's synthesis proposes) or encode them in the element type.
3. Colour and metadata
Not applicable: mir-algorithm has no colour, colour-space, transfer-function, ICC, CICP, EXIF or XMP model, and does not claim one. Its numeric support (mir.math, mir.interpolate) can implement colour conversions as map functions, but the semantics live in the caller. The Labels template parameter attaches per-axis label arrays (for mir.series), not per-image metadata.
4. I/O and codec architecture
Not applicable: mir-algorithm reads and writes no image or file format. Its serialisation neighbour, mir-ion, is out of scope here. The relevant property for a codec is that a decoder can write into a Slice it did not allocate: sliced over caller storage, or a Canonical sub-view of a padded buffer, is a valid destination for dst[] = src and each. Verified: a float destination slice over a caller's stack array filled by dst[] = row.as!float.map!(a => a / 255.0f) with no allocation, under -betterC.
5. Hostile input and safety
This is where mir is weakest as a foundation for decoders. The library's bounds checks are asserts in indexStride and sliced, the size arithmetic is unchecked, and the @trusted boundary is wide. Four behaviours were verified in functions explicitly annotated @safe pure nothrow @nogc, compiled with -preview=dip1000:
// scratch experiment, safety.d
@safe pure nothrow @nogc ubyte wrapRead(ubyte[] backing)
{
auto s = backing[0 .. 16].sliced((size_t(1) << 62) + 1, 4); // product wraps to 4
return s[4, 0]; // byte 16: outside the slice's array
}
@safe pure nothrow @nogc int reversedContiguous(int[] backing)
{
auto r = backing[0 .. 4].sliced.reversed; // iterator at element 3, stride -1
return r.assumeContiguous[3]; // reads element 6
}| Case | Debug build | -release |
|---|---|---|
sliced with lengths whose product overflows size_t | reads outside the source array, no error | same |
reversed then assumeContiguous, index within the length | reads outside the source array, no error | same |
| struct literal with strides that exceed the described extent | reads beyond the 2 x 2 extent, no error | same |
index past a length ([3, 0] on a 2 x 2 slice) | AssertError "bounds check failed" | reads outside, no error |
zip!true on planes of different pitch | AssertError | no error |
The causes, in the source:
lengthsProductmultipliessize_tlengths with no overflow check (internal.d), although mir-core shipsmir.checkedint(checkedint.d).sliced's only guard isassert(lengthsProduct(...) <= iterator.length), which a wrapped product passes. The allocators have the same shape:makeUninitSlicecomputeslen * T.sizeofunchecked (allocation.d),stdcUninitSlicelikewise (allocation.d), andmir_rc_createcomputeslength * typeInfo.sizeunchecked (rc/context.d). A decoder that passes header dimensions straight to these functions gets a small buffer and a huge slice.assumeContiguousis documented as "(unsafe)" but is an unannotated template whose body only copies fields, so it infers@safe(topology.d).- The element
opIndexoverloads are templates marked@trustedas a whole (slice.d). Their soundness depends on the invariant that lengths and strides describe memory the iterator may reach, and that invariant is held only by convention: the fields and the literal constructor are public. - Index checks are
assert, not D array bounds checks, so-boundscheck=ondoes not restore them under-release(verified). Sparkles shipscheckedbuilds, in which asserts stay live (build types), so the index check would survive there; the three overflow and invariant cases above do not depend on build mode.
The Sparkles guidelines reach the opposite conclusion on two of these points: @trusted "wraps only the unavoidable operation ... never a whole function, never a template", and the generic-programming synthesis asks to "Validate stride × extent against the backing buffer once, at construction, so a hostile decoder header cannot create an out-of-bounds view" (paper synthesis). mir has no fuzzing harness in the repository.
dip1000 does work through Slice (verified): returning a[].sliced(4, 4) over a local static array, or a transposed view of it, from an @safe function is rejected with "escaping a reference to local variable" and "returning scope variable".
6. Allocation and ownership
Views own nothing; ownership is the iterator's. A T* slice borrows; an RCI!T slice shares ownership by atomic reference count; a T[] from slice!T is GC-owned. makeSlice accepts any std.experimental.allocator allocator (allocation.d), which matches Sparkles' allocator guidance, but the result is a borrowed T* slice and the caller must remember the allocator and the original length to free it (the stdcFreeSlice pair makes the same demand).
Gaps for an image library:
- Every allocator returns a
Contiguousblock. There is no "allocatehrows ofwpixels with each row aligned to 64 bytes" function, so a padded, aligned image (the usual layout for SIMD row loops and GPU staging) is a 1-D allocation sliced into aCanonicalview by hand. - Allocation failure is
assert(0)in the allocator and C-runtime families and anErrorin the reference-counted one. None returns anExpected; a decoder that wants to report "image too large" as a value must check sizes before calling. rcsliceusesmallocthroughmir.internal.memoryonly; it takes no allocator, and its header is not aligned beyondmalloc's guarantee.- There is no move-only owning slice. Sparkles' default,
UniqueBuffer, has no counterpart;rcslicecorresponds toSharedBuffer.
7. CPU parallelism and SIMD
mir-algorithm has no parallel loops: no std.parallelism, no thread pool, no tiling scheduler in ndslice or algorithm. Parallelism is the caller's, typically by splitting the outer dimension (blocks, chopped, or row ranges) across workers.
It has no explicit SIMD in ndslice either. mir.internal.ldc_simd is used by mir.string and mir.interpolate, not by the slice loops. Vectorisation is left to LLVM, helped by two choices: the unit-stride inner loop of Canonical and Contiguous slices, and @fmamath, which the whole Slice struct is marked with and which is llvmFastMathFlag("contract") on LDC (math/common.d). Verified only coarsely: at -O3 -mcpu=x86-64-v3, the object code for each over Contiguous, Canonical and Universal views of a byte image contains AVX ymm instructions in all three functions; which loops are vectorised, and how fast, was not measured.
contract permits fused multiply-add. A float kernel compiled through Slice may therefore round differently from a scalar reference, which matters to exact-output oracles (the shaders baseline, G8, already sees ULP-scale differences between host and device).
8. GPU offload and interop
mir-algorithm has no GPU support. Its interop surfaces are:
- The Python buffer protocol (
connect/cpython.d):fromPythonBufferandtoPythonBuffer,nothrow @nogc @trusted, convert byte strides to element strides and back, and reject format, item-size and dimension mismatches with error codes. They copy shape and strides from the buffer without checking them against any extent. - C++ headers under
include/mir/mirrormir_slice,mir_rcarrayand friends forextern(C++)calls. Read, not compiled: the primary C++ template, used for theuniversalandcanonicalkinds, declaresmir_size_t _lengths[0]where the D struct hassize_t[N], while thecontiguousspecialisations declare_lengths[N](ndslice.h). - DLPack is not supported; nothing in either repository mentions it. The mapping is close: a
DLTensorcarriesint64_tshape,int64_tstrides "in number of elements, not bytes", and abyte_offset(dlpack.h), which is aUniversalSliceplus a device tag and a dtype. Element strides on both sides avoid the byte-to-element division; the 64-bit fields narrow on 32-bit targets such as wasm32.
The neighbours: dcompute compiles D kernels for OpenCL and CUDA and has no ndslice dependency (a search of its tree finds none); lubeck is a BLAS/LAPACK front end that takes and returns Slice.
9. Temporal, animation and containers
Not applicable as a feature. Time is one more dimension: a video clip or an animation is a 4-D [t, h, w, c] slice, and pack!3 iterates frames, but there is no frame timing, disposal mode, container or streaming model.
10. Testing and conformance
Tests are DDoc'd unittests in the modules, gated by version identifiers (mir_ndslice_test, mir_test) that dub.sdl's unittest build type sets (dub.sdl); the pinned slice.d, topology.d and iteration.d contain 118, 152 and 114 lines containing unittest. CI runs dub test on Ubuntu for x86 and x86_64, on macOS x86_64 and on Windows x86_64, against DMD and LDC master, latest and beta (ci.yml, compilers.json). Read in that configuration: no -betterC, no wasm and no Android job, no -preview=dip1000 job (a unittest-dip1000 build type exists but CI does not select it), no sanitizer and no fuzzing.
What this survey ran, all verified:
| Build | Result |
|---|---|
LDC, DMD; -preview=dip1000 -preview=in | @safe pure nothrow @nogc functions using sliced, sub-views, windows, reduce, each, all, map, as compile and run |
LDC -betterC, with and without -release | the same program runs; rcslice also runs under -betterC |
DMD -betterC | the same program runs |
LDC -betterC, wasm32-unknown-unknown, only the program's module compiled (-I to mir's sources) | compiles, links (with a trapping __assert) and runs under Node.js with no imports, for Contiguous and Canonical views |
LDC -betterC, wasm32-unknown-unknown, mir's modules compiled too (ldc2 -i, dub build) | fails: mir/internal/memory.d(154): static assert (only Posix and Windows are handled) |
The wasm outcome depends on what is compiled, not on what the program uses. mir.internal.memory ends in a module-level else static assert(0); (memory.d), so it fails whenever that module is compiled for a target that is neither Posix nor Windows. Slice reaches it through imports: for a non-pointer or non-contiguous slice it imports mir.rc.array to declare ptr (slice.d), which imports mir.rc.context, whose mir_rc_create imports mir.internal.memory inside its body (rc/context.d). Compiling only the program's module, with mir's templates instantiated there, never analyses that body: sliced plus reduce over a Contiguous and over a Canonical view compiled, linked with -link-internally -L--no-entry -L--export-dynamic and a trapping __assert, and returned the expected sums under Node.js (verified). ldc2 -i compiles every imported module and fails on both views, and so does a dub build --arch=wasm32-unknown-unknown-wasm, which compiles mir-core as a library first (verified). The experiment reaches the same result for its kernels and adds a druntime-side failure (an array cast). Android is a Posix target and was not tried here; the experiment links for it.
Lessons for Sparkles
The Slice experiment tests these points on Sparkles' own build; this section gives the evidence from reading and from the scratch runs above.
- Q1: the three-kind classification transfers; the element-stride unit is a choice with a cost.
Contiguous/Canonical/Universalencode exactly the properties an image loop specialises on, andCanonicalis the ordinary padded image. Element strides makeRGBA-typed views fail on odd pitches; byte strides (NumPy, the paper synthesis) make every access a cast. mir answers this by makingubytewith a channel dimension the universal fallback. Where Sparkles stands: no view type at all (existing code, G2; shaders, G10). - Q1: planar YUV is a tuple of slices, not a slice. Independent per-plane lengths and pitches need one
Sliceper plane, andziprefuses multi-dimensional slices of different strides (astatic assertwithoutsameStrides, anassertwith it). A Sparkles image type for video formats is therefore a struct of planes over slices, with the plane relationship (subsampling, siting) in that struct. This holds whether or not the planes are mir slices. - Q2, Q3: lazy topology composes cheaply and stays
@nogc.map,as,windows,blocks,pack, sub-views andeach/reduceall compiled in@safe pure nothrow @nogccode over caller storage, with-preview=dip1000, anddip1000caught escapes through the view types. This matches the functional guideline, which already tells authors to "usemir.ndslicefor any multidimensional data" while no package does (existing code, capability table). - Q3, Q7: no parallel or SIMD layer to inherit. The unit-stride row loop is the only vectorisation aid; dispatch to AVX2/AVX-512/NEON kernels would remain Sparkles' own, as in the BLAKE3 pattern (shaders baseline, G4 and G5).
@fmamathonSlicechanges float rounding, which an exact-output oracle must account for. - Q4, Q8: the safety model does not meet a hostile-input bar. Unchecked
lengthsProduct, unvalidated public fields, anassumeContiguousthat infers@safeand whole-template@trustedindexing mean that mir cannot be the layer that validates a decoder's header. Whichever type a decoder constructs must checkstride * (length - 1)against the buffer with checked arithmetic and return anExpected, and must not exposeassumeContiguous-style escapes to@safecallers. Wrapping a mirSlicein such a type is possible; using it bare is not. - Q6, Q9: interop is easier than it looks, but not provided. A
Universalslice is one step from aDLTensor(element strides on both sides), andconnect/cpythonshows the error-code style for such a bridge. Neither DLPack nor a GPU staging layout exists in mir; both would be Sparkles code (vulkan baseline). - Q9: the dependency has a build-mode cost. mir-core's betterC claim held under LDC and DMD for the tested pipeline. For
wasm32-unknown-unknown,Slicecode builds only when mir's own modules are not compiled:dub buildandldc2 -ifail onmir.internal.memory's static assert, so a browser build must compile the program's modules alone against mir's sources, as the experiment did. mir-algorithm's CI tests neither betterC nor wasm nor Android. A Sparkles image core that must build for the browser through dub would need an upstream fix tomir.internal.memory, a fork, or its own view type. The dependency also bringsmir-coreand Apache-2.0 licensing, and the pin lags upstream by nine months: the dub and Nix locks would need to follow upstream releases. - Q2: no pixel or colour vocabulary to adopt. mir supplies the array, not the image: channel order, alpha state and colour encoding must come from Sparkles, as a runtime descriptor or as element types (existing code, open question 3).
Strengths
- One mature, documented type covers 1-D rows, 2-D images, 3-D channel stacks and 4-D clips, with contiguity in the type and every view operation free of copies.
- A large, composable selector library (
windows,blocks,pack,zip,map,bitwise,bytegroup,transposed,reversed) that already covers neighbourhood and tiled image access. - Allocation is separated from views and offers GC, allocator, C runtime and reference-counted storage.
- The core works in
@safe pure nothrow @nogccode withdip1000, and under-betterCon LDC and DMD (verified). size_tlengths, signedptrdiff_telement strides and C/C++ and Python-buffer interop are already worked out.- Maintained: a push on November 6, 2026, and a newer tag (
v3.22.5) than the pin.
Weaknesses
- Bounds and size checks are
asserts; length products and byte sizes overflow silently; strides are never validated against storage;assumeContiguousinfers@safe(all verified). @trustedcovers whole templated indexing functions, against Sparkles' rule.- Element strides cannot express pitches that are not a multiple of the element size.
- No per-row alignment in allocation, no
Expectederrors, no move-only owner. - No parallel loops, no explicit SIMD, no GPU path, no DLPack.
- A
wasm32-unknown-unknownbuild fails whenever mir-core's modules are compiled, asdub buildalways does; only a compile of the program's own modules against mir's sources works (verified). CI covers none of betterC, wasm, Android ordip1000. - Multi-plane formats with independent pitches need a type above
Slice. - The C++ header's strided
mir_slicedeclares zero-length_lengths(read, not compiled).
Key design decisions and trade-offs
| Decision | Rationale | Trade-off |
|---|---|---|
Contiguity (SliceKind) as a template parameter | loops specialise at compile time; Contiguous stores no strides | three instantiations of every algorithm; views silently weaken the kind to Universal |
| Strides in elements, signed | indexing is iterator[sum(i * s)]; negative strides give free reversal | pitches that are not a multiple of the element size are unrepresentable |
Generic over any isIterator type | lazy views (map, zip, bitwise) are just iterators; sub-byte data fits | invariants live in each iterator; a slice over a custom iterator is as safe as that iterator |
| Views never allocate; allocation in one module | ownership is the iterator's; the same view code serves GC, malloc and RC storage | a borrowed T* slice carries no ownership, so freeing is the caller's bookkeeping |
| Public fields and literal constructor | zero-cost construction from any source (Python buffers, C++) | no constructor can enforce stride against extent; @safe code can build an out-of-bounds view |
Bounds checks by assert | free in -release, informative in debug | gone in -release even with -boundscheck=on; no recoverable error for hostile input |
@fmamath (contract) on Slice | lets LDC fuse multiply-adds in element-wise maths | float results differ from a non-contracted reference |
zip requires equal strides for N greater than 1 | one stride set serves every zipped iterator | planes of different pitch cannot be zipped; multi-plane images need a separate type |
Reference counting through a malloced header | GC-free shared ownership, usable from C++ and betterC | no allocator parameter, no alignment control, allocation failure is an Error |
Sources
- libmir/mir-algorithm at
2da212382e434ef86e93f7b028bc2309205ae00e; libmir/mir-core at4397b03a804b0368100e99252016c7b32aa562d1 - mir-algorithm API documentation;
mir.ndslice.slice source/mir/ndslice/slice.d: kinds, representation,sliced, indexingsource/mir/ndslice/topology.d: selectorssource/mir/ndslice/dynamic.d:transposed,reversedsource/mir/ndslice/allocation.dsource/mir/algorithm/iteration.dsource/mir/rc/context.d,source/mir/rc/array.dsource/mir/ndslice/connect/cpython.d,include/mir/ndslice.h- mir-core
README.md,source/mir/internal/memory.d,source/mir/math/common.d - DLPack
dlpack.hat94485e23519bc30e22a18397a9c8ff1ea6b2b81e - Neighbours: lubeck; dcompute and its research page
- Sparkles: existing-code baseline, shaders and SIMD baseline, Vulkan baseline, generic image programming papers, the
Sliceexperiment, catalog umbrella