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

sizehint! now supports shrinking arrays #2879 #26201

Merged
merged 2 commits into from
Mar 7, 2018

Conversation

adrianisuru
Copy link
Contributor

sizehint! reduces the allocated space in some cases

sizehint! reduces the allocated space in some cases
@JeffBezanson JeffBezanson added performance Must go faster arrays [a, r, r, a, y, s] labels Feb 25, 2018
@adrianisuru
Copy link
Contributor Author

adrianisuru commented Feb 25, 2018

julia> A = zeros(400_000_000)
400000000-element Array{Float64,1}:

Top shows we are using 3.144g of memory.

32723 adrian    20   0 3704048 3.144g  62044 S   0.0 41.7   0:02.79 julia
julia> resize!(A, 1)
1-element Array{Float64,1}:
 0.0

julia> sizehint!(A, 1)
1-element Array{Float64,1}:
 0.0

After using sizehint! the memory has been released.

32723 adrian    20   0  782572 211692  87612 S   0.0  2.7   0:03.66 julia

src/array.c Outdated

if (sz <= a->maxsize) {
size_t dec = a->maxsize - sz;
//if we dont save >10% of maxsize then its not worth it to shrink
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would make it a factor of 2, since that's the increment that we grow by.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I disagree, going from 2 GB to 1.2 GB is a large gain. sizehint!-ing to a value shorter than the array is likely rare, and if you do, you likely want to regain the memory.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A possible rationale for the different behavior when shrinking vs. when growing is that you strictly need to increase the storage size to push! a new element, so it makes sense to directly double the size to avoid doing it repeatedly. OTC, to call pop!, you don't strictly need to shrink the storage size: you can perfectly call resize! after a bunch of pop! calls, and it will be more efficient. So it makes sense to optimize for this case and assume the user really wants the exact requested size.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree; if somebody specifically sizehint!s to a smaller size, the assumption that the array will keep growing doesn't really hold any more. We could compromise on 25% maybe?

if (sz <= a->maxsize) {
size_t dec = a->maxsize - sz;
//if we dont save at least an eighth of maxsize then its not worth it to shrink
if (dec < a->maxsize / 8) return;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a->maxsize >> 3? Though the compiler may do this optimization for us.

@KristofferC
Copy link
Member

Putting a reference to #33974 here. cc @adrianisuru

vtjnash pushed a commit that referenced this pull request Oct 29, 2020
Implement shrinking of small arrays

Refs #33974, #26201, #2879
Fixes #33902
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
arrays [a, r, r, a, y, s] performance Must go faster
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants