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

Grow large arrays more slowly #11482

Merged

Conversation

mgomes
Copy link
Contributor

@mgomes mgomes commented Nov 21, 2021

Fixes #11445

This introduces the heuristic found in Go for re-sizing arrays. For arrays < 256 elements, the heuristic doubles array capacities as before. For arrays >= 256, it starts to limit the growth of re-sizes. It does so in a linear fashion until around ~20k elements at which it limits array growth to ~25%.

I've included a couple specs showing the progression of the array growth with the new heuristic, but let me know if we should include more progressions. I was just concerned about memory usage.

Progression examples:

Initial Capacity Re-sized Capacity Delta %
512 832 62.5%
1024 1472 43.8%
2048 2752 34.4%
4096 5312 29.7%
8192 10432 27.3%
16384 20672 26.2%
32768 41152 25.6%
65536 82112 25.3%

src/array.cr Outdated Show resolved Hide resolved
src/array.cr Outdated Show resolved Hide resolved
@beta-ziliani
Copy link
Member

@mgomes do you want to continue this? Can you add some specs testing the capacity growth?

@mgomes
Copy link
Contributor Author

mgomes commented Nov 26, 2021

@beta-ziliani Thanks for the early review! Sorry I pushed up a draft because I couldn't get the test suite to run locally.

If you don't mind waiting a couple more days (it's a holiday here) I'd love to push this one to the finish line.

Copy link
Member

@beta-ziliani beta-ziliani left a comment

Choose a reason for hiding this comment

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

Thanks @mgomes !

@beta-ziliani beta-ziliani added this to the 1.3.0 milestone Nov 29, 2021
@@ -52,6 +52,12 @@ class Array(T)
# Size of an Array that we consider small to do linear scans or other optimizations.
private SMALL_ARRAY_SIZE = 16

# The initial capacity reserved for new arrays; just a lucky number
Copy link
Member

Choose a reason for hiding this comment

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

😄

This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Large arrays grow too much
5 participants