# \[ANN\] ArrayAllocators.jl v0.3 composes with OffsetArrays.jl v1.12.1+ for faster zeros with offset indexing

**URL:** <https://discourse.julialang.org/t/ann-arrayallocators-jl-v0-3-composes-with-offsetarrays-jl-v1-12-1-for-faster-zeros-with-offset-indexing/83161>\
**Category:** Package Announcements\
**Tags:** announcement\
**Created:** [June 22, 2022, 5:04am UTC](https://discourse.julialang.org/t/ann-arrayallocators-jl-v0-3-composes-with-offsetarrays-jl-v1-12-1-for-faster-zeros-with-offset-indexing/83161 "2022-06-22T05:04:29Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [June 22, 2022, 6:17am UTC](https://discourse.julialang.org/t/ann-arrayallocators-jl-v0-3-composes-with-offsetarrays-jl-v1-12-1-for-faster-zeros-with-offset-indexing/83161/2 "2022-06-22T06:17:37Z")

</div>

Another feature of ArrayAllocators.jl v0.3 is an alternate implementation of `zeros` via `calloc`. Note that this `ArrayAllocators.zeros` is not exported. Here `ArrayAllocators.zeros(T, ...)` is essentially just `Array{T}(calloc, ...)`.

```julia
julia> using ArrayAllocators

julia> @time AAZ = ArrayAllocators.zeros(Int, 1024, 1024, 1024);
  0.029364 seconds (61.31 k allocations: 8.004 GiB, 99.89% compilation time)

julia> @time AAZ = ArrayAllocators.zeros(Int, 1024, 1024, 1024);
  0.000037 seconds (4 allocations: 8.000 GiB)

julia> @time BZ = Base.zeros(Int, 1024, 1024, 1024);
  4.448959 seconds (2 allocations: 8.000 GiB, 0.52% gc time)

julia> @time BZ = Base.zeros(Int, 1024, 1024, 1024);
  4.665584 seconds (2 allocations: 8.000 GiB, 2.48% gc time)

```

Note that on some operating systems this may defer the actual allocation of memory until writing.

```julia
julia> @time fill!(AAZ, 1)
  4.849603 seconds (8.17 k allocations: 491.429 KiB, 0.64% compilation time)

julia> @time fill!(AAZ, 2);
  1.634943 seconds

julia> @time fill!(BZ, 1);
  1.710879 seconds

julia> @time fill!(BZ, 2);
  1.717882 seconds

```

If you wanted to switch your code to use `ArrayAllocators.zeros` instead of `Base.zeros`, you can import it before your first reference to `zeros`.

```julia
julia> using ArrayAllocators: zeros

julia> @time zeros(Int, 1024, 1024, 1024);
  0.000028 seconds (4 allocations: 8.000 GiB)

julia> @time Base.zeros(Int, 1024, 1024, 1024);
  4.442414 seconds (2 allocations: 8.000 GiB, 0.10% gc time)

```

While this may look like a free lunch, note @Sukera’s caveats in the original post:

> [@Faster zeros with calloc](https://discourse.julialang.org/t/faster-zeros-with-calloc/69860):
>
> Abstract TL; DR zeros\_via\_calloc is a potentially faster version of zeros that is comparable to Array{T}(undef, ...) and numpy.zeros. function zeros\_via\_calloc(::Type{T}, dims::Integer...) where T ptr = Ptr{T}(Libc.calloc(prod(dims), sizeof(T))) return unsafe\_wrap(Array{T}, ptr, dims; own=true) end # Windows benchmark julia\> @btime zeros\_via\_calloc(Float64, 1024, 1024); 12.400 μs (2 allocations: 8.00 MiB) julia\> @btime zeros(Float64, 1024, 1024); 1.652 ms (2 allocations: 8.00 MiB)…

---

_[View the full topic](https://discourse.julialang.org/t/ann-arrayallocators-jl-v0-3-composes-with-offsetarrays-jl-v1-12-1-for-faster-zeros-with-offset-indexing/83161)._
