-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Combine capacity guidance for maps and slices #97
Conversation
5a87129
to
8011d9b
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks pretty good, just left some small comments
style.md
Outdated
Specify container capacity where possible, as the compiler will allocate all | ||
required memory for that capacity up front rather than potentially allocating | ||
it in chunks as the container grows. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The mention of the compiler seems a little too detailed here (since technically, most of the performance guidance affects what the compiler will do, though we don't call it out explicitly).
I also want to emphasize that it's not just that we allocate the memory in chunks (it might seem like instead of allocating 1mb, we'll allocate 5x200kb), but that we'll end up constantly resizing which increases total allocations, and also total time spent due to copies.
Perhaps:
"Specify container capacity where possible to allocate all required memory for the container up front. This avoids copies + resizing of the container as elements are added."
would replace mention of the compiler here (since it can happen at runtime for heap allocations), and the issue isn't just the chunks, but the resizing (e.g., overall more memory allocated + copies)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I think you're right. I generalized this a bit more to apply more correctly to both slices and maps (since map capacity hints don't guarantee that you'll "allocate all required memory for the container up front" obv) - not trying to be overly pedantic, just want the text to be accurate. Let me know what you think.
8011d9b
to
92d7cac
Compare
style.md
Outdated
### Prefer Specifying Container Capacity | ||
|
||
Specify container capacity where possible in order to allocate memory for the | ||
container up front. This minimizes subsequent allocations (by copying and |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
super nit: I think there's two spaces before "This"
style.md
Outdated
Unlike maps, slice capacity is not a hint: the compiler will allocate enough | ||
memory for the capacity of the slice as provided to `make()`, which means that | ||
subsequent `append()` operations will incur zero allocations (until the length | ||
of the slice matches the capacity, which will require a resize to hold |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/which will require a resize/after which any appends will require a resize/
Per @prashantv's feedback on #79:
This also expands a bit more on map allocations at initialization time.