Skip to content

Explain how Vec::with_capacity is faithful - #135933

Merged
bors merged 1 commit into
rust-lang:masterfrom
hkBst:patch-19
Feb 25, 2025
Merged

bors merged 1 commit into
rust-lang:masterfrom
hkBst:patch-19

Conversation

@hkBst

@hkBst hkBst commented Jan 23, 2025

Copy link
Copy Markdown
Member

This is a revival of #99790 building on the prose of @workingjubilee and edits of @jmaargh. Closes #99385.

@rustbot

ghost commented Jan 23, 2025

Copy link
Copy Markdown
Collaborator

r? @ibraheemdev

rustbot has assigned @ibraheemdev.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jan 23, 2025

ghost left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Awesome! Not only is this very clear and understandable, it's also a good design that can make use of advanced allocator capabilities.

Linking #101316 for convenience.

Comment thread library/alloc/src/vec/mod.rs Outdated
/// and no other size (such as, for example: a size rounded up to the nearest power of 2).
/// The allocator will return an allocation that is at least as large as requested, but it may be larger.
///
/// It is guaranteed that the [Vec::capacity] method returns a value that is at least the requested capacity

ghost Jan 31, 2025

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
/// It is guaranteed that the [Vec::capacity] method returns a value that is at least the requested capacity
/// It is guaranteed that the [`Vec::capacity`] method returns a value that is at least the requested capacity

ghost Feb 19, 2025

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I incorporated this.

Comment thread library/alloc/src/vec/mod.rs Outdated
/// It is guaranteed that the [Vec::capacity] method returns a value that is at least the requested capacity
/// and not more than the allocated capacity.
///
/// The method [Vec::shrink_to_fit] can discard excess capacity an allocator has given to a `Vec`.

ghost Jan 31, 2025

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
/// The method [Vec::shrink_to_fit] can discard excess capacity an allocator has given to a `Vec`.
/// The method [`Vec::shrink_to_fit`] can discard excess capacity an allocator has given to a `Vec`.

ghost Feb 19, 2025

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Also incorporated this.

@ibraheemdev

ghost commented Jan 31, 2025

Copy link
Copy Markdown
Member

r? @workingjubilee because there were some open questions on the original PR.

@rustbot

ghost commented Jan 31, 2025

Copy link
Copy Markdown
Collaborator

Could not assign reviewer from: workingjubilee.
User(s) workingjubilee are either the PR author, already assigned, or on vacation. Please use r? to specify someone else to assign.

@ibraheemdev

ghost commented Jan 31, 2025

Copy link
Copy Markdown
Member

Seems they are on vacation. r? libs-api because from what I understand this adds additional guarantees to the public API.

@rustbot rustbot added the T-libs-api [DEPRECATED; DO NOT USE] label Jan 31, 2025
@rustbot rustbot assigned dtolnay and unassigned ibraheemdev Jan 31, 2025
@dtolnay dtolnay removed the T-libs Relevant to the library team, which will review and decide on the PR/issue. label Jan 31, 2025
@dtolnay

ghost commented Jan 31, 2025

Copy link
Copy Markdown
Member

@rust-lang/libs-api:
@rfcbot fcp merge

This PR adds the guarantee that vec! and Vec::with_capacity request an allocation of the exact size needed for that number of elements (as long as size_of::<T>() * n > 0 as carved out in a previous paragraph).

Whether the actual allocation can hold a larger number of elements is up to the allocator. So this still only guarantees that vec.capacity() >= n, not ==.

Before this PR, we guaranteed vec.capacity() >= n but did not guarantee that n would be the requested size of the allocation. Hypothetically the Vec implementation could have rounded up the capacity, and that will no longer be allowed. Only the allocator gets to round up.

@rfcbot

ghost commented Jan 31, 2025 •

Copy link
Copy Markdown

Team member @dtolnay has proposed to merge this. The next step is review by the rest of the tagged team members:

No concerns currently listed.

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

See this document for info about what commands tagged team members can give me.

@rfcbot rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. labels Jan 31, 2025
@rfcbot rfcbot added final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. and removed proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. labels Feb 11, 2025
@rfcbot

ghost commented Feb 11, 2025

Copy link
Copy Markdown

🔔 This is now entering its final comment period, as per the review above. 🔔

Comment thread library/alloc/src/vec/mod.rs Outdated

ghost left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is what I was aiming at, yes. Thank you for incorporating this and @jmaargh's edits.

/// and from a [`Box<[T]>`][owned slice] without reallocating or moving the elements.
/// It is guaranteed, in order to respect the intentions of the programmer, that
/// all of `vec![e_1, e_2, ..., e_n]`, `vec![x; n]` and [`Vec::with_capacity(n)`] produce a `Vec`,
/// that requests an allocation of the exact size needed for precisely `n` elements from the allocator,

ghost Feb 21, 2025

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
/// that requests an allocation of the exact size needed for precisely `n` elements from the allocator,
/// that requests an allocation of the exact size needed for precisely `n` elements from the allocator

AFAIK there shouldn't be a comma here?

ghost Feb 21, 2025

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

If we replace this comma and the rest of the sentence with just a dot, then the sentence already says everything it should. Then if we want to re-add this clarifying clause, it seems logical to replace the dot with a comma and then the clause. Thus I think the comma is correct here, but I could be wrong.

I'm not sure I can justify the comma that comes before the part you quoted though...

ghost Feb 21, 2025

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ah sorry I meant to remove the one after Vec, indeed. That's the one that is definitely wrong.

ghost Feb 21, 2025

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed.

Co-authored-by: Jubilee <workingjubilee@gmail.com> and jmaargh
@rfcbot rfcbot added finished-final-comment-period The final comment period is finished for this PR / Issue. to-announce Announce this issue on triage meeting and removed final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. labels Feb 21, 2025
@rfcbot

ghost commented Feb 21, 2025

Copy link
Copy Markdown

The final comment period, with a disposition to merge, as per the review above, is now complete.

As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed.

This will be merged soon.

@workingjubilee

ghost commented Feb 24, 2025

Copy link
Copy Markdown
Member

@bors r+

@bors

ghost commented Feb 24, 2025

Copy link
Copy Markdown
Collaborator

📌 Commit da7210b has been approved by workingjubilee

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 24, 2025
@workingjubilee

ghost commented Feb 24, 2025

Copy link
Copy Markdown
Member

@bors rollup

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. finished-final-comment-period The final comment period is finished for this PR / Issue. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs-api [DEPRECATED; DO NOT USE]

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Removal of exact capacity guarantee for Vec::with_capacity() is a breaking change