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

Tracking Issue for try_reserve method on more containers #91789

Closed
3 of 5 tasks
TennyZhuang opened this issue Dec 11, 2021 · 24 comments · Fixed by #95392
Closed
3 of 5 tasks

Tracking Issue for try_reserve method on more containers #91789

TennyZhuang opened this issue Dec 11, 2021 · 24 comments · Fixed by #95392
Labels
C-tracking-issue Category: An issue tracking the progress of sth. like the implementation of an RFC 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. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@TennyZhuang
Copy link
Contributor

TennyZhuang commented Dec 11, 2021

This is another tracking issue for the try_reserve part of the RFC "fallible collection allocation" (rust-lang/rfcs#2116).
The feature gate for the issue is #![feature(try_reserve_2)].

The feature is a follow-up to #48043. Since try_reserve was stabilized in 1.57.0, we may need a new feature gate for further try_reserve implementation on more containers.

About tracking issues

Tracking issues are used to record the overall progress of implementation.
They are also used as hubs connecting to other relevant issues, e.g., bugs or open design questions.
A tracking issue is however not meant for large scale discussion, questions, or bug reports about a feature.
Instead, open a dedicated issue for the specific matter and add the relevant feature gate label.

Steps

If any containers were missing, please comment in the issue, and I will add it to the list.

Unresolved Questions

Implementation history

#91529
#92338
#92513

@TennyZhuang TennyZhuang added the C-tracking-issue Category: An issue tracking the progress of sth. like the implementation of an RFC label Dec 11, 2021
@camelid camelid added the T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. label Dec 13, 2021
@Xuanwo
Copy link
Contributor

Xuanwo commented Dec 14, 2021

After a diff between https://doc.rust-lang.org/std/?search=reserve and https://doc.rust-lang.org/std/?search=try_reserve, I think the current list is complete.

@Xuanwo
Copy link
Contributor

Xuanwo commented Dec 14, 2021

I'm interested in implement try_reserve and try_reserve_exact on PathBuf.

@Xuanwo Xuanwo moved this to 📋 Backlog in Xuanwo's Work Dec 15, 2021
@Xuanwo Xuanwo moved this from 📋 Backlog to 🔨 In Progress in Xuanwo's Work Dec 28, 2021
@Xuanwo
Copy link
Contributor

Xuanwo commented Dec 28, 2021

@TennyZhuang Please help update the issue link into description: #92338

@Xuanwo
Copy link
Contributor

Xuanwo commented Dec 28, 2021

And the support of PathBuf will be based on work #92338, I will continue it after this PR get merged.

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Dec 31, 2021
Add try_reserve and  try_reserve_exact for OsString

Add `try_reserve` and `try_reserve_exact` for OsString.

Part of rust-lang#91789

I will squash the commits after PR is ready to merge.

Signed-off-by: Xuanwo <github@xuanwo.io>
@Xuanwo
Copy link
Contributor

Xuanwo commented Jan 3, 2022

@TennyZhuang #92513 has been submitted for review. Let's go forward the stabilization!

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jan 24, 2022
std: Implement try_reserve and try_reserve_exact on PathBuf

Part of rust-lang#91789

Signed-off-by: Xuanwo <github@xuanwo.io>
@Xuanwo
Copy link
Contributor

Xuanwo commented Jan 27, 2022

Let's stabilize this feature! Steps are listed here:

From https://rustc-dev-guide.rust-lang.org/stability.html#stabilizing-a-library-feature

  • Ask a T-libs-api member to start an FCP on the tracking issue and wait for the FCP to complete
  • Change #[unstable(...)] to #[stable(since = "version")]
  • Remove #![feature(...)] from any test or doc-test for this API
  • Open a PR against rust-lang/rust
    • Add the appropriate labels: @rustbot modify labels: +T-libs-api.
    • Link to the tracking issue and say "Closes #XXXXX".

@Xuanwo
Copy link
Contributor

Xuanwo commented Jan 27, 2022

@yaahc Hi, can you help start an FCP for feature try_reserve_2?

@Xuanwo Xuanwo moved this from 🔨 In Progress to 📋 Backlog in Xuanwo's Work Jan 29, 2022
@yaahc
Copy link
Member

yaahc commented Jan 31, 2022

Absolutely!

@rfcbot merge

@rfcbot
Copy link

rfcbot commented Jan 31, 2022

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

Concerns:

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, 2022
@dtolnay
Copy link
Member

dtolnay commented Feb 1, 2022

It's worth being aware that the OsString version of these methods is extremely weird, and probably not adequately documented about how weird it is. In particular the following "obvious" code is not correct, but it seems likely that almost every caller would fall into this trap unless there is very clear guidance on how to use the method correctly.

fn os_string_push_str(buf: &mut OsString, data: &str) -> Result<(), TryReserveError> {
    buf.try_reserve(data.len())?;
    buf.push(data);
    Ok(())
}

The units in which string length is measured, and the units in which OsString capacity is measured, are in general totally different units.

Quoting from elsewhere in the OsString docs:

As discussed in the OsString introduction, OsString and OsStr store strings in a form best suited for cheap inter-conversion between native-platform and Rust string forms, which may differ significantly from both of them, including in storage size and encoding.

This is saying that the "storage size" of an OS string (which is the size that len returns, and the same units as the additional arg of try_reserve) "may differ significantly" from the storage size of the "Rust string form" of the same data, i.e. that os_str.len() and os_str.to_str().unwrap().len() may differ significantly.

@rfcbot concern do we need better docs about what the argument value means (not bytes)

@TennyZhuang
Copy link
Contributor Author

It's worth being aware that the OsString version of these methods is extremely weird, and probably not adequately documented about how weird it is. In particular the following "obvious" code is not correct, but it seems likely that almost every caller would fall into this trap unless there is very clear guidance on how to use the method correctly.

I definitely agree that OsString::try_reserve is very easy to be used incorrectly, but I think the problem already exists due to the with_capacity API. Users may also got wrong results while they use with_capacity from another type’s len().

@dtolnay
Copy link
Member

dtolnay commented Feb 2, 2022

I agree that with_capacity also needs to be better explained. That function is from 2016 when our standards were lower. 😉

@Xuanwo
Copy link
Contributor

Xuanwo commented Feb 4, 2022

I will start PRs to address the concern that @dtolnay proposed.

  • Add better docs for OsString::try_reserve about what the argument value means
  • (Maybe) Polish the docs for OsString::with_capacity too ?

@m-ou-se
Copy link
Member

m-ou-se commented Feb 4, 2022

This is saying that the "storage size" of an OS string "may differ significantly" from the storage size of the "Rust string form" of the same data.

We should probably just update that documentation. We already guarantee any &str can be turned into a &OsStr through AsRef, so there's not much room for doing anything other than representing str data exactly as is in an OsStr, with the same length. So your example snippet will work fine. The only space left for 'surprises' is how many units/bytes unmatched UTF-16 surrogate pair halves take on Windows.

@Xuanwo
Copy link
Contributor

Xuanwo commented Mar 13, 2022

Sorry for stalling this work for so long; I'll take care of all the concerns next week.

@Xuanwo Xuanwo moved this from 📋 Backlog to 🔨 In Progress in Xuanwo's Work Mar 13, 2022
@dtolnay
Copy link
Member

dtolnay commented Mar 27, 2022

#91789 (comment) sounds good to me. I would be prepared to accept (with libs-api FCP) a PR that updates all the places talking about OS string capacity to indicate that this means UTF-8 byte size for OS strings which were created from valid unicode, and not otherwise specified for other contents.

@Xuanwo
Copy link
Contributor

Xuanwo commented Mar 28, 2022

Great, please take a look at #95392

@Xuanwo Xuanwo moved this from 🔨 In Progress to 🔍 In Review in Xuanwo's Work Apr 7, 2022
@joshtriplett
Copy link
Member

#97202

@dtolnay
Copy link
Member

dtolnay commented Jun 15, 2022

@rfcbot resolve do we need better docs about what the argument value means (not bytes)

@rfcbot rfcbot added the final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. label Jun 15, 2022
@rfcbot
Copy link

rfcbot commented Jun 15, 2022

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

@rfcbot rfcbot removed the proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. label Jun 15, 2022
@bors bors closed this as completed in b516806 Jun 17, 2022
@Xuanwo Xuanwo moved this from 🔍 In Review to 📦 Done in Xuanwo's Work Jun 20, 2022
@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 Jun 25, 2022
@Stargateur
Copy link
Contributor

Stargateur commented Jul 8, 2022

look like BTreeSet::try_reserve have been forgotten too.

Don't know what would be the plan for LinkedList

@Xuanwo
Copy link
Contributor

Xuanwo commented Jul 8, 2022

look like BTreeSet::try_reserve have been forgotten too.

Don't know what would be the plan for LinkedList

IMHO, we need a proposal to discuss adding reserve for BTressSet and LinkedList first.

@TennyZhuang
Copy link
Contributor Author

BTreeSet and LinkedList are pointer-based data structure, which means that they can't benefit from batch allocation. The reserve method on them is exactly same as just allocate one by one, but will make the code much more complicated (we have to introduce placeholder nodes for that).

@Stargateur
Copy link
Contributor

ah I get fouled by https://doc.rust-lang.org/src/core/iter/traits/collect.rs.html#379

these two collections probably need something like try_push() instead.

@apiraino apiraino removed the to-announce Announce this issue on triage meeting label Sep 8, 2022
workingjubilee pushed a commit to tcdi/postgrestd that referenced this issue Sep 15, 2022
std: Stabilize feature try_reserve_2

This PR intends to stabilize feature `try_reserve_2`, closes rust-lang/rust#91789

This PR will also replace the previous PR: rust-lang/rust#95139
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-tracking-issue Category: An issue tracking the progress of sth. like the implementation of an RFC 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. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants