ANN: MemViews.jl - low-level view into Memory{T}
MemViews.jl is a small package that introduces two new types:
MemView
is a view intoMemory{T}
MemKind(T::Type)
is a trait signalling ifT
can be represented as memory.
MemViews.jl requires Julia the last Julia 1.11 release branch or master (it does not work on Julia 1.11-beta2).
It is currently undergoing registration.
The MemView
type has the following important features compared to SubArray
:
- It has a concrete, simple memory layout, because it is not generic over the underlying
AbstractArray
or index type.
This makes it much easier to reason about low-level considerations such as whether you can take a pointer to the view, and what stride it has, and what dimensionality it has, and whether it uses one-based indexing, and much else.
Practically speaking, this makes it easier to write correct code. - The
MemKind
trait makes it easier to write specialized implementations for dense, memory backed arrays. In contrast, it’s difficult to select the correct subtypes ofSubArray
(orAbstractArray
) that are dense and memory-backed. - Mutability or immutability is statically encoded into the type as either
MemView{T, :mutable}
orMemView{T, :immutable}
. This makes it more difficult to e.g. accidentally mutate a string, even in code where you regularly manipulate strings with pointers orccall
s.
Please see the documentation for more information:
Also, see the related Julia issue
I’ve already been dogfooding MemViews.jl by developing the upcoming XAMAuxData.jl and PairwiseMappingFormat.jl and I must say it’s been quite nice to use, API-wise.
I’d like to express my appreciation for the work of @Oscar_Smith and @jameson , whose work on the underlying Julia types Memory
and MemoryRef
has made this package possible.
Please give feedback on the package’s design etc
Example
julia> MemView([1, 2, 3]) |> typeof
MutableMemView{Int64} (alias for MemView{Int64, :mutable})
julia> MemView(view(codeunits("abc"), Base.OneTo(0x02))) |> typeof
ImmutableMemView{UInt8} (alias for MemView{UInt8, :immutable})
julia> immutable = MemView("abc");
julia> unsafe_copyto!(immutable, MemView([0x01, 0x02, 0x03]))
ERROR: MethodError: no method matching unsafe_copyto!(::ImmutableMemView{UInt8}, ::MutableMemView{UInt8})