Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

spzeros(T, n) allocates #386

Open
dpinol opened this issue May 9, 2023 · 3 comments
Open

spzeros(T, n) allocates #386

dpinol opened this issue May 9, 2023 · 3 comments
Labels
documentation Improvements or additions to documentation

Comments

@dpinol
Copy link
Contributor

dpinol commented May 9, 2023

According to documentation:

spzeros([type,]m[,n])
  .... No storage will be allocated for nonzero values during construction

However, it does allocate:

function spallo(n)
           for i in 1:n
               spzeros(Int, 10)
           end
 end
@allocated spallo(10)
1280

julia> @allocated spallo(100)
12800

@ViralBShah
Copy link
Member

It doesn't allocate for nonzero values. It still has to allocate for the SparseMatrixCSC data structure, roughly 1 int per column.

@dpinol
Copy link
Contributor Author

dpinol commented May 9, 2023

  • spzeros does not create a SparseMatrixCSC, but a SparseVector. I guess that spzeros allocates because it contains 2 vectors (one for dims and one for values). Even if they're initialized as empty, Julia allocates for empty vectors. See code below
  • In any case, I think the reason why it allocates is not important. What is important is that the documentation says that it does not allocate, and it does. This means that it's misleading when optimized code is required, since the documentation seems to imply that using it and pushing data might have the same performance as setting the data directly through other SparseVector constructors.
function f(n)
           for i in 1:n
               a= Int[]
               resize!(a,1)
           end
       end
f (generic function with 1 method)

julia> @allocations f(10)
20

@ViralBShah ViralBShah reopened this May 9, 2023
@ViralBShah
Copy link
Member

That is right, the empty vectors will be allocated. You are right - the documentation could be improved to not give the wrong impression. If you could make a PR that would be great.

@ViralBShah ViralBShah added the documentation Improvements or additions to documentation label May 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

2 participants