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

Add doc comments about safest way to initialize a vector of zeros #54860

Merged
merged 2 commits into from
Oct 12, 2018

Conversation

mandeep
Copy link
Contributor

@mandeep mandeep commented Oct 5, 2018

This adds more information about the vec! macro as discussed in #54628. I think this is a good starting point, but I think additional detail is needed so that we can explain why vec! is safer than the alternatives.

@rust-highfive
Copy link
Collaborator

r? @shepmaster

(rust_highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Oct 5, 2018
@shepmaster
Copy link
Member

I don't know if this is something we wish to guarantee, especially the bits about "previously zeroed memory is requested from the operating system".

r? @alexcrichton

@mandeep
Copy link
Contributor Author

mandeep commented Oct 6, 2018

Yeah it's not likely to be the case on all systems, but I would like to somehow convey how this macro operates under the hood.

@alexcrichton
Copy link
Member

Thanks for the PR! I don't think we need to have the term "safest" in the description as this isn't any safer than other mechanisms, and I agree with @shepmaster that we shouldn't try to make any guarantees about where memory comes from.

I think it may be best if we instead say that this strategy is an optimized way to initialize with zeros but not go too much into the details about what the optimization is.

@mandeep
Copy link
Contributor Author

mandeep commented Oct 6, 2018

Is there anything we can add that will let users know that they should use the macro instead of the other options when initializing with zeros?

@the8472
Copy link
Member

the8472 commented Oct 6, 2018

How about this?

It can also initialize each element of a Vec with a given value. This may be more efficient than performing allocation and initialization in separate steps, especially when initializing with zero-values.

let vec = vec![0; 5];
assert_eq!(vec, [0, 0, 0, 0, 0]);

// equivalent, but potentially slower:
let mut vec1 = Vec::with_capacity(5);
vec1.resize(5, 0);

More weasely and also adds it to the code examples in case someone doesn't read the prose.

@alexcrichton
Copy link
Member

@the8472 that sounds great to me! @mandeep I think it's a bit strong to say that users should use this macro one way or another. We'll basically always keep the optimization of calling calloc where possible, but it's not always the case that speed needs to be at an optimum.

@alexcrichton
Copy link
Member

@bors: r+ rollup

@bors
Copy link
Contributor

bors commented Oct 9, 2018

📌 Commit 1e584bf has been approved by alexcrichton

@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 Oct 9, 2018
pietroalbini added a commit to pietroalbini/rust that referenced this pull request Oct 9, 2018
Add doc comments about safest way to initialize a vector of zeros

This adds more information about the vec! macro as discussed in rust-lang#54628. I think this is a good starting point, but I think additional detail is needed so that we can explain why vec! is safer than the alternatives.
pietroalbini added a commit to pietroalbini/rust that referenced this pull request Oct 10, 2018
Add doc comments about safest way to initialize a vector of zeros

This adds more information about the vec! macro as discussed in rust-lang#54628. I think this is a good starting point, but I think additional detail is needed so that we can explain why vec! is safer than the alternatives.
Mark-Simulacrum added a commit to Mark-Simulacrum/rust that referenced this pull request Oct 11, 2018
Add doc comments about safest way to initialize a vector of zeros

This adds more information about the vec! macro as discussed in rust-lang#54628. I think this is a good starting point, but I think additional detail is needed so that we can explain why vec! is safer than the alternatives.
kennytm added a commit to kennytm/rust that referenced this pull request Oct 12, 2018
Add doc comments about safest way to initialize a vector of zeros

This adds more information about the vec! macro as discussed in rust-lang#54628. I think this is a good starting point, but I think additional detail is needed so that we can explain why vec! is safer than the alternatives.
bors added a commit that referenced this pull request Oct 12, 2018
Rollup of 16 pull requests

Successful merges:

 - #54755 (Documents reference equality by address (#54197))
 - #54811 (During rustc bootstrap, make default for `optimize` independent of `debug`)
 - #54825 (NLL says "borrowed content" instead of more precise "dereference of raw pointer")
 - #54860 (Add doc comments about safest way to initialize a vector of zeros)
 - #54869 (Fix mobile docs)
 - #54891 (Fix tracking issue for Once::is_completed)
 - #54913 (doc fix: it's auto traits that make for automatic implementations)
 - #54920 (Fix handling of #[must_use] on unit and uninhabited types)
 - #54932 (A handful of random string-related improvements)
 - #54936 (impl Eq+Hash for TyLayout)
 - #54950 (std: Synchronize global allocator on wasm32)
 - #54956 ("(using ..." doesn't have the matching ")")
 - #54958 (add a macro for static (compile-time) assertions)
 - #54967 (Remove incorrect span for second label inner macro invocation)
 - #54983 (Fix slice's benchmarks)
 - #54989 (Fix spelling in the documentation to htmldocck.py)

Failed merges:

r? @ghost
@bors bors merged commit 1e584bf into rust-lang:master Oct 12, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants