ArrrayAllocators.jl v0.3.0+ and OffsetArrays.jl v1.12.1+ now compose. This means that you can now construct an OffsetArray
of 0
s using the following equivalent lines of code.
julia> using OffsetArrays, ArrayAllocators
julia> OA = OffsetArray{Int}(undef, -512:511); fill!(OA, 0);
julia> OA_calloc = OffsetArray{Int}(calloc, -512:511);
julia> OA[-512] == OA_calloc[-512]
true
julia> isequal(OA_calloc, OA)
true
In some cases, this can yield a reduction in the initialization time of the OffsetArray
as illustrated below.
julia> using BenchmarkTools
julia> @btime begin
OA2 = OffsetArray{Int}(undef, -512:511, -512:511)
fill!(OA2, 0)
end;
1.483 ms (2 allocations: 8.00 MiB)
julia> @btime begin
OA2 = OffsetArray{Int}(calloc, -512:511, -512:511)
end;
1.089 ms (8 allocations: 8.00 MiB)
julia> @btime begin
fill!(OA2, 1)
end setup = (OA2 = OffsetArray{Int}(undef, -512:511, -512:511));
1.521 ms (0 allocations: 0 bytes)
julia> @btime begin
fill!(OA2_calloc, 1)
end setup = (OA2_calloc = OffsetArray{Int}(calloc, -512:511, -512:511));
1.312 ms (0 allocations: 0 bytes)
I encourage you to vigorously benchmark your application when using this as the potential optimization may be operating system and hardware dependent.
Also with the subpackage NumaAllocators v0.2.0, you may now directly allocate an OffsetArray
on a specific non-uniform memory architecture (NUMA) node.
julia> using OffsetArrays, NumaAllocators
julia> OA_numa_0 = OffsetArray{Int}(numa(0), -512:512, -9:9);
The composition was enabled by implementing Base.unsafe_wrap
for OffsetArray
. Neither package depends directly upon the other, so ensure that both packages are at the required version or later.
julia> ptr = Libc.calloc(1024, 8)
Ptr{Nothing} @0x0000000004e3ae10
julia> OA3 = unsafe_wrap(OffsetArray, Ptr{Int}(ptr), 1024);
If you missed the original ArrayAllocators.jl announcement, see below.