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

Why are SArrays extremely memory inefficient? #876

Closed
Volker-Weissmann opened this issue Feb 13, 2021 · 3 comments
Closed

Why are SArrays extremely memory inefficient? #876

Volker-Weissmann opened this issue Feb 13, 2021 · 3 comments

Comments

@Volker-Weissmann
Copy link

Hello,

This should allocate 8 MB,

zeros(SArray{Tuple{1024,1024},Float64})

but it prints

ERROR: syntax: expression too large

I did some smaller allocations, checked the output of htop and found that zeros(SArray{Tuple{128,N},Float64}) needs wayyyy more memory than the N kB expected.

@mschauer
Copy link
Collaborator

Note that in the current implementation, working with large StaticArrays puts a lot of stress on the compiler, and becomes slower than Base.Array as the size increases. A very rough rule of thumb is that you should consider using a normal Array for arrays larger than 100 elements.

From the ReadMe.md

@andyferris
Copy link
Member

Yes.

Basically most functions in StaticArrays are generated dynamically (via @generated) and consist of a small number of lines of code per array element. So the example above has millions of lines of code that needs to be processed by the compiler and LLVM.

The compiler not only temporarily uses these expressions while compiling but caches much of it. So the first time you call zeros(SArray{Tuple{128,N},Float64}) for some small N you will see htop memory usage bump quite a lot (also - these compiler cached things are stored until you quite Julia and are never freed).

For each instance of that type that you keep, you'll store an additional N kB of program data on top of all that compiled code. However - even then the compiler is free to choose to box such a large instance, using slightly more allocations and RAM.

While the documentation mentions arrays up to 100 elements I tend to recommend it for e.g. geometry with 3-vectors and rotation matrices, and similar.

@Volker-Weissmann
Copy link
Author

Ok. That's sad, but Thank you.
I think I need to stick to Base.Array then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants