I want to write a better performance array, don't know if there is a ready-made one?

I want to write a better performance array, don’t know if there is a ready-made one?

I wrote a prototype

struct Arr{T, N} <: AbstractArray{T, N}
ilength::Int64
data::Array{T, N}
end

Base.length(A::Arr) = A.ilength
Base.empty!(A::Arr) =A.ilength=0
Base.push!(A::Arr,x)=(A.data[A.ilength+1]=x;A.ilength+=1)

This array does not empty the data when empty! is executed, but ilength=0, which avoids memory allocation, so the speed should be faster

No memory allocation is required when executing push!

I think someone has written such an array, but I didn’t find a ready-made one. I hope someone can tell, thank you

I took a look at StaticArrays, but he recommends no more than 100 elements

The built-in Array type already does this if you use sizehint!.

3 Likes

I found PushVectors, the code is almost the same as my idea, thanks for your hint!

You may also want to watch this issue, which led to the creation of PushVectors:
https://github.com/JuliaLang/julia/issues/24909

2 Likes